首页 > 其他分享 >pc从arduino获取传感器的数据

pc从arduino获取传感器的数据

时间:2022-10-14 09:33:47浏览次数:35  
标签:arduino HEX Bytes pc 传感器 ###### SERIAL Data Port

主代码,要log4ahk库

#include <log4ahk>
#NoEnv
SendMode Input
SetWorkingDir %A_ScriptDir%
ARDUINO_Port     = COM3
ARDUINO_Baud     = 115200
ARDUINO_Parity   = N
ARDUINO_Data     = 8
ARDUINO_Stop     = 1
arduino_setup(start_polling_serial:=false)
i := 1
loop{
    data = % arduino_read()
    
    If (data != ""){    
        Str := data
        DllCall("crypt32\CryptStringToBinaryW", "ptr", &str, "uint", StrLen(str), "uint", 0x4, "ptr", 0, "uint*", bytes, "ptr", 0, "ptr", 0)
        VarSetCapacity(buf, bytes) 
        DllCall("crypt32\CryptStringToBinaryW", "ptr", &str, "uint", StrLen(str), "uint", 0x4, "ptr", &buf, "uint*", bytes, "ptr", 0, "ptr", 0)
        log.info(StrGet(&buf, "utf-8"))  
        }
    }
Return
OnExit:
arduino_close()
; ===============================================================================================================================
#include %A_ScriptDir%\Arduino.ahk

Arduino.ahk

#include %A_ScriptDir%\Serial.ahk
; Arduino AHK Library
arduino_setup(start_polling_serial=true,ping_device=true){
	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)
	if ping_device {
		arduino_send("")
	}
	if (arduino_poll_serial_enabled := start_polling_serial) {
		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, arduino_poll_serial
	; turn off timer if it is running
	SetTimer, arduino_poll_serial, Off
	; wait a bit for timer to finished if it happened
	Sleep, 100
	Serial_Close(ARDUINO_Handle)
}
arduino_poll_serial:
	if (IsFunc(f:="OnSerialData")&&(arduino_poll_serial_enabled == true)){
		SerialData := arduino_read_raw()
		if SerialData {
				%f%(SerialData)
			}
		SetTimer, arduino_poll_serial, -1
	}else{
		MsgBox, OnSerialData function not defined. 
		SetTimer, arduino_poll_serial, Off
		arduino_poll_serial_enabled = false
		}
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
	OldIntegerFormat := A_FormatInteger
  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%
   	SetFormat, Integer, %OldIntegerFormat%
    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
  	OldIntegerFormat := A_FormatInteger
  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
  SetFormat, Integer, %OldIntegerFormat%
  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
    	OldIntegerFormat := A_FormatInteger
  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
	SetFormat, Integer, %OldIntegerFormat%
  
  Return Data
}

arduino代码,用来模拟传感器输出

int pos = 100;
 
void setup() 
{ 
  delay(1000);
  Serial.begin(115200);
} 
 
void loop() 
{ 
  if (Serial.available() > 0) {
  Serial.print(pos);  //模拟传感器输出
  delay(1500);
  }
}

 

标签:arduino,HEX,Bytes,pc,传感器,######,SERIAL,Data,Port
From: https://www.cnblogs.com/ff888/p/16790550.html

相关文章

  • 竞赛获奖系统解读:VPC2022语音隐私保护赛NWPU-ASLP说话人匿名化系统
    https://blog.csdn.net/weixin_48827824/article/details/127207248?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522166567862316782412578074%2522%252C%2522......
  • http请求方式-OkHttpClient
    http请求方式-OkHttpClientimportcom.example.core.mydemo.http.OrderReqVO;importokhttp3.*;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importor......
  • http请求方式-CloseableHttpClient
    http请求方式-CloseableHttpClientimportcom.alibaba.fastjson.JSON;importcom.alibaba.fastjson.JSONObject;importcom.example.core.mydemo.http.OrderReqVO;imp......
  • http请求方式-HttpClient
    http请求方式-HttpClientimportcom.alibaba.fastjson.JSON;importcom.alibaba.fastjson.JSONObject;importcom.example.core.mydemo.http.OrderReqVO;importorg.a......
  • 压力传感器与压力变送器, “傻傻”分不清楚
    压力传感器,压力变送器认为是同一种产品吗?二者区别是什么呢?今天这篇文章为您分享压力传感器与压力变送器的区别。压力传感器是一种能感受压力,并按照一定的规律将压力信号转换......
  • 光电传感器问题
    光电传感器问题主要是发射光长时间通电会导致发射光衰减。导致输出有影响。还有关于使用环境的变化也会造成光电传感器的异常。输出端,有两种输出模式:常开、常闭。实际应用......
  • 【音视频连载-008】基础学习篇-SDL 播放 PCM 音频文件(下)
    公众号回复:OpenGL,领取学习资源大礼包音视频学习入门技术文章连载:​​技术开发故事会连载​​​​【音视频连载-001】基础学习篇-SDL介绍以及工程配置​​​​【音视频连载-......
  • PLC采集网关实现三菱 PLC FX2N 接入OPC UA云平台
    COM口采集三菱PLCFX2N的配置工业智能网关一共有四个COM接口,4个COM口的配置内容一样,COM1固定为RS232,COM2、COM3和COM4是RS232/RS485可选串口(默认为RS485)。因三菱PLCFX......
  • 腾讯地图PC端下线
    我是卢松松,点点上面的头像,欢迎关注我哦!腾讯地图PC端将于11月11日停止服务,移动用户可以下载腾讯地图App使用。腾讯2021年宣布收购搜狗。此前搜狗地图宣布2022年5月15日下线,关......
  • 自动消毒液机要实现缺液提醒,用哪种传感器合适?
    自动消毒液机与传统的手动按压液体消毒机相比,用自动出液器只需将手放在消毒器下就能自动感应出液。相比之下更方便、卫生、环保、低碳,使用寿命长,在使用过程中可以有效地避免......