找回密码
 加入
搜索
查看: 3573|回复: 1

[系统综合] 如何实现串口以16进制格式发送数据?

[复制链接]
发表于 2015-6-18 19:34:58 | 显示全部楼层 |阅读模式
那位大大帮忙看一下串口如下以16进制格式发送数据?
我的理解是:
1,发送的数据本身是16进制的。
2,将数据的每两位转换为其ASCII码,再以其字符形式发送。
看看我的理解是否有问题。
#NoTrayIcon
#Region ;**** 由 AccAu3Wrapper_GUI 创建指令 ****
#AccAu3Wrapper_Icon=..\ICON\Token\Token 2\7 W.ico
#AccAu3Wrapper_OutFile=D7BOXControl.exe
#AccAu3Wrapper_Change2CUI=Y
#AccAu3Wrapper_Res_Language=2052
#AccAu3Wrapper_Res_requestedExecutionLevel=None
#EndRegion ;**** 由 AccAu3Wrapper_GUI 创建指令 ****
#include <CommMG.au3>
;MsgBox(0,"yy",ChangeString("0201000100110103"))
FileInstall("commg.dll","commg.dll",1)
;MsgBox(0,"uu",ChangeString("010100030504"))
If $CMDLINE[0]==4 Then
        Global $sportSetError = ''
        Global $CMPort = $CMDLINE[1] ; Port
        Global $CmBoBaud = $CMDLINE[2] ; Baud
        Global $CmboDataBits = 8 ; Data Bits
        Global $CmBoParity = "none" ; Parity
        Global $CmBoStop = 1 ; Stop
        ; Global $setflow = 2 ; Flow
        Global $Command=$CMDLINE[3],$Timeout=$CMDLINE[4],$buffer="",$string="",$SendString=""
        _CommSetPort($CMPort, $sportSetError, $CmBoBaud, $CmboDataBits, $CmBoParity, $CmBoStop)
        If StringCompare($Command,"/P")==0 Then
                _CommSendstring(ChangeString("0201000100110103"))
                ;_CommSendstring("0201000100110103")
                If @error Then
                        ConsoleWrite("Send COM port Command:0201000100110103 error!!"&@CRLF)
                        Exit 1
                Else
                        $string="0201000100110103"
                EndIf
        ElseIf StringCompare($Command,"/F")==0 Then
                _CommSendstring(ChangeString("0201000000120103"))
                If @error Then
                        ConsoleWrite("Send COM port Command:0201000000120103 error!!"&@CRLF)
                        Exit 1
                Else
                        $string="0201000000120103"
                EndIf
        ElseIf StringCompare($Command,"/A")==0 Then
                _CommSendstring(ChangeString("0201000000060103"))
                If @error Then
                        ConsoleWrite("Send COM port Command:0201000000060103 error!!"&@CRLF)
                        Exit 1
                Else
                        $string="0201000000060103"
                EndIf
        Else
                _CommSendstring(ChangeString($Command))
                If @error Then
                        ConsoleWrite("Send COM port Command:"&$Command&" error!!"&@CRLF)
                        Exit 1
                Else
                        $string=$Command
                EndIf
        EndIf
        $start=TimerInit()
        While 1
                $buffer=_CommGetString()
                If @error Then
                        ConsoleWrite("Get Fixture PLC Exit string error!"&@CRLF)
                        Exit 1
                EndIf
                If $buffer<>"" Then
                        If StringCompare($buffer,"0201111111110103")==0 Then
                                ExitLoop
                                ConsoleWrite("UUT Send "&$string&" OK!"&@CRLF)
                                Exit 0
                        Else
                                ExitLoop
                                ConsoleWrite("UUT Send "&$string&" OK! but Fixture PLC Exit string ["&$buffer&"] error!"&@CRLF)
                                Exit 1
                        EndIf
                Else
                        $gap=TimerDiff($start)
                        If $gap/1000>=$Timeout Then
                                ExitLoop
                                ConsoleWrite("UUT wait for PLC Exit string timeout !!"&@CRLF)
                                Exit 1
                        Else
                                Sleep(100)
                        EndIf
                EndIf
        WEnd
Else
        helpmsg()
EndIf


