LoginSignup
9
3

More than 3 years have passed since last update.

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

Last updated at Posted at 2019-12-17

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

今年あった切実な事例

ユーザの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.

ってことで

以上
やましょうでした。

9
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
3