1

Тема: AHK: Получения информации в ahk о подключенных к Arduino кнопках

Доброго времени суток, на англо-язычных ресурсах нашел следующее

ARDUINO_Port     = COM7
ARDUINO_Baud     = 9600
ARDUINO_Parity   = N
ARDUINO_Data     = 8
ARDUINO_Stop     = 1
; setup arduino serial communication
arduino_setup()
; *****************GUI CODE***************
	Gui, +AlwaysOnTop -Caption +ToolWindow +LastFound
	Gui, Color, ffffff
	Gui, Font, s24, w1000
	Gui, Font, 000000
	Gui, Add, Text, vMyText1, cLime cccccccccccccccccccccccccccccccc
	Gui, Show, x1 y0 na
	SetTimer, UpdateOSD, 10
return

UpdateOSD:
vag = % arduino_send(data)
if vag = 1
vv = D
else if vag = 0
vv = U
GuiControl,, MyText1, %vv% %vag%
return

GuiClose:
OnExit:
	; make sure to cleanly shut down serial port on exit
	arduino_close()
; this is important!! or else theprogram does not end when closed
ExitApp

#include %A_ScriptDir%\Arduino.ahk

$*f7::
exitapp

Arduino.ahk

+ открыть спойлер
#include %A_ScriptDir%\Serial.ahk
; Arduino AHK Library
arduino_setup(){
	global
	ARDUINO_Settings = %ARDUINO_Port%:baud=%ARDUINO_Baud% parity=%ARDUINO_Parity% data=%ARDUINO_Data% stop=%ARDUINO_Stop% dtr=off ;to=off  xon=off odsr=off octs=off rts=off idsr=off
	ARDUINO_Handle := Serial_Initialize(ARDUINO_Settings)
	arduino_send("")
	;SetTimer, arduino_poll_serial, -1

}

arduino_send(data){
	global ARDUINO_Handle
	Serial_Write(ARDUINO_Handle, data)
	sleep, 50
	return arduino_read_raw()
}

arduino_read(){
	global ARDUINO_Handle
	return Serial_Read(ARDUINO_Handle, "0xFF", Bytes_Received)
}

arduino_read_raw(){
	global ARDUINO_Handle
	return Serial_Read_Raw(ARDUINO_Handle, "0xFF", "raw",Bytes_Received)
}

arduino_close(){
	global ARDUINO_Handle
	SetTimer, arduino_poll_serial, Off
	Sleep, 100
	Serial_Close(ARDUINO_Handle)
}

arduino_poll_serial:
	if IsFunc(f:="OnSerialData"){
	SerialData := arduino_read_raw()
	if SerialData {
			%f%(SerialData)
		}
	SetTimer, arduino_poll_serial, -1
	}else{
		MsgBox, OnSerialData function not defined. 
		SetTimer, arduino_poll_serial, Off
		}
return

Serial.ahk

