找回密码
 加入
搜索
查看: 10499|回复: 10

[系统综合] 串口发送十六进制问题

  [复制链接]
发表于 2012-4-10 19:31:31 | 显示全部楼层 |阅读模式
如何像串口调试助手一样,发送的是FF,接受到的也是FF,而不是63 63
发表于 2012-4-10 20:39:35 | 显示全部楼层
试试 _CommSendString(Chr(63)&Chr(63))
 楼主| 发表于 2012-4-10 20:50:55 | 显示全部楼层
回复 2# xms77


   不行
COM2 Write: 2(Byte)3F 1E
发表于 2012-4-10 22:19:31 | 显示全部楼层
本帖最后由 xms77 于 2012-4-10 22:20 编辑

回复 3# lin0308
发送把发送的字符转换成二进制_CommSendString(StringtoBinary("FF")),接受的内容用BinaryToString()试试
 楼主| 发表于 2012-4-10 22:36:25 | 显示全部楼层
回复 2# xms77


    发送给单机片的..
发表于 2012-4-11 08:51:20 | 显示全部楼层
回复 5# lin0308
我也做过发送给单片机的程序,测试成功,系统环境xp sp3,以下代码给你借鉴一下,我也是借鉴别人的代码改的,在我的机器上通讯成功。
#Region ;**** 参数创建于 ACNWrapper_GUI ****
#AutoIt3Wrapper_icon=C:\WINDOWS\system32\SHELL32.dll|-49
#EndRegion ;**** 参数创建于 ACNWrapper_GUI ****
;Example program showing how to use some of the commMg3.au3 UDF functions
;this example is a very simple terminal
;Version 2 26th July 2006
;changes-
;change flow control checkbox to combo and add NONE
;correct error in call to _CommSetPort - stop bits were missing which meant th eflow control was used for stop bits

#include <GUIConstants.au3>
#include 'CommMG.au3';or if you save the commMg.dll in the @scripdir use #include @SciptDir & '\commmg.dll'
#include <GuiEdit.au3>
#include <GuiComboBox.au3>
#include <windowsconstants.au3>
#include <buttonconstants.au3>

Opt("WINTITLEMATCHMODE", 3)
OnAutoItExitRegister("alldone")
HotKeySet("{ESC}", "alldone")

$result = '';used for any returned error message setting port
Const $settitle = "COMMG Example - set Port", $maintitle = "COMMG Example"
$setflow = 2;default to no flow control
Dim $FlowType[3] = ["XOnXoff", "Hardware (RTS, CTS)", "NONE"]
#Region main program

#Region ### START Koda GUI section ### Form=d:\my documents\miscdelphi\commg\ExampleComm.kxf
$Form2 = GUICreate("COMMG Example", 473, 349, 339, 333); BitOR($WS_MAXIMIZEBOX, $WS_MINIMIZEBOX, $WS_SIZEBOX, $WS_THICKFRAME, $WS_SYSMENU, $WS_CAPTION, $WS_TILEDWINDOW, $WS_POPUP, $WS_POPUPWINDOW, $WS_GROUP, $WS_TABSTOP, $WS_BORDER, $WS_CLIPSIBLINGS))
$Edit1 = GUICtrlCreateEdit("", 10, 25, 449, 223, BitOR($ES_AUTOVSCROLL, $ES_AUTOHSCROLL, $ES_READONLY, $ES_WANTRETURN, $WS_HSCROLL, $WS_VSCROLL))
$BtnSend = GUICtrlCreateButton("Send", 400, 273, 53, 30, $BS_FLAT)
$Input1 = GUICtrlCreateInput("", 18, 279, 361, 21)
$Checkbox1 = GUICtrlCreateCheckbox("Add LF to incomming CR", 200, 4, 170, 17)
GUICtrlSetState(-1, $GUI_CHECKED)
GUICtrlSetResizing(-1, $GUI_DOCKAUTO)
$Label11 = GUICtrlCreateLabel("Text to send:", 20, 261, 100, 17)
$BtnSetPort = GUICtrlCreateButton("Set Port", 16, 312, 73, 30, $BS_FLAT)
$BtnCancel1 = GUICtrlCreateButton("Cancel", 100, 312, 73, 30, $BS_FLAT)
$Label21 = GUICtrlCreateLabel("Received text:", 12, 8, 90 ,17)
$Label31 = GUICtrlCreateLabel("commg.dll version unknown", 272, 328, 135, 17)
$checkbox_2HD = GUICtrlCreateCheckbox("2HD",130,257,50,17)
$checkbox_4HD = GUICtrlCreateCheckbox("4HD",200,257,50,17)
GUICtrlSetColor(-1, 0x008080)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
WinSetTitle($Form2, "", $maintitle & "  UDF = " & _CommGetVersion(1))

