找回密码
 加入
搜索
查看: 580|回复: 3

[系统综合] 求助,关于串口操作,如何获取接受信息[搞定]

[复制链接]
发表于 2022-6-7 17:55:17 | 显示全部楼层 |阅读模式
本帖最后由 you74222 于 2022-6-8 15:12 编辑

代码来自  https://www.autoitx.com/forum.ph ... hlight=%B4%AE%BF%DA

进行的一点点修改

#include <GUIConstantsEx.au3>
#PRE_UseX64=n
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("串口1", 320, 60, 30, 30);图形界面
$Button1 = GUICtrlCreateButton("打开", 20, 12, 75, 36);按键1
$Button2 = GUICtrlCreateButton("关闭", 220, 12, 75, 36);按键1
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###


While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Button1
$com =4;设定要使用的端口4
$Comm=ObjCreate("MSCOMMLib.MSComm");调用串口控件
$Comm.CommPort=$com
$Comm.Handshaking=0  
$Comm.RThreshold=0
$Comm.Settings="9600,N,8,1" ;设置串口传输参数
$Comm.InputLen=0
$Comm.RTSEnable=False
$Comm.InputMode=1 ;使用二进制传输
$Comm.PortOpen=TRUE
$temp=Binary('0x98010101000000009B')
$Comm.Output=$temp ; ;调用com4发送98010101000000009B


Case $Button2

EndSwitch
WEnd;----------------------------------------------------


代码执行后,命令发出,怎么接受返回信息,在网上找了很多,没搞明白,这里只有发出指令,没有接受返回信息的方法。有没有哪个大哥指导一下
发表于 2022-6-7 23:15:01 | 显示全部楼层
本帖最后由 yohoboy 于 2022-6-7 23:20 编辑

https://www.autoitx.com/forum.ph ... hlight=%B4%AE%BF%DA
參考看看
#AutoIt3Wrapper_UseX64=n ;因commg.dll只能在32位元系統正常使用,加上這一句不編譯也可以運行,若編輯器和我的不一樣,添加一條類似的就可以正常運行

#include 'CommMG.au3'
Dim $dCommFlag = False ;串列打開狀態標誌

$a = _OpenCom(3) ;設定及打開串列

If @error Then Exit MsgBox(0, "", $a)

$b = _SendEvent("02  5F 5f 42 20 44 03") ;發送資料
If @error Then Exit MsgBox(0, "", $b)
$c = _ReceiveEvent() ;接收資料
If @error Then
MsgBox(0, "1", $c)
Else
        ConsoleWrite($c & @CRLF)
EndIf

_CloseCom() ;關閉串列
Exit

Func _OpenCom($sSetport, $sSetBaud = 9600, $sSetBits = 8, $sSetParity = 0, $sSetStop = 1, $sSetFlow = 0) ;打開串列
        ;~ $aSetport 串列號; $aSetBaud 串列傳輸速率; $aSetBits 數據位元; $aSetParity 校驗位NONE; $aSetStop 停止位; $aSetFlow 流控制NONE
        Local $aSportSetError ;存放錯誤返回值
        Local $aResOpen
        $aResOpen = _CommSetPort($sSetport, $aSportSetError, $sSetBaud, $sSetBits, $sSetParity, $sSetStop, $sSetFlow)
        If $aResOpen = 0 then
                $dCommFlag = False
                Return SetError(1, "", $aSportSetError)
        Else
                $dCommFlag = True ;串列打開成功
        EndIf
EndFunc

Func _SendEvent($sData) ;向串列發送“每位元組用空格間隔的”Hex資料
        If Not $dCommFlag Then Return SetError(1, "", "未打開串列")
        Local $SendText = StringStripWS($sData, 1 + 2)
        Local $SendTextArray = StringSplit($SendText, " ")
        For $i = 1 To $SendTextArray[0]
                _CommSendByte(Dec($SendTextArray[$i]))
        Next
EndFunc

Func _ReceiveEvent() ;從串列接收資料,返回“每位元組用空格間隔的”Hex資料
        If Not $dCommFlag Then Return SetError(1, "", "未打開串列")
        Local $aInstr, $aReturn, $aTimer
        $aTimer = TimerInit()
        Do
                $aInstr =  _commreadbyte()
                If $aInstr <> "" Then
                        $aReturn &= Hex($aInstr, 2) & " "
                EndIf
                If TimerDiff($aTimer) > 1000 And $aInstr = "" Then ;開始接收資料後1秒若無數據返回則認為資料接收已結束
                        ExitLoop
                EndIf
        Until 0
        Return $aReturn
EndFunc

Func _CloseCom() ;關閉串列
        If $dCommFlag Then _Commcloseport(true)
EndFunc

;此用法採用的是單字節發送和接收,應該還可以優化加快發送和接收資料的速度
;CommMG.au3發佈地址:https://www.autoitscript.com/forum/topic/128546-serial-port-com-port-udf/#comments
;CommMG.au3和commg.dll下載地址:https://www.mosaiccgl.co.uk/AutoItDownloads/confirm.php?get=COMMGvv2.zip


 楼主| 发表于 2022-6-8 08:18:29 | 显示全部楼层
yohoboy 发表于 2022-6-7 23:15
https://www.autoitx.com/forum.ph ... hlight=%B4%AE%BF%DA
參考看看

谢谢大哥,对您敢的膜拜如滔滔江水
 楼主| 发表于 2022-6-8 15:10:56 | 显示全部楼层
搞定
Sleep(1000)  ;原来是需要延时

        $buffer=$Comm.Input
        $sTemp=$buffer
        MsgBox(0,0,$sTemp)

$Comm.PortOpen=False
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-4-24 17:20 , Processed in 0.072614 second(s), 19 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表