+ открыть спойлер
; from here: http://www.autohotkey.com/forum/viewtopic.php?p=290253#290253
;########################################################################
;###### Initialize COM Subroutine #######################################
;########################################################################
Serial_Initialize(SERIAL_Settings){
  ;Global SERIAL_FileHandle      ;uncomment this if there is a problem

  ;###### Build COM DCB ######
  ;Creates the structure that contains the COM Port number, baud rate,...
  VarSetCapacity(DCB, 28)
  BCD_Result := DllCall("BuildCommDCB"
       ,"str" , SERIAL_Settings ;lpDef
       ,"UInt", &DCB)        ;lpDCB
  If (BCD_Result <> 1){
    error := DllCall("GetLastError")
    MsgBox, There is a problem with Serial Port communication. `nFailed Dll BuildCommDCB, BCD_Result=%BCD_Result% `nLasterror=%error%`nThe Script Will Now Exit.
    ExitApp
  }

  ;###### Extract/Format the COM Port Number ######
  StringSplit, SERIAL_Port_Temp, SERIAL_Settings, `:
  SERIAL_Port_Temp1_Len := StrLen(SERIAL_Port_Temp1)  ;For COM Ports > 9 \\.\ needs to prepended to the COM Port name.
  If (SERIAL_Port_Temp1_Len > 4)                   ;So the valid names are
    SERIAL_Port = \\.\%SERIAL_Port_Temp1%             ; ... COM8  COM9   \\.\COM10  \\.\COM11  \\.\COM12 and so on...
  Else                                          ;
    SERIAL_Port = %SERIAL_Port_Temp1%
  ;MsgBox, SERIAL_Port=%SERIAL_Port%

  ;###### Create COM File ######
  ;Creates the COM Port File Handle
  ;StringLeft, SERIAL_Port, SERIAL_Settings, 4  ; 7/23/08 This line is replaced by the "Extract/Format the COM Port Number" section above.
  SERIAL_FileHandle := DllCall("CreateFile"
       ,"Str" , SERIAL_Port     ;File Name
       ,"UInt", 0xC0000000   ;Desired Access
       ,"UInt", 3            ;Safe Mode
       ,"UInt", 0            ;Security Attributes
       ,"UInt", 3            ;Creation Disposition
       ,"UInt", 0            ;Flags And Attributes
       ,"UInt", 0            ;Template File
       ,"Cdecl Int")
  If (SERIAL_FileHandle < 1){
    error := DllCall("GetLastError")
    MsgBox, There is a problem with Serial Port communication. `nFailed Dll CreateFile, SERIAL_FileHandle=%SERIAL_FileHandle% `nLasterror=%error%`nThe Script Will Now Exit.
    ExitApp
  }

  ;###### Set COM State ######
  ;Sets the COM Port number, baud rate,...
  SCS_Result := DllCall("SetCommState"
       ,"UInt", SERIAL_FileHandle ;File Handle
       ,"UInt", &DCB)          ;Pointer to DCB structure
  If (SCS_Result <> 1){
    error := DllCall("GetLastError")
    MsgBox, There is a problem with Serial Port communication. `nFailed Dll SetCommState, SCS_Result=%SCS_Result% `nLasterror=%error%`nThe Script Will Now Exit.
    Serial_Close(SERIAL_FileHandle)
    ExitApp
  }

  ;###### Create the SetCommTimeouts Structure ######
  ReadIntervalTimeout        = 0xffffffff
  ReadTotalTimeoutMultiplier = 0x00000000
  ReadTotalTimeoutConstant   = 0x00000000
  WriteTotalTimeoutMultiplier= 0x00000000
  WriteTotalTimeoutConstant  = 0x00000000

  VarSetCapacity(Data, 20, 0) ; 5 * sizeof(DWORD)
  NumPut(ReadIntervalTimeout,         Data,  0, "UInt")
  NumPut(ReadTotalTimeoutMultiplier,  Data,  4, "UInt")
  NumPut(ReadTotalTimeoutConstant,    Data,  8, "UInt")
  NumPut(WriteTotalTimeoutMultiplier, Data, 12, "UInt")
  NumPut(WriteTotalTimeoutConstant,   Data, 16, "UInt")

  ;###### Set the COM Timeouts ######
  SCT_result := DllCall("SetCommTimeouts"
     ,"UInt", SERIAL_FileHandle ;File Handle
     ,"UInt", &Data)         ;Pointer to the data structure
  If (SCT_result <> 1){
    error := DllCall("GetLastError")
    MsgBox, There is a problem with Serial Port communication. `nFailed Dll SetCommState, SCT_result=%SCT_result% `nLasterror=%error%`nThe Script Will Now Exit.
    Serial_Close(SERIAL_FileHandle)
    ExitApp
  }

  Return SERIAL_FileHandle
}

;########################################################################
;###### Close COM Subroutine ############################################
;########################################################################
Serial_Close(SERIAL_FileHandle){
  ;###### Close the COM File ######
  CH_result := DllCall("CloseHandle", "UInt", SERIAL_FileHandle)
  If (CH_result <> 1)
    MsgBox, Failed Dll CloseHandle CH_result=%CH_result%
  Return
}

;########################################################################
;###### Write to COM Subroutines ########################################
;########################################################################
Serial_Write(SERIAL_FileHandle, Message){
  ;Global SERIAL_FileHandle

  SetFormat, Integer, DEC

  ;Parse the Message. Byte0 is the number of bytes in the array.
  StringSplit, Byte, Message, `,
  Data_Length := Byte0
  ;msgbox, Data_Length=%Data_Length% b1=%Byte1% b2=%Byte2% b3=%Byte3% b4=%Byte4%

  ;Set the Data buffer size, prefill with 0xFF.
  VarSetCapacity(Data, Byte0, 0xFF)

  ;Write the Message into the Data buffer
  i=1
  Loop %Byte0% {
    NumPut(Byte%i%, Data, (i-1) , "UChar")
    ;msgbox, %i%
    i++
  }
  ;msgbox, Data string=%Data%

  ;###### Write the data to the COM Port ######
  WF_Result := DllCall("WriteFile"
       ,"UInt" , SERIAL_FileHandle ;File Handle
       ,"UInt" , &Data          ;Pointer to string to send
       ,"UInt" , Data_Length    ;Data Length
       ,"UInt*", Bytes_Sent     ;Returns pointer to num bytes sent
       ,"Int"  , "NULL")
  If (WF_Result <> 1 or Bytes_Sent <> Data_Length)
    MsgBox, Failed Dll WriteFile to COM Port, result=%WF_Result% `nData Length=%Data_Length% `nBytes_Sent=%Bytes_Sent%
   
    Return Bytes_Sent
}

;########################################################################
;###### Read from COM Subroutines #######################################
;########################################################################
Serial_Read(SERIAL_FileHandle, Num_Bytes, byref Bytes_Received = ""){
  ;Global SERIAL_FileHandle
  ;Global SERIAL_Port
  ;Global Bytes_Received
  SetFormat, Integer, HEX

  ;Set the Data buffer size, prefill with 0x55 = ASCII character "U"
  ;VarSetCapacity won't assign anything less than 3 bytes. Meaning: If you
  ;  tell it you want 1 or 2 byte size variable it will give you 3.
  Data_Length  := VarSetCapacity(Data, Num_Bytes, 0)
  ;msgbox, Data_Length=%Data_Length%
	;Bytes_Received := 0

  ;###### Read the data from the COM Port ######
  ;msgbox, SERIAL_FileHandle=%SERIAL_FileHandle% `nNum_Bytes=%Num_Bytes%
  Read_Result := DllCall("ReadFile"
       ,"UInt" , SERIAL_FileHandle   ; hFile
       ,"Str"  , Data             ; lpBuffer
       ,"Int"  , Num_Bytes        ; nNumberOfBytesToRead
       ,"UInt*", Bytes_Received   ; lpNumberOfBytesReceived
       ,"Int"  , 0)               ; lpOverlapped
  ;MsgBox, Read_Result=%Read_Result% `nBR=%Bytes_Received% ,`nData=%Data%
  If (Read_Result <> 1){
    MsgBox, There is a problem with Serial Port communication. `nFailed Dll ReadFile on COM Port, result=%Read_Result% - The Script Will Now Exit.
    Serial_Close(SERIAL_FileHandle)
    Exit
  }

  ;###### Format the received data ######
  ;This loop is necessary because AHK doesn't handle NULL (0x00) characters very nicely.
  ;Quote from AHK documentation under DllCall:
  ;     "Any binary zero stored in a variable by a function will hide all data to the right
  ;     of the zero; that is, such data cannot be accessed or changed by most commands and
  ;     functions. However, such data can be manipulated by the address and dereference operators
  ;     (& and *), as well as DllCall itself."
  i = 0
  Data_HEX =
  Loop %Bytes_Received% {
    ;First byte into the Rx FIFO ends up at position 0

    Data_HEX_Temp := NumGet(Data, i, "UChar") ;Convert to HEX byte-by-byte
    StringTrimLeft, Data_HEX_Temp, Data_HEX_Temp, 2 ;Remove the 0x (added by the above line) from the front

    ;If there is only 1 character then add the leading "0'
    Length := StrLen(Data_HEX_Temp)
    If (Length =1)
      Data_HEX_Temp = 0%Data_HEX_Temp%

    i++

    ;Put it all together
    Data_HEX .= Data_HEX_Temp
  }
  ;MsgBox, Read_Result=%Read_Result% `nBR=%Bytes_Received% ,`nData_HEX=%Data_HEX%

  SetFormat, Integer, DEC
  Data := Data_HEX

  Return Data

}

;########################################################################
;###### Read from COM Subroutines #######################################
;########################################################################
Serial_Read_Raw(SERIAL_FileHandle, Num_Bytes, mode = "",byref Bytes_Received = ""){
  ;Global SERIAL_FileHandle
  ;Global SERIAL_Port
  ;Global Bytes_Received
  SetFormat, Integer, HEX

  ;Set the Data buffer size, prefill with 0x55 = ASCII character "U"
  ;VarSetCapacity won't assign anything less than 3 bytes. Meaning: If you
  ;  tell it you want 1 or 2 byte size variable it will give you 3.
  Data_Length  := VarSetCapacity(Data, Num_Bytes, 0)
  ;msgbox, Data_Length=%Data_Length%


  ;###### Read the data from the COM Port ######
  ;msgbox, SERIAL_FileHandle=%SERIAL_FileHandle% `nNum_Bytes=%Num_Bytes%
  Read_Result := DllCall("ReadFile"
       ,"UInt" , SERIAL_FileHandle   ; hFile
       ,"Str"  , Data             ; lpBuffer
       ,"Int"  , Num_Bytes        ; nNumberOfBytesToRead
       ,"UInt*", Bytes_Received   ; lpNumberOfBytesReceived
       ,"Int"  , 0)               ; lpOverlapped
  ;MsgBox, Read_Result=%Read_Result% `nBR=%Bytes_Received% ,`nData=%Data%
  If (Read_Result <> 1){
    MsgBox, There is a problem with Serial Port communication. `nFailed Dll ReadFile on COM Port, result=%Read_Result% - The Script Will Now Exit.
    Serial_Close(SERIAL_FileHandle)
    Exit
  }

  ;if you know the data coming back will not contain any binary zeros (0x00), you can request the 'raw' response
  If (mode = "raw")
    Return Data

  ;###### Format the received data ######
  ;This loop is necessary because AHK doesn't handle NULL (0x00) characters very nicely.
  ;Quote from AHK documentation under DllCall:
  ;     "Any binary zero stored in a variable by a function will hide all data to the right
  ;     of the zero; that is, such data cannot be accessed or changed by most commands and
  ;     functions. However, such data can be manipulated by the address and dereference operators
  ;     (& and *), as well as DllCall itself."
  i = 0
  Data_HEX =
  Loop %Bytes_Received% 
  {
    ;First byte into the Rx FIFO ends up at position 0

    Data_HEX_Temp := NumGet(Data, i, "UChar") ;Convert to HEX byte-by-byte
    StringTrimLeft, Data_HEX_Temp, Data_HEX_Temp, 2 ;Remove the 0x (added by the above line) from the front

    ;If there is only 1 character then add the leading "0'
    Length := StrLen(Data_HEX_Temp)
    If (Length =1)
      Data_HEX_Temp = 0%Data_HEX_Temp%

    i++

    ;Put it all together
    Data_HEX .= Data_HEX_Temp
  }
  ;MsgBox, Read_Result=%Read_Result% `nBR=%Bytes_Received% ,`nData_HEX=%Data_HEX%

  SetFormat, Integer, DEC
  Data := Data_HEX

  Return Data

}