While setport(0) = -1
    If MsgBox(4, 'Port not set', 'Do you want to quite the program?') = 6 Then Exit
WEnd



#cs
    _CommSwitch(2)
    MsgBox(0,'set com11 res = ',_CommSetport(11,$result,9600,8,'none',1,1))
    MsgBox(0,'set port 11 gives',$result)
    _CommSendString("It's Monday" & @CR)
    _CommSwitch(1)
    AdlibEnable("port11",500)
#ce

Global $fPortOpen = False
Global $hDll
Global $DLLNAME = 'commg.dll'
;GUISwitch($Form2)
;ConsoleWrite("stage 1" & @CRLF)
GUICtrlSetData($Label31, 'using ' & _CommGetVersion(1))
;ConsoleWrite("stage 2" & @CRLF)
Events()

GUICtrlSetState($Edit1, $GUI_FOCUS)

While 1

    ;gets characters received returning when one of these conditions is met:
    ;receive @CR, received 20 characters or 200ms has elapsed
    $instr = _CommGetString()

    If $instr <> '' Then;if we got something

        If GUICtrlRead($Checkbox1) = $GUI_CHECKED Then $instr = StringReplace($instr,@CR,@CRLF)
                GUICtrlSetData($Edit1, $instr,1)

    EndIf

WEnd

Alldone()


Func port11()
    ;MsgBox(0,'now set to channel',_CommSwitch(2))
    _commSwitch(2)
    $s2 = "1 2 3 4";_CommGetString()
    ConsoleWrite("comm1 gets " & $s2 & @CRLF)
    _CommSendString($s2)
    _CommSwitch(1)

EndFunc   ;==>port11

#EndRegion main program
Func Events()
    Opt("GUIOnEventMode", 1)
    GUISetOnEvent($GUI_EVENT_CLOSE, "justgo")
    GUICtrlSetOnEvent($BtnSend, "SendEvent")
    GUICtrlSetOnEvent($BtnSetPort, "SetPortEvent")
        GUICtrlSetOnEvent($BtnCancel1, "Cancel")
EndFunc   ;==>Events

Func SetPortEvent()
    setport();needed because a parameter is optional for setport so we can't use "setport" for the event
    GUICtrlSetState($Edit1, $GUI_FOCUS)
EndFunc   ;==>SetPortEvent

Func justgo()
    Exit
EndFunc   ;==>justgo

Func Cancel()
        _CommSendString(Chr(27)&@CR)
EndFunc   ;==>cancel

