所以我试图连接到一块硬件。如果我先连接并断开与超级终端的连接。然后关闭连接和程序。一切正常。如果我没有从硬件上收到随机字符。我在超级终端中使用与在代码中相同的设置。
波特= 9600
parity = n
data = 8
停止= 1
硬件流控制" ON":
octs = on to = on on dtr = on rts = hs
如果我然后断开硬件和串口,我将再次遇到同样的问题。
无论如何,我可以看到在超级终端打开并关闭端口后如何配置comport?我应该注意到我正在使用多产的串口转USB适配器。
以下是我用来打开com端口的代码。
Function OpenCom(PortNum As Integer, Baud As Long) As Long
Dim lpDCB As DCB
Dim ComTimeout As COMMTIMEOUTS
com$ = "COM" + Trim(Str(PortNum))
'open the communications port
hcomtemp& = CreateFile(com$, GENERIC_READ Or GENERIC_WRITE, 0, ByVal 0, OPEN_EXISTING, 0, ByVal 0)
'check for errors
If hcomtemp& < 0 Then
OpenCom = hcomtemp&
Exit Function
End If
r& = PurgeComm(hcomtemp&, 12) ' purge the comm RX and TX (RXCLEAR=0x08 and TXCLEAR=0x04)
' COMMAND LINE for "Hardware" flow control - mode com: baud=9600 parity=n data=8 stop=1 octs=on to=on dtr=on rts=hs
Build$ = "baud=" + Trim(str(Baud)) + " parity=N data=8 stop=1 octs=on to=on dtr=on rts=hs"
'build the data communications block
r& = BuildCommDCB(Build$, lpDCB)
'set the communications port's parameters with the DCB
r& = SetCommState(hcomtemp&, lpDCB)
ComTimeout.ReadIntervalTimeout = 100 'maximum time to wait between received bytes (milliseconds)
ComTimeout.ReadTotalTimeoutConstant = 1000 'maximum time to wait for receive data (milliseconds)
'set the timeouts
r& = SetCommTimeouts(hcomtemp&, ComTimeout)
'set the input buffer size to 4096 bytes and the output buffer size to 4096 bytes
r& = SetupComm(hcomtemp&, 4096, 4096)
'return the handle of the newly opened communications port
OpenCom = hcomtemp&
End Function
答案 0 :(得分:1)
请尝试高级串口监视器 - &gt;间谍模式。 http://www.aggsoft.com/serial-port-monitor.htm。它将显示Hyperterminal在端口上执行的所有操作。然后,您可以重复这些设置。似乎问题与硬件流控制设置有关。
答案 1 :(得分:0)
以下是我用来修复问题的代码。对于一件设备,我只需要这样做。
Function HandShakeBM5AS(ComPort As Integer) As Boolean
Dim Bm5ACom As Long
Dim x As Variant
Dim Path As String
comm$ = ComPort
Commands$ = "MODE COM" & comm$ & ": BAUD=9600 PARITY=N DATA=8 STOP=1 TO=ON XON=OFF ODSR=OFF OCTS=ON DTR=ON RTS=HS IDSR=OFF"
Call Shell("cmd.exe /S /C" & Commands$, vbNormalFocus)
'Shell (Commands$)
End Function
答案 2 :(得分:0)
BuildCommDCB()失败包含在控制字符串中。这会导致lpDCB未正确设置,并且使用错误的值调用SetCommState。
调用BuildCommDCB后,可以在lpDCB结构中设置RTS控制标志。 (我会包含代码,但我对基本语法不确定)