<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title><![CDATA[Серый форум &mdash; AHK: Получения информации в ahk о подключенных к Arduino кнопках]]></title>
		<link>https://forum.script-coding.com/viewtopic.php?id=13247</link>
		<atom:link href="https://forum.script-coding.com/extern.php?action=feed&amp;tid=13247&amp;type=rss" rel="self" type="application/rss+xml" />
		<description><![CDATA[Недавние сообщения в теме «AHK: Получения информации в ahk о подключенных к Arduino кнопках».]]></description>
		<lastBuildDate>Thu, 30 Nov 2017 11:40:59 +0000</lastBuildDate>
		<generator>PunBB</generator>
		<item>
			<title><![CDATA[Re: AHK: Получения информации в ahk о подключенных к Arduino кнопках]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=121818#p121818</link>
			<description><![CDATA[<p>Работает !!!, искреннее спасибо.</p>]]></description>
			<author><![CDATA[null@example.com (Invalid)]]></author>
			<pubDate>Thu, 30 Nov 2017 11:40:59 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=121818#p121818</guid>
		</item>
		<item>
			<title><![CDATA[Re: AHK: Получения информации в ahk о подключенных к Arduino кнопках]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=121778#p121778</link>
			<description><![CDATA[<p><a href="http://forum.script-coding.com/viewtopic.php?id=12662">AHK: Работа с COM портом для микроконтроллеров</a></p><div class="codebox"><pre><code>#SingleInstance,Force
CoordMode,ToolTip
SetBatchLines,-1
SetWorkingDir,%A_ScriptDir%
OnExit,EXIT

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

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




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




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

loop ; основной цикл
{
  sleep,10
  Loop_Time:=oldLoop_Time!=&quot;&quot; ? 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:=&quot;COM2&quot;)

  }

  if (!timer:=timer&gt;1000 ? 0 : timer=&quot;&quot; ? 1 : timer+Loop_Time)
    b_0:=!b_0    ; 1 байт

  if RS232_FileHandle ; если хендл есть, читаем и пишем в порт
  {
    if (!tie:=tie&gt;tie_interval ? 0 : tie=&quot;&quot; ? 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,&quot;Uchar&quot;)          ; 1 байт
      NumPut(b_1,Out_Data,1,&quot;Uchar&quot;)          ; 1 байт
      NumPut(b_2,Out_Data,2,&quot;Uchar&quot;)          ; 1 байт
      NumPut(b_3,Out_Data,3,&quot;Uint&quot;)           ; 1 байт
      NumPut(b_4,Out_Data,4,&quot;Uchar&quot;)          ; 1 байт

      ; пишем данные в порт
      RS232_Write(RS232_FileHandle,&amp;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,&quot;uchar&quot;)		; 1 байт
        button_2_Pin:=numget(In_Data,1,&quot;uchar&quot;)		; 1 байт
        button_3_Pin:=numget(In_Data,2,&quot;uchar&quot;)		; 1 байт
        button_4_Pin:=numget(In_Data,3,&quot;uchar&quot;)		; 1 байт
        button_5_Pin:=numget(In_Data,4,&quot;uchar&quot;)		; 1 байт
      }
      else ; если не одинакова - увеличиваем интервал между запросами
      {
        tie_interval+=1
        if tie_interval&gt;1000 ; максимально возможный интервал, ms
          tie_interval=100
      }

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

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

      ; выводим на экран
      ;tooltip,% &quot;button_1=&quot; button_1_Pin &quot;`nbutton_2=&quot; button_2_Pin &quot;`nbutton_3=&quot; button_3_Pin &quot;`nbutton_4=&quot; button_4_Pin &quot;`nbutton_5=&quot; 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 &quot;|&quot;
    num++
  }
  loop,parse,com,|
  {
    if !A_loopfield
      continue
    COMPort:=A_loopfield
    Loop,HKLM,SYSTEM\CurrentControlSet\Enum,1,1
    {
      if (A_LoopRegName=&quot;PortName&quot;)
      {
        RegRead,Outputvar
        if (Outputvar=COMPort)
        {
          RegRead,FriendlyName,% A_LoopRegKey,% RegExReplace(A_LoopRegSubKey, &quot;(.*)\\Device Parameters&quot;, &quot;$1&quot;),FriendlyName
          if InStr(FriendlyName,name)
            return COMPort
        }
      }
    }
  }
}

Get_RS232_FileHandle(RS232_Port,RS232_Baud=9600,RS232_Parity=&quot;N&quot;,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&#039;t work.
  StringSplit, RS232_Temp, RS232_Settings, `:
  RS232_Temp1_Len := StrLen(RS232_Temp1)  ;For COM Ports &gt; 9 \\.\ needs to prepended to the COM Port name.
  If (RS232_Temp1_Len &gt; 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(&quot;BuildCommDCB&quot;
      ,&quot;str&quot; , RS232_Settings ;lpDef
      ,&quot;ptr&quot;, &amp;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(&quot;CreateFile&quot;
      ,&quot;Str&quot; , RS232_COM    ;File Name
      ,&quot;UInt&quot;, 0xC0000000   ;Desired Access
      ,&quot;UInt&quot;, 3            ;Safe Mode
      ,&quot;UInt&quot;, 0            ;Security Attributes
      ,&quot;UInt&quot;, 3            ;Creation Disposition
      ,&quot;UInt&quot;, 0            ;Flags And Attributes
      ,&quot;ptr&quot; , 0            ;Template File
      ,&quot;Cdecl Int&quot;)

  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(&quot;SetCommState&quot;
      ,&quot;ptr&quot;, RS232_FileHandle ;File Handle
      ,&quot;ptr&quot;, &amp;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, &quot;UInt&quot;)
  NumPut(ReadTotalTimeoutMultiplier,  Data,  4, &quot;UInt&quot;)
  NumPut(ReadTotalTimeoutConstant,    Data,  8, &quot;UInt&quot;)
  NumPut(WriteTotalTimeoutMultiplier, Data, 12, &quot;UInt&quot;)
  NumPut(WriteTotalTimeoutConstant,   Data, 16, &quot;UInt&quot;)

  ;###### Set the RS232 COM Timeouts ######
  SCT_result := DllCall(&quot;SetCommTimeouts&quot;
    ,&quot;ptr&quot;, RS232_FileHandle ;File Handle
    ,&quot;ptr&quot;, &amp;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(&quot;CloseHandle&quot;,&quot;ptr&quot;,RS232_FileHandle)
}

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

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(&quot;ReadFile&quot;
      ,&quot;ptr&quot; , RS232_FileHandle   ; hFile
      ,&quot;ptr&quot;  , &amp;Data             ; lpBuffer
      ,&quot;Int&quot;  , Num_Bytes        ; nNumberOfBytesToRead
      ,&quot;ptr*&quot;, RS232_Bytes_Received   ; lpNumberOfBytesReceived
      ,&quot;Int&quot;  , 0)               ; lpOverlapped
  If (Read_Result &lt;&gt; 1)
  {
    RS232_Close(RS232_FileHandle)
    COMFail=1
    return
  }
  D:=Data
}</code></pre></div><div class="fancy_spoiler_switcher"><div class="fancy_spoiler_switcher_header"><strong>+</strong>&nbsp;Скетч</div><div class="fancy_spoiler"><div class="codebox"><pre><code>
#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);
  }
}
</code></pre></div></div></div>]]></description>
			<author><![CDATA[null@example.com (Alectric)]]></author>
			<pubDate>Wed, 29 Nov 2017 08:05:32 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=121778#p121778</guid>
		</item>
		<item>
			<title><![CDATA[AHK: Получения информации в ahk о подключенных к Arduino кнопках]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=121687#p121687</link>
			<description><![CDATA[<p>Доброго времени суток, на англо-язычных ресурсах нашел следующее</p><div class="codebox"><pre><code>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
</code></pre></div><p>Arduino.ahk<br /></p><div class="fancy_spoiler_switcher"><div class="fancy_spoiler_switcher_header" data-lang-open="открыть спойлер" data-lang-close="скрыть спойлер"><strong>+</strong>&nbsp;открыть спойлер</div><div class="fancy_spoiler"><div class="codebox"><pre><code>#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(&quot;&quot;)
	;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, &quot;0xFF&quot;, Bytes_Received)
}

arduino_read_raw(){
	global ARDUINO_Handle
	return Serial_Read_Raw(ARDUINO_Handle, &quot;0xFF&quot;, &quot;raw&quot;,Bytes_Received)
}

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

arduino_poll_serial:
	if IsFunc(f:=&quot;OnSerialData&quot;){
	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
</code></pre></div></div></div><p>Serial.ahk<br /></p><div class="fancy_spoiler_switcher"><div class="fancy_spoiler_switcher_header" data-lang-open="открыть спойлер" data-lang-close="скрыть спойлер"><strong>+</strong>&nbsp;открыть спойлер</div><div class="fancy_spoiler"><div class="codebox"><pre><code>; 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(&quot;BuildCommDCB&quot;
       ,&quot;str&quot; , SERIAL_Settings ;lpDef
       ,&quot;UInt&quot;, &amp;DCB)        ;lpDCB
  If (BCD_Result &lt;&gt; 1){
    error := DllCall(&quot;GetLastError&quot;)
    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 &gt; 9 \\.\ needs to prepended to the COM Port name.
  If (SERIAL_Port_Temp1_Len &gt; 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 &quot;Extract/Format the COM Port Number&quot; section above.
  SERIAL_FileHandle := DllCall(&quot;CreateFile&quot;
       ,&quot;Str&quot; , SERIAL_Port     ;File Name
       ,&quot;UInt&quot;, 0xC0000000   ;Desired Access
       ,&quot;UInt&quot;, 3            ;Safe Mode
       ,&quot;UInt&quot;, 0            ;Security Attributes
       ,&quot;UInt&quot;, 3            ;Creation Disposition
       ,&quot;UInt&quot;, 0            ;Flags And Attributes
       ,&quot;UInt&quot;, 0            ;Template File
       ,&quot;Cdecl Int&quot;)
  If (SERIAL_FileHandle &lt; 1){
    error := DllCall(&quot;GetLastError&quot;)
    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(&quot;SetCommState&quot;
       ,&quot;UInt&quot;, SERIAL_FileHandle ;File Handle
       ,&quot;UInt&quot;, &amp;DCB)          ;Pointer to DCB structure
  If (SCS_Result &lt;&gt; 1){
    error := DllCall(&quot;GetLastError&quot;)
    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, &quot;UInt&quot;)
  NumPut(ReadTotalTimeoutMultiplier,  Data,  4, &quot;UInt&quot;)
  NumPut(ReadTotalTimeoutConstant,    Data,  8, &quot;UInt&quot;)
  NumPut(WriteTotalTimeoutMultiplier, Data, 12, &quot;UInt&quot;)
  NumPut(WriteTotalTimeoutConstant,   Data, 16, &quot;UInt&quot;)

  ;###### Set the COM Timeouts ######
  SCT_result := DllCall(&quot;SetCommTimeouts&quot;
     ,&quot;UInt&quot;, SERIAL_FileHandle ;File Handle
     ,&quot;UInt&quot;, &amp;Data)         ;Pointer to the data structure
  If (SCT_result &lt;&gt; 1){
    error := DllCall(&quot;GetLastError&quot;)
    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(&quot;CloseHandle&quot;, &quot;UInt&quot;, SERIAL_FileHandle)
  If (CH_result &lt;&gt; 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) , &quot;UChar&quot;)
    ;msgbox, %i%
    i++
  }
  ;msgbox, Data string=%Data%

  ;###### Write the data to the COM Port ######
  WF_Result := DllCall(&quot;WriteFile&quot;
       ,&quot;UInt&quot; , SERIAL_FileHandle ;File Handle
       ,&quot;UInt&quot; , &amp;Data          ;Pointer to string to send
       ,&quot;UInt&quot; , Data_Length    ;Data Length
       ,&quot;UInt*&quot;, Bytes_Sent     ;Returns pointer to num bytes sent
       ,&quot;Int&quot;  , &quot;NULL&quot;)
  If (WF_Result &lt;&gt; 1 or Bytes_Sent &lt;&gt; 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 = &quot;&quot;){
  ;Global SERIAL_FileHandle
  ;Global SERIAL_Port
  ;Global Bytes_Received
  SetFormat, Integer, HEX

  ;Set the Data buffer size, prefill with 0x55 = ASCII character &quot;U&quot;
  ;VarSetCapacity won&#039;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(&quot;ReadFile&quot;
       ,&quot;UInt&quot; , SERIAL_FileHandle   ; hFile
       ,&quot;Str&quot;  , Data             ; lpBuffer
       ,&quot;Int&quot;  , Num_Bytes        ; nNumberOfBytesToRead
       ,&quot;UInt*&quot;, Bytes_Received   ; lpNumberOfBytesReceived
       ,&quot;Int&quot;  , 0)               ; lpOverlapped
  ;MsgBox, Read_Result=%Read_Result% `nBR=%Bytes_Received% ,`nData=%Data%
  If (Read_Result &lt;&gt; 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&#039;t handle NULL (0x00) characters very nicely.
  ;Quote from AHK documentation under DllCall:
  ;     &quot;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
  ;     (&amp; and *), as well as DllCall itself.&quot;
  i = 0
  Data_HEX =
  Loop %Bytes_Received% {
    ;First byte into the Rx FIFO ends up at position 0

    Data_HEX_Temp := NumGet(Data, i, &quot;UChar&quot;) ;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 &quot;0&#039;
    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 = &quot;&quot;,byref Bytes_Received = &quot;&quot;){
  ;Global SERIAL_FileHandle
  ;Global SERIAL_Port
  ;Global Bytes_Received
  SetFormat, Integer, HEX

  ;Set the Data buffer size, prefill with 0x55 = ASCII character &quot;U&quot;
  ;VarSetCapacity won&#039;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(&quot;ReadFile&quot;
       ,&quot;UInt&quot; , SERIAL_FileHandle   ; hFile
       ,&quot;Str&quot;  , Data             ; lpBuffer
       ,&quot;Int&quot;  , Num_Bytes        ; nNumberOfBytesToRead
       ,&quot;UInt*&quot;, Bytes_Received   ; lpNumberOfBytesReceived
       ,&quot;Int&quot;  , 0)               ; lpOverlapped
  ;MsgBox, Read_Result=%Read_Result% `nBR=%Bytes_Received% ,`nData=%Data%
  If (Read_Result &lt;&gt; 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 &#039;raw&#039; response
  If (mode = &quot;raw&quot;)
    Return Data

  ;###### Format the received data ######
  ;This loop is necessary because AHK doesn&#039;t handle NULL (0x00) characters very nicely.
  ;Quote from AHK documentation under DllCall:
  ;     &quot;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
  ;     (&amp; and *), as well as DllCall itself.&quot;
  i = 0
  Data_HEX =
  Loop %Bytes_Received% 
  {
    ;First byte into the Rx FIFO ends up at position 0

    Data_HEX_Temp := NumGet(Data, i, &quot;UChar&quot;) ;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 &quot;0&#039;
    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

}</code></pre></div></div></div><p>sketch_arduino<br /></p><div class="fancy_spoiler_switcher"><div class="fancy_spoiler_switcher_header" data-lang-open="открыть спойлер" data-lang-close="скрыть спойлер"><strong>+</strong>&nbsp;открыть спойлер</div><div class="fancy_spoiler"><div class="codebox"><pre><code>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);
 }</code></pre></div></div></div><p>Одна кнопка работает хорошо, D нажата U отпущена, но стоит добавить вторую.<br />sketch_arduino</p><div class="fancy_spoiler_switcher"><div class="fancy_spoiler_switcher_header" data-lang-open="открыть спойлер" data-lang-close="скрыть спойлер"><strong>+</strong>&nbsp;открыть спойлер</div><div class="fancy_spoiler"><div class="codebox"><pre><code>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(&quot;b1&quot;);Serial.print(b1);
      Serial.print(&quot;b2&quot;);Serial.print(b2);
 }</code></pre></div></div></div><p>В переменную %vag% начинают выводится символы не понятные, иероглифы, причем в Arduino IDE монитор порта все верно иероглифов нет, прошу помощи что изменить в скрипте или sketch чтобы ahk отображала информацию о нескольких кнопках.</p>]]></description>
			<author><![CDATA[null@example.com (Invalid)]]></author>
			<pubDate>Mon, 27 Nov 2017 11:29:46 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=121687#p121687</guid>
		</item>
	</channel>
</rss>
