2010-05-12 85 views
0

我有困難發送到串行端口的整數發送到COM1整數...我想這樣的東西,它運行良好,但我不能在港口撿東西了。使用的SerialPort

Private Sub fireToPort() 
    Dim sPort As New SerialPort("COM1", 56000, Parity.None, 8, StopBits.One) 
    sPort.Open() 
    sPort.Write(New Byte() {Hex(1), 255}, 0, 0) 
    sPort.Close() 
End Sub 

有什麼建議嗎?

回答

3

應該不是最後一個參數sPort.Write是要發送的字節數?

例如

sPort.Write(New Byte() {Hex(1), 255}, 0, 2) 
+0

大聲笑。好點:請參閱http://msdn.microsoft.com/en-us/library/ms143551(v=VS.100).aspx – GrahamS 2010-05-12 11:19:40

+1

是的。波特率也是錯誤的,57600是最接近的比賽,除非駕駛員能夠以某種方式處理它。 – 2010-05-12 13:04:58

+0

感謝羅布和漢斯。 – Galwegian 2010-05-12 16:25:38

2

獲取PortMon By Mark Russinovich (formerly of SysInternals)

這將幫助你確認端口被打開,正確配置和寫入。 您應該看到您的流程進行了IRP_MJ_WRITE操作,結果爲SUCCESS

如果這將是工作那麼問題可能是,無論你已連接到COM1期待一個不同的波特率。

+0

謝謝Graham ... PortMon非常棒。 – Galwegian 2010-05-12 16:23:17

1

試試這個

'I looped my serial port on COM5 
    Dim sPort As New IO.Ports.SerialPort("COM5", 57600, IO.Ports.Parity.None, _ 
             8, IO.Ports.StopBits.One) 

    sPort.Handshake = IO.Ports.Handshake.None '<<< pick the correct one 

    sPort.Open() 
    Dim b() As Byte = New Byte() {1, 255, 0, 0} 
    sPort.Write(b, 0, b.Length) 
    sPort.BaseStream.Flush() 

    'because I looped the port I should be able to read it 
    Dim b1(3) As Byte 
    sPort.Read(b1, 0, 4) 
    sPort.Close() 
+0

謝謝@dbasnett – Galwegian 2010-05-13 13:13:17

+0

沒問題。我的分線盒是我擁有並且仍然工作的最老的設備(20年以上)。 – dbasnett 2010-05-13 13:48:00

1
sPort.Write(New Byte() {Hex(1), 255}, 0, 0) 

最後一個參數應該是255這是你將發送數據的長度。