sketch_arduino

+ открыть спойлер
const int buttonPin = 6;
int buttonState = 0;

void setup() {
  pinMode(buttonPin, INPUT_PULLUP);
 Serial.begin(9600);
}

void loop() {
 buttonState = digitalRead(buttonPin);
     Serial.print(buttonState);
  delay(40);
 }

Одна кнопка работает хорошо, D нажата U отпущена, но стоит добавить вторую.
sketch_arduino

+ открыть спойлер
int b1 = 0;
int b2 = 0;

void setup() {
 pinMode(5, INPUT_PULLUP);
 pinMode(6, INPUT_PULLUP);
 Serial.begin(9600);
}

void loop() {
  b1 = !digitalRead(5);
  b2 = !digitalRead(6);
      Serial.print("b1");Serial.print(b1);
      Serial.print("b2");Serial.print(b2);
 }

В переменную %vag% начинают выводится символы не понятные, иероглифы, причем в Arduino IDE монитор порта все верно иероглифов нет, прошу помощи что изменить в скрипте или sketch чтобы ahk отображала информацию о нескольких кнопках.

2 (изменено: Alectric, 2017-11-29 12:37:59)

Re: AHK: Получения информации в ahk о подключенных к Arduino кнопках

AHK: Работа с COM портом для микроконтроллеров