Func SendEvent();send the text in the inputand append CR
        ;$input_data = GUICtrlRead($Input1)
        $Check_2HD = GUICtrlRead($checkbox_2HD)        
        $Check_4HD = GUICtrlRead($checkbox_4HD)
        ;If $Check_2HD = $GUI_UNCHECKED And $Check_4HD = $GUI_UNCHECKED Then MsgBox(0,"Warning","You Must Select one, 2HD or 4HD!")
        If $Check_2HD = $GUI_UNCHECKED And $Check_4HD = $GUI_UNCHECKED Then _CommSendstring(GUICtrlRead($Input1) & @CR)
        If $Check_2HD = $GUI_CHECKED And $Check_4HD = $GUI_UNCHECKED Then _CommSendString(Chr(1)&Chr(2)&@CR)
        If $Check_4HD = $GUI_CHECKED And $Check_2HD = $GUI_UNCHECKED Then _CommSendString(Chr(1)&Chr(4)&@CR)        
    If $Check_2HD = $GUI_CHECKED And $Check_4HD = $GUI_CHECKED Then MsgBox(0,"Warning","You only can select one!")           
    GUICtrlSetData($Input1, '');clear the input
    ;GUICtrlSetState($edit1,$GUI_FOCUS);sets the caret back in the terminal screen
EndFunc   ;==>SendEvent


Func AllDone()
    ;MsgBox(0,'will close ports','')
    _Commcloseport()
    ;MsgBox(0,'port closed','')
    Exit
EndFunc   ;==>AllDone


