2015-10-27 105 views
0

所以我试图连接到一块硬件。如果我先连接超级终端并断开连接。然后关闭连接和程序。一切正常。如果我不收到硬件中的随机字符。我在超级终端中使用与代码中相同的设置。Com端口不工作,除非超级终端打开并关闭端口第一

波特= 9600

奇偶=正

数据= 8

停止= 1个

硬件流控制 “ON”:

OCTS =到上DTR = = on rts = hs

如果我然后断开硬件和串口I将再次出现同样的问题。

是否有反正我可以看到如何在超级终端打开和关闭端口后配置正在配置?我应该注意到我正在使用一个多产的串口转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 

回答

1

请尝试高级串行端口监视器 - >间谍模式。 http://www.aggsoft.com/serial-port-monitor.htm。它将显示超级终端在端口上执行的所有操作。然后你可以重复这些设置。看来问题与硬件流量控制设置有关。

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 
0

如果'rts = hs'包含在控制字符串中,BuildCommDCB()将失败。这会导致lpDCB设置不正确,并且使用错误的值调用SetCommState。

调用BuildCommDCB后,可以在lpDCB结构中设置RTS控制标志。 (我会包括代码,但我不确定基本语法)