#SingleInstance,Force
CoordMode,ToolTip
SetBatchLines,-1
SetWorkingDir,%A_ScriptDir%
OnExit,EXIT

DebugMode=1 ; показывать отладочную информацию

global COMFail ; 1 - ошибка ком порта
tie_interval=100 ; начальный интервал связи, если длина запрошенных
                ; и полученных байт будет отличаться - интервал увеличится




PortName:="USB-SERIAL CH340" ; имя порта для автоматического поиска




Receive_Data_Size=5 ; длина запрошенных из порта байт
Send_Data_Size=5    ; длина отправляемых в порт байт

loop ; основной цикл
{
  sleep,10
  Loop_Time:=oldLoop_Time!="" ? a_tickcount-oldLoop_Time : 0 ; вычисляем время одного цикла
  oldLoop_Time:=a_tickcount





  if (!RS232_FileHandle or COMFail) ; если хэндл порта не взят или ошибка ком порта
  {
    COMFail=0
    ; повторно искать порт и брать хэндл
    RS232_FileHandle:=Get_RS232_FileHandle(COMPort:=Seach_COM(PortName))
                    ; Get_RS232_FileHandle(номер порта, скорость связи в бодах, четность, битность, стоповые биты)

    ; вместо поиска можно жёстко задать № порта
    ;RS232_FileHandle:=Get_RS232_FileHandle(COMPort:="COM2")

  }

  if (!timer:=timer>1000 ? 0 : timer="" ? 1 : timer+Loop_Time)
    b_0:=!b_0    ; 1 байт

  if RS232_FileHandle ; если хендл есть, читаем и пишем в порт
  {
    if (!tie:=tie>tie_interval ? 0 : tie="" ? 1 : tie+Loop_Time) ; с интервалом в tie_interval милисекунд
    {
      ; данные для отправки
      b_1++          ; 1 байт
      b_2=255        ; 1 байт
      b_3=111        ; 1 байт
      b_4=128        ; 1 байт

      varsetcapacity(Out_Data,Send_Data_Size) ; задаём размер переменной для отправки
      NumPut(b_0,Out_Data,0,"Uchar")          ; 1 байт
      NumPut(b_1,Out_Data,1,"Uchar")          ; 1 байт
      NumPut(b_2,Out_Data,2,"Uchar")          ; 1 байт
      NumPut(b_3,Out_Data,3,"Uint")           ; 1 байт
      NumPut(b_4,Out_Data,4,"Uchar")          ; 1 байт

      ; пишем данные в порт
      RS232_Write(RS232_FileHandle,&Out_Data,Send_Data_Size)

      ; читаем ответ от порта в переменную In_Data
      varsetcapacity(In_Data,Receive_Data_Size)
      RS232_Read(RS232_FileHandle,Receive_Data_Size,RS232_Bytes_Received,In_Data)

      ; если длина запрошенных и полученных байт одинакова выполняем присвоение переменным
      if (RS232_Bytes_Received==Receive_Data_Size)
      {
        ; пишем полученные данные в свои переменные
        button_1_Pin:=numget(In_Data,0,"uchar")		; 1 байт
        button_2_Pin:=numget(In_Data,1,"uchar")		; 1 байт
        button_3_Pin:=numget(In_Data,2,"uchar")		; 1 байт
        button_4_Pin:=numget(In_Data,3,"uchar")		; 1 байт
        button_5_Pin:=numget(In_Data,4,"uchar")		; 1 байт
      }
      else ; если не одинакова - увеличиваем интервал между запросами
      {
        tie_interval+=1
        if tie_interval>1000 ; максимально возможный интервал, ms
          tie_interval=100
      }

      if DebugMode ; показывать отладочную информацию
      {            ; перечислять все полученные байты, независимо от типов данных
        v=
        loop,% RS232_Bytes_Received
        {
          tmp:=numget(In_Data,a_index-1,"uchar")
          v.=a_index-1 "`t" format("0x{:02x}",tmp) "  " tmp "`n"
        }
        tooltip,
        (LTrim
          COMPort=%COMPort%
          COM handle=%RS232_FileHandle%
          Bytes Received=%RS232_Bytes_Received%/%Receive_Data_Size%

          Recieved data:
          %v%
        ),400,0,2
      }

      ; выводим на экран
      ;tooltip,% "button_1=" button_1_Pin "`nbutton_2=" button_2_Pin "`nbutton_3=" button_3_Pin "`nbutton_4=" button_4_Pin "`nbutton_5=" button_5_Pin
    }
  }
}
return


