views:

228

answers:

2

I have written a program in .NET that listens to a particular Serial Port and processes the data that is being received. I wrote a test program using serialport control and my program was working fine. When i run our program in Windows Mobile with the original device, the data received was garbled. The same device when connected with PC produced the desired output.

In mobile I connected our device with bluetooth.
I recive the following garbled string : "?09?D0??6D?
I have used the following code :

 Dim WithEvents port1 As SerialPort = _
  New SerialPort("Com2", 19200, Parity.Even, 7, StopBits.One)

 port1.Open()
        port1.Open()
        strcmd="09RD00000123"
        port1.Write(strcmd, 0, strcmd.Length)
        System.Threading.Thread.Sleep(70)
        strReadSegment0 = port1.ReadExisting
+1  A: 

You forgot to say what input you expected. Seeing question marks in the received data indicates that the SerialPort.Encoding property isn't set right. It defaults to ASCII, any byte that has a value between 128 and 255 will be turned into a question mark. Maybe you don't actually want to receive characters, maybe you need bytes. Use Read().

Another explanation for question marks is that the serial port configuration for the device doesn't match the one for the machine. A baudrate mismatch produces garbled data, that could turn into question marks as explained above. Getting a parity mismatch also produces question marks.

Last but not least, you cannot use Sleep() to reliably synchronize your thread with the serial port. Use the DataReceived event instead.

Hans Passant
A: 

Duplicate post?

dbasnett
click close, don't post an answer.
ctacke