Func helpmsg()
        Local $text
        ConsoleWrite("Usage: D7BOXControl.exe v1.0, use to control D7 test box behavior."&@CRLF&"  D7BOXControl.exe [COM Port] [Baud rate] [Command string] [Timeout]"&@CRLF&"        1> COM Port:1,2,3,4,5,6."&@CRLF&"        2> Baud rate:9600,115200."&@CRLF&"        3> Command String:/P -->Send PASS flag. /F: -->Send Fail Flag. /A: -->Open the Audio Module. More Command see the Control Command Table."&@CRLF&"        4> Time out:Timeout time second."&@CRLF&"Return Code: 0-->PASS. 1-->Fail."&@CRLF&"Example:"&@CRLF&"        D7BOXControl.exe 1 9600 /P 5"&@CRLF&"        D7BOXControl.exe 1 9600 /F 5"&@CRLF&"        D7BOXControl.exe 1 9600 /A 5"&@CRLF&'        D7BOXControl.exe 1 9600 "0201000100010103" 5'&@CRLF)
        $text = "Start        Set/Reset        Command        End        Action"&@CRLF _
                        &"0201        0001/0000        0001        0103        Open Toggle Clamp"&@CRLF _
                        &"0201        0001/0000        0002        0103        Close Toggle Clamp"&@CRLF _
                        &"0201        0001/0000        0003        0103        Pull Cylinder Slantly"&@CRLF _
                        &"0201        0001/0000        0004        0103        Push Chassis Front Wall"&@CRLF _
                        &"0201        0001/0000        0005        0103        Left USB"&@CRLF _
                        &"0201        0001/0000        0006        0103        Audio Module"&@CRLF _
                        &"0201        0001/0000        0007        0103        Power Switch"&@CRLF _
                        &"0201        0001/0000        0008        0103        SATA Cable"&@CRLF _
                        &"0201        0001/0000        0009        0103        Chassis Positioning"&@CRLF _
                        &"0201        0001/0000        000C        0103        Buzzing"&@CRLF _
                        &"0201        0001/0000        000D        0103        Red Light"&@CRLF _
                        &"0201        0001/0000        000E        0103        Yellow Light"&@CRLF _
                        &"0201        0001/0000        000F        0103        Green Light"&@CRLF _
                        &"0201        0001/0000        00010        0103        Shutdown"&@CRLF _
                        &"0201        0001                0011        0103        Test PASS"&@CRLF _
                        &"0201        0000                0012        0103        Test FAIL"&@CRLF _
                        &"0201        1111                1111        0103        PLC Return Code"&@CRLF
        ConsoleWrite("=============D7 Function Box Control Command Table=============="&@CRLF)
        ConsoleWrite($text)
        ConsoleWrite("================================================================"&@CRLF)
        Exit 1
EndFunc
        
Func GetModemComPort($ModemName)
        Local $i, $RegName
        Local $KeyName="HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM"
        For $i= 1 to 10
                $RegName=RegEnumVal($KeyName,$i)
                If @error <> 0 then
                        ExitLoop
                ElseIf StringInStr($RegName,$ModemName) <> 0 Then
                        Return StringTrimLeft(RegRead($KeyName,$RegName), 3)
                EndIf
        Next
        Return -1
EndFunc

Func ChangeString($sString)
        Local $StringtoArry,$eString=""
        $StringtoArry=StringToASCIIArray($sString)
        ;MsgBox(0,"UBound($StringtoArry)",UBound($StringtoArry))
        $eString=Chr(Hex("0x"&Chr($StringtoArry[0])&Chr($StringtoArry[1]),2))&Chr(Hex("0x"&Chr($StringtoArry[2])&Chr($StringtoArry[3]),2))&Chr(Hex("0x"&Chr($StringtoArry[4])&Chr($StringtoArry[5]),2))&Chr(Hex("0x"&Chr($StringtoArry[6])&Chr($StringtoArry[7]),2))
        ;ConsoleWrite("$eString="&$eString&@CRLF)
        Return $eString
EndFunc
发表于 2015-6-20 04:24:53 | 显示全部楼层
取每两个字节 转成 二进制后发送
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-4-30 02:00 , Processed in 0.076662 second(s), 23 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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