esc::
EXIT:
if RS232_FileHandle
  RS232_Close(RS232_FileHandle)
exitapp

Seach_COM(name)
{
  Loop,HKLM,HARDWARE\DEVICEMAP\SERIALCOMM\
  {
    RegRead,COMPort
    com.=COMPort "|"
    num++
  }
  loop,parse,com,|
  {
    if !A_loopfield
      continue
    COMPort:=A_loopfield
    Loop,HKLM,SYSTEM\CurrentControlSet\Enum,1,1
    {
      if (A_LoopRegName="PortName")
      {
        RegRead,Outputvar
        if (Outputvar=COMPort)
        {
          RegRead,FriendlyName,% A_LoopRegKey,% RegExReplace(A_LoopRegSubKey, "(.*)\\Device Parameters", "$1"),FriendlyName
          if InStr(FriendlyName,name)
            return COMPort
        }
      }
    }
  }
}

Get_RS232_FileHandle(RS232_Port,RS232_Baud=9600,RS232_Parity="N",RS232_Data=8,RS232_Stop=1)
{
  sleep,1000
  RS232_Settings = %RS232_Port%:baud=%RS232_Baud% parity=%RS232_Parity% data=%RS232_Data% stop=%RS232_Stop% dtr=Off xon=off to=off odsr=off octs=off rts=off idsr=off
  return RS232_Initialize(RS232_Settings)
}