; Function SetPort($mode=1)
; Creates a form for the port settings
;Parameter $mode sets the return value depending on whether the port was set
;Returns  0 if $mode <> 1
;          -1 If` the port not set and $mode is 1
Func SetPort($mode = 1);if $mode = 1 then returns -1 if settings not made

    Opt("GUIOnEventMode", 0);keep events for $Form2, use GuiGetMsg for $Form3

    #Region ### START Koda GUI section ### Form=d:\my documents\miscdelphi\commg\examplecommsetport.kxf
    $Form3 = GUICreate("COMMG Example - set Port", 422, 279, 329, 268, BitOR($WS_MINIMIZEBOX, $WS_CAPTION, $WS_POPUP, $WS_GROUP, $WS_BORDER, $WS_CLIPSIBLINGS, $DS_MODALFRAME), BitOR($WS_EX_TOPMOST, $WS_EX_WINDOWEDGE))
    $Group1 = GUICtrlCreateGroup("Set COM Port", 18, 8, 288, 252)
    $CmboPortsAvailable = GUICtrlCreateCombo("", 127, 28, 145, 25)
    $CmBoBaud = GUICtrlCreateCombo("9600", 127, 66, 145, 25, BitOR($CBS_DROPDOWN, $CBS_AUTOHSCROLL, $CBS_SORT, $WS_VSCROLL))
    GUICtrlSetData(-1, "10400|110|115200|1200|128000|14400|150|15625|1800|19200|2000|2400|256000|28800|3600|38400|4800|50|56000|57600|600|7200|75")
    $CmBoStop = GUICtrlCreateCombo("1", 127, 141, 145, 25)
    GUICtrlSetData(-1, "1|2|1.5")
    $CmBoParity = GUICtrlCreateCombo("none", 127, 178, 145, 25)
    GUICtrlSetData(-1, "odd|even|none")
    $Label2 = GUICtrlCreateLabel("Port", 94, 32, 23, 17)
    $Label3 = GUICtrlCreateLabel("baud", 89, 70, 28, 17)
    $Label4 = GUICtrlCreateLabel("No. Stop bits", 45, 145, 65, 17)
    $Label5 = GUICtrlCreateLabel("parity", 88, 182, 29, 17)
    $CmboDataBits = GUICtrlCreateCombo("8", 127, 103, 145, 25)
    GUICtrlSetData(-1, "7|8")
    $Label7 = GUICtrlCreateLabel("No. of Data Bits", 38, 107, 79, 17)
    $ComboFlow = GUICtrlCreateCombo("NONE", 127, 216, 145, 25)
    GUICtrlSetData(-1, "NONE|XOnXOff|Hardware (RTS, CTS)")
    $Label1 = GUICtrlCreateLabel("flow control", 59, 220, 58, 17)
    GUICtrlCreateGroup("", -99, -99, 1, 1)
    $BtnApply = GUICtrlCreateButton("Apply", 315, 95, 75, 35, $BS_FLAT)
    GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
    $BtnCancel = GUICtrlCreateButton("Cancel", 316, 147, 76, 35, $BS_FLAT)
    GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
    GUISetState(@SW_SHOW)
    #EndRegion ### END Koda GUI section ###


    WinSetTitle($Form3, "", $settitle);ensure a change to Koda design doesn't stop script working
    $mainxy = WinGetPos($Form2)
    WinMove($Form3, "", $mainxy[0] + 20, $mainxy[1] + 30)
    ;$set = _CommSetport(1,$result,9600,8,0,1,0)
    ;help
    ;send /rcv
     _CommSetDllPath("c:\windows\system32\commg.dll")
        $CommPort = _CommListPorts(0);find the available COM ports and write them into the ports combo
  If @error = 1 Then
     MsgBox(0, 'trouble getting portlist', 'Program will terminate!')
                Exit
  EndIf

        ;$portlist = "com1"
    For $pl = 1 To $CommPort[0]
        GUICtrlSetData($CmboPortsAvailable, $CommPort[$pl]);_CommListPorts())
    Next
    GUICtrlSetData($CmboPortsAvailable, $CommPort[1]);show the first port found
    GUICtrlSetData($ComboFlow, $FlowType[$setflow])
    _GUICtrlComboBox_SetMinVisible($CmBoBaud, 10);restrict the length of the drop-down list

    $retval = 0

    While 1
        $msg = GUIGetMsg()
        If $msg = $BtnCancel Then
            If Not $mode Then $retval = -1
            ExitLoop
        EndIf


        If $msg = $BtnApply Then
                        Local $sportSetError
                        $comboflowsel = GUICtrlRead($ComboFlow)
                        For $n = 0 To 2
                                If $comboflowsel = $flowtype[$n] Then
                                        $setFlow = $n
                                        ConsoleWrite("flow = " & $setflow & @CRLF)
                                        ExitLoop
                                EndIf

                        Next
                        $setport = StringReplace(GUICtrlRead($CmboPortsAvailable),'COM','')
                        _CommSetPort($setPort,$sportSetError,GUICtrlRead($CmBoBaud),GUICtrlRead($CmboDataBits),GUICtrlRead($CmBoParity),GUICtrlRead($CmBoStop),$setFlow)
                        MsgBox(0,'Setport error = ',$sportSetError)
                        $mode = 1;
                        ExitLoop
                EndIf

        ;stop user switching back to $form2
        If WinActive($maintitle) Then
            ConsoleWrite('main is active' & @CRLF)
            If WinActivate($settitle) = 0 Then MsgBox(0, 'not found', $settitle)
        EndIf


    WEnd
    GUIDelete($Form3)
    WinActivate($maintitle)
    Events()
    Return $retval


EndFunc   ;==>SetPort

评分

参与人数 1金钱 +10 收起 理由
user3000 + 10 耐心人

查看全部评分

发表于 2012-4-12 10:16:53 | 显示全部楼层
回复 6# xms77


    lin0308
以被盗走
发送的还是不行啊
发表于 2012-4-12 12:31:32 | 显示全部楼层
回复 7# lin12458
发送不行是什么意思?发送出错还是发送了没有反应?
发表于 2012-4-12 18:36:34 | 显示全部楼层
回复 8# xms77
发送的 FF收到的是
COM2, Write: 3(Bytes) 66 66 0D
发表于 2012-4-12 21:22:35 | 显示全部楼层
回复 9# lin12458
你用的是com口检测软件吗?十六进制显示接受到数据呀!
发表于 2012-4-24 23:37:09 | 显示全部楼层
本帖最后由 hxg123 于 2012-4-24 23:51 编辑

我今天用到了向串口发送十六进制。
如设备地址0x3,控制命令0xa7,我用这个实现了控制。
$Address=3
$Command=0xa7
_CommSendByte($Address)
_CommSendByte($Command)
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-12-23 22:05 , Processed in 0.085231 second(s), 27 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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