views:

61

answers:

3

I'm having difficulty sending an integer to a serial port... I'm trying stuff like this, which run fine but I'm not picking anything up at the port.

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

Any advice?

+2  A: 

Get hold of PortMon By Mark Russinovich (formerly of SysInternals).

This will help you confirm that the port is being opened, configured correctly and written to. You should see your process making an IRP_MJ_WRITE operation with a result of SUCCESS.

If that is all working then the issue is probably that whatever you have attached to COM1 is expecting a different baud rate.

GrahamS
Thanks Graham... PortMon is excellent.
Galwegian
+3  A: 

Shouldn't the last argument to sPort.Write be the number of bytes you want to send?

e.g.

  sPort.Write(New Byte() {Hex(1), 255}, 0, 2)
RobS
LOL. Good point: see http://msdn.microsoft.com/en-us/library/ms143551(v=VS.100).aspx
GrahamS
Yup. Baudrate is wrong too, 57600 is the closest match unless the driver can handle it somehow.
Hans Passant
Thanks Rob and Hans.
Galwegian
+1  A: 

Try this

    '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()
dbasnett
Thanks @dbasnett
Galwegian
No problem. My breakout box is the oldest piece of equipment (20+ years) that I have and still works.
dbasnett