RS232_Initialize(RS232_Settings)
{
  ;###### Extract/Format the RS232 COM Port Number ######
  ;7/23/08 Thanks krisky68 for finding/solving the bug in which RS232 COM Ports greater than 9 didn't work.
  StringSplit, RS232_Temp, RS232_Settings, `:
  RS232_Temp1_Len := StrLen(RS232_Temp1)  ;For COM Ports > 9 \\.\ needs to prepended to the COM Port name.
  If (RS232_Temp1_Len > 4)                   ;So the valid names are
    RS232_COM = \\.\%RS232_Temp1%             ; ... COM8  COM9   \\.\COM10  \\.\COM11  \\.\COM12 and so on...
  Else                                          ;
    RS232_COM = %RS232_Temp1%

  ;8/10/09 A BIG Thanks to trenton_xavier for figuring out how to make COM Ports greater than 9 work for USB-Serial Dongles.
  StringTrimLeft, RS232_Settings, RS232_Settings, RS232_Temp1_Len+1 ;Remove the COM number (+1 for the semicolon) for BuildCommDCB.
  ;MsgBox, RS232_COM=%RS232_COM% `nRS232_Settings=%RS232_Settings%

  ;###### Build RS232 COM DCB ######
  ;Creates the structure that contains the RS232 COM Port number, baud rate,...
  VarSetCapacity(DCB, 28)
  BCD_Result := DllCall("BuildCommDCB"
      ,"str" , RS232_Settings ;lpDef
      ,"ptr", &DCB)        ;lpDCB
;  If (BCD_Result != 1)
;  {
;    MsgBox, There is a problem with Serial Port communication. `nFailed Dll BuildCommDCB, BCD_Result=%BCD_Result% `nThe Script Will Now COMFail.`n %DCB%
;    COMFail=1
;    return
;  }

  ;###### Create RS232 COM File ######
  ;Creates the RS232 COM Port File Handle
  RS232_FileHandle := DllCall("CreateFile"
      ,"Str" , RS232_COM    ;File Name
      ,"UInt", 0xC0000000   ;Desired Access
      ,"UInt", 3            ;Safe Mode
      ,"UInt", 0            ;Security Attributes
      ,"UInt", 3            ;Creation Disposition
      ,"UInt", 0            ;Flags And Attributes
      ,"ptr" , 0            ;Template File
      ,"Cdecl Int")

  If (!RS232_FileHandle)
  {
;;    MsgBox, There is a problem with Serial Port communication. `nFailed Dll CreateFile, RS232_FileHandle=%RS232_FileHandle% `nThe Script Will Now COMFail.
    COMFail=1
    return
  }

  ;###### Set COM State ######
  ;Sets the RS232 COM Port number, baud rate,...
  SCS_Result := DllCall("SetCommState"
      ,"ptr", RS232_FileHandle ;File Handle
      ,"ptr", &DCB)            ;Pointer to DCB structure
  If (SCS_Result!=1)
  {
;;    MsgBox, There is a problem with Serial Port communication. `nFailed Dll SetCommState, SCS_Result=%SCS_Result% `nThe Script Will Now COMFail.
    RS232_Close(RS232_FileHandle)
    COMFail=1
    return
  }

  ;###### Create the SetCommTimeouts Structure ######
  ReadIntervalTimeout        = 0x01000000
  ReadTotalTimeoutMultiplier = 0x00000000
  ReadTotalTimeoutConstant   = 0x00000010
  WriteTotalTimeoutMultiplier= 0x00000000
  WriteTotalTimeoutConstant  = 0x00000010

  VarSetCapacity(Data,20) ; 5 * sizeof(DWORD)
  NumPut(ReadIntervalTimeout,         Data,  0, "UInt")
  NumPut(ReadTotalTimeoutMultiplier,  Data,  4, "UInt")
  NumPut(ReadTotalTimeoutConstant,    Data,  8, "UInt")
  NumPut(WriteTotalTimeoutMultiplier, Data, 12, "UInt")
  NumPut(WriteTotalTimeoutConstant,   Data, 16, "UInt")

  ;###### Set the RS232 COM Timeouts ######
  SCT_result := DllCall("SetCommTimeouts"
    ,"ptr", RS232_FileHandle ;File Handle
    ,"ptr", &Data)         ;Pointer to the data structure
  If (SCT_result != 1)
  {
;;    MsgBox, There is a problem with Serial Port communication. `nFailed Dll SetCommState, SCT_result=%SCT_result% `nThe Script Will Now COMFail.
    RS232_Close(RS232_FileHandle)
    COMFail=1
    return
  }
  Return %RS232_FileHandle%
}

RS232_Close(RS232_FileHandle)
{
  return DllCall("CloseHandle","ptr",RS232_FileHandle)
}

RS232_Write(RS232_FileHandle,Data,Data_Length)
{
  if RS232_FileHandle
  WF_Result := DllCall("WriteFile"
      ,"ptr" , RS232_FileHandle ;File Handle
      ,"ptr" , Data          ;Pointer to string to send
      ,"UInt", Data_Length    ;Data Length
      ,"ptr*", Bytes_Sent     ;Returns pointer to num bytes sent
      ,"Int" , "NULL")
}

RS232_Read(RS232_FileHandle,Data_Length,ByRef RS232_Bytes_Received,Byref D)
{
  Num_Bytes:=Data_Length*2
;  SetFormat,Integer,HEX
  VarSetCapacity(Data,Num_Bytes,0xff)
  if RS232_FileHandle
  Read_Result := DllCall("ReadFile"
      ,"ptr" , RS232_FileHandle   ; hFile
      ,"ptr"  , &Data             ; lpBuffer
      ,"Int"  , Num_Bytes        ; nNumberOfBytesToRead
      ,"ptr*", RS232_Bytes_Received   ; lpNumberOfBytesReceived
      ,"Int"  , 0)               ; lpOverlapped
  If (Read_Result <> 1)
  {
    RS232_Close(RS232_FileHandle)
    COMFail=1
    return
  }
  D:=Data
}
+ Скетч

#define button_1_Pin 2
#define button_2_Pin 3
#define button_3_Pin 4
#define button_4_Pin 5
#define button_5_Pin 6

byte buttons_State[5], recieve_data[5];

void setup()
{
  Serial.begin(9600);
  Serial.setTimeout(50);

  pinMode(button_1_Pin, INPUT_PULLUP);
  pinMode(button_2_Pin, INPUT_PULLUP);
  pinMode(button_3_Pin, INPUT_PULLUP);
  pinMode(button_4_Pin, INPUT_PULLUP);
  pinMode(button_5_Pin, INPUT_PULLUP);

  pinMode(13, OUTPUT);
}

void loop()
{
  if (Serial.available())
  {
    // получаем данные
    Serial.readBytes(recieve_data,5);
    while(Serial.available()) Serial.read();

    // обрабатываем полученные данные
    if (recieve_data[0])
      digitalWrite(13,HIGH);
    else
      digitalWrite(13,LOW);

    // готовим данные для отправки
    buttons_State[0] = digitalRead(button_1_Pin);
    buttons_State[1] = digitalRead(button_2_Pin);
    buttons_State[2] = digitalRead(button_3_Pin);
    buttons_State[3] = digitalRead(button_4_Pin);
    buttons_State[4] = digitalRead(button_5_Pin);

    // отправляем данные
    delayMicroseconds(500);
    Serial.write(buttons_State,5);
    delay(50);
  }
}
Win 7 x64
AHK v1.1.24.00
                       Справка тебе в помощь.

3

Re: AHK: Получения информации в ahk о подключенных к Arduino кнопках

Работает !!!, искреннее спасибо.