Help us understand the problem. What is going on with this article?

DelphiでArduinoのhexを自動書込&パラメータを設定するexeを作る。

こんにちは、
やましょうです。

今年あった切実な事例

ユーザのArudino の中身を書込みを遠隔地のユーザーに行ってもらう。
Arduino ideとか面倒なものはpcにインストールさせたくない。

さて困ったときのdekoさん頼みで
https://ht-deko.com/arduino/usbasp.html
を見る
要約すると1行でいける筈。

実行バッチCmdの作成

Firmudate.cmd
avrdude -c arduino -P %1 -b 115200 -p m328p -U flash:w:%2

というファイルをとりあえず、作る。

Delphiでcomポート名よりArduino Unoをみつけてcomポート番号指定して、バッチを実行すれば良い。

Apiをごにょれば良い。

procedure TForm.FormCreate(Sender: TObject);
var
ClassGuid     : TGUID;
dwRequiredSize: DWORD;
DeviceInfoSet  : HDEVINFO;
DeviceInfoData : SP_DEVINFO_DATA;
dwMemberIndex  : DWORD;
i : Integer;
Str : String;

szFriendlyName : array [0..255] of AnsiChar;
szPortName     : array [0..255] of WideChar;

check_Port: string;
dwReqSize : DWORD;
dwPropType : DWORD;
dwType: DWORD;
PortNumber : Integer;
HkeyData :  hKey;
ItemList : TStringList;

LFile   : string;
LParams : string;
begin
    DeviceInfoSet :=SetupDiGetClassDevs(@ClassGuid, Nil, 0, DIGCF_PRESENT or DIGCF_PROFILE);
    if Assigned( DeviceInfoSet  ) then
  begin
    ///// デバイス情報があったのでデバイスを列挙 //////////////
    dwMemberIndex := 0;
        while (SetupDiEnumDeviceInfo
               (DeviceInfoSet, dwMemberIndex, @DeviceInfoData)) do
    begin
       dwReqSize := 0;
       dwType    := REG_SZ;
             SetupDiGetDeviceRegistryProperty(DeviceInfoSet,
                                              @DeviceInfoData,
                                                    SPDRP_FRIENDLYNAME,
                                                    @dwPropType,
                                                    @szFriendlyName,
                                                    sizeof(szFriendlyName),
                                                    @dwReqSize);

            hKeyData := SetupDiOpenDevRegKey(DeviceInfoSet,
                                        @DeviceInfoData,
                                        DICS_FLAG_GLOBAL,
                                        0, DIREG_DEV, KEY_READ);

            if (hKeyData <> 0) then
      begin
            dwReqSize := sizeof(szPortName);
                 RegQueryValueEx(hKeyData,
                                       'PortName',
                                       NIL,
                                       @dwType,
                                       @szPortName,
                                       @dwReqSize);
                RegCloseKey(hKeyData);
      end;

     Check_Port := 'Arduino Uno';
    if(AnsiStrLComp(szFriendlyName,'Arduino Uno',10) = 0 ) then
     begin
         LFile   := ExtractFilePath(paramstr(0))+'Firmudate.cmd';
         Str := szPortName;
         LParams :=STR+' '+書きたい.hex';
        ShellExecute(Handle, 'open', PChar(LFile), PChar(LParams), NIL, SW_SHOWNORMAL);
       exit;
     end;
       Inc(dwMemberIndex);
    end;
  end;
end;

ってことでusbでArudinoをつなげて、アイコン叩けば自動でupdateできる.exeはDelphiで簡単にできました。

確認する。

(書きたい.hexは本当は英語ファイル名)

avrdude -c arduino -P COM5 -b 115200 -p m328p -U flash:w:書きたい.hex

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x1e950f
avrdude: NOTE: FLASH memory has been specified, an erase cycle will be performed
         To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: reading input file "書きたい.hex"
avrdude: input file 書きたい.hex auto detected as Intel Hex
avrdude: writing flash (1292 bytes):

Writing | ################################################## | 100% 0.33s

avrdude: 1292 bytes of flash written
avrdude: verifying flash memory against 書きたい.hex:
avrdude: load data flash data from input file 書きたい.hex:
avrdude: input file 書きたい.hex auto detected as Intel Hex
avrdude: input file 書きたい.hex contains 1292 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 0.33s

avrdude: verifying ...
avrdude: 1292 bytes of flash verified

avrdude: safemode: Fuses OK

avrdude done.  Thank you.

ってことで

以上
やましょうでした。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした