找回密码
 加入
搜索
查看: 9355|回复: 13

[网络通信] Serial Port 串口操作 _CommSendstring

  [复制链接]
发表于 2012-7-12 14:11:29 | 显示全部楼层 |阅读模式
本帖最后由 ljxu 于 2012-7-12 14:13 编辑

#include 'CommMG.au3'

Global $sportSetError = ''
Global $CMPort = 6 ; Port
Global $CmBoBaud = 57600 ; Baud
Global $CmboDataBits = 8 ; Data Bits
Global $CmBoParity = "none" ; Parity
Global $CmBoStop = 1 ; Stop
; Global $setflow = 2 ; Flow

_CommSetPort($CMPort, $sportSetError, $CmBoBaud, $CmboDataBits, $CmBoParity, $CmBoStop)
_CommSendstring("" & @CR)
_CommSendstring("reboot" & @CR);重启,重启过程中会出现VLAN UP列表(5秒钟之后默认选择3),我需要这时输入2,即选择2
While 1
$instr = _CommGetString()

        If $instr <> '' Then
               
                Local $result = StringInStr($instr, "VLAN UP")
                If $result <> 0 Then
                        ;MsgBox(0, "VLAN UP", $result)
                        _CommSendstring("2" & @CR);没有输入2再回车,而是直接回车?!!!
                        ConsoleWrite($instr & "-----------------1" & @CRLF)
                        Exit
                EndIf

        EndIf
WEnd

_CommClosePort()
If @error = -1 Then ConsoleWrite("!Error: " & @error & @CRLF)
ConsoleWrite输出为:
VLAN UP

-----------------1


有哪位知道原因的?谢谢

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
发表于 2012-7-12 16:53:48 | 显示全部楼层
本帖最后由 xiehuahere 于 2012-7-12 16:55 编辑

我最近也在看这方面的东西,这个UDF需要 commg.dll,这个东西对我来说是黑盒,并且没有经过太多人的测试。
我先在尝试用微软提供的MSCOMM32.OCX,麻烦的话再来看这个UDF。传送门:
http://msdn.microsoft.com/en-us/library/aa259393(v=VS.60).aspx   ---  发现论坛一个bug,[/url]自动添加在小括号前,导致网址链接显示不正确,我手动更改过来
http://www.autoitscript.com/forum/topic/13890-rs232/
 楼主| 发表于 2012-7-13 09:44:46 | 显示全部楼层
回复 2# xiehuahere

http://www.autoitscript.com/foru ... -port-com-port-udf/
缺少的dll,你可以去这里下载
发表于 2012-7-13 10:44:44 | 显示全部楼层
回复 3# ljxu


    我下了dll的,只是对我来说是黑盒,万一有问题我无法修改。
而微软的mscomm32.ocx虽然对我也是黑盒,但测试的人多了,问题应该要少些。

但我昨天发现,还是用UDF会方便许多。
发表于 2012-7-13 10:47:29 | 显示全部楼层
本帖最后由 xiehuahere 于 2012-7-16 11:10 编辑

回复 1# ljxu

是数字2而非字符2,可以试试用 _CommSendByte() 发送ascii格式的数字2和回车,或者用 _CommSendString() 发送 Chr(50) & @CR
发表于 2012-7-18 12:50:13 | 显示全部楼层
本帖最后由 xiehuahere 于 2012-7-18 12:55 编辑

回复 3# ljxu


    哥们,解决了吗?
那个UDF中的_CommPortConnection函数是不是没用啊,直接_CommSetPort就行了吧?
我看Example里也没用到。

还有,为什么要 _CommSendstring("" & @CR) 先发送一个空字符呢?
发表于 2012-7-19 12:59:49 | 显示全部楼层
本帖最后由 xiehuahere 于 2012-7-20 12:12 编辑

搞定了!
基于这个UDF,我给自己封装了用于串口收发的两个函数SendLine和WaitFor,这样用起来就方便啦  ^_^
Local $buff, $dbgSwitch = False
 
;============================================================================
; Name: SendLine
; Description: Send one command line
; Return value: 0 - Success
;                -1 - Failure
;============================================================================
Func SendLine($string)
        _CommClearInputBuffer()
        _CommClearOutputBuffer()
        
        _CommSendstring($string & @CR, 1) ; Wait till string sent
        If @error <> 0 Then
                Logger("! Error occured while sending string '" & $string & "', ErrCode: " & @error, 2)
                Return -1
        EndIf
        Logger("T -> " & $string, 6)
        Return 0
EndFunc

;============================================================================
; Name: WaitFor
; Description: Wait for desired output. Use it after each SendLine() to make buff always hold the latest content
; Return value: 0 - Success
;                -1 - Failure
;============================================================================
Func WaitFor($string, $timeout=2000)
        $buff = ""        ; Clear buff before each receiving
        If $string <> "" Then
                Local $timer = TimerInit()
                Do
                        $buff &= _CommGetString()
                        If @error <> 0 Then
                                Logger("! Error occured while receiving string '" & $string & "', ErrCode: " & @error, 2)
                                Return -1
                        EndIf
                        If $buff = '' Then
                                Sleep(20)
                        Else
                                mgdebugCW($buff & @CRLF)
                        EndIf
                        If TimerDiff($timer) > $timeout Then
                                Logger("! Timeout when waiting for '" & $string & "'", 2)
                                Return -1
                        EndIf
                Until StringInStr($buff, $string) > 0
        EndIf
        Logger("R <- " & $buff & @LF, 6) ; @LF is for Unix->DOS convertion
        Return 0
EndFunc
 
Func Logger($msg, $logLevel = 8)
        Switch $logLevel
                Case 1
                        $levelInfo = "Level(1,User)"
                Case 2
                        $levelInfo = "Level(2,Warning)"
                Case 4
                        $levelInfo = "Level(4,Error)"
                Case 6
                        If Not $dbgSwitch Then Return        ; Default: No debug info
                        $levelInfo = "Level(6,Debug)"
                Case 8
                        $levelInfo = "Level(8,Information)"
                Case Else
                        $levelInfo = "Level(?,None)"
        EndSwitch
        Local $logfile = $logFolder & "\log" & '_' & @YEAR & @MON & @MDAY & '.txt'
        _FileWriteLog($logfile, $levelInfo & ", " & $msg)
EndFunc
 楼主| 发表于 2012-7-19 21:06:37 | 显示全部楼层
回复 7# xiehuahere


    编程习惯这么好的!

我还没搞定,发送空白字符是因为我这的串口的必须先回车一下才会有输出,你的可能不需要。
我现在的问题:我需要修改串口输出中的内容
比如:我讲的例子中的需要输入2,现在我想输入2,然后再删除2,再输入3
试了多种方法方法删除2这个动作始终不能执行,伤不起啊。。。
发表于 2012-7-19 22:36:56 | 显示全部楼层
本帖最后由 xiehuahere 于 2012-7-19 22:49 编辑

回复 8# ljxu

我试验过了,我的串口也是需要先发送一个回车来触发的。
已发送到串口终端的内容无法删除,想象不到你为什么有这种需求。
发表于 2012-7-19 22:59:57 | 显示全部楼层
回复 8# ljxu


    你试试发送对应按键(Backspace)的ASCII码试试。
 楼主| 发表于 2012-7-20 08:40:25 | 显示全部楼层
回复 9# xiehuahere


    是终端输出的内容我需要删除:
例:终端输出jack,我需要删除k,输入son,jacson
发表于 2012-7-20 09:44:37 | 显示全部楼层
进来学习一下了。
发表于 2012-7-20 09:53:19 | 显示全部楼层
回复 11# ljxu


    这个真不会,只能你自己摸索了。
   我也还要去研究怎么通过串口传文件,Zmodem协议或其他。
 楼主| 发表于 2012-7-20 09:57:45 | 显示全部楼层
回复 13# xiehuahere


   有结果了让我也学习学习啊
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-5-4 16:42 , Processed in 0.086010 second(s), 24 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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