views:

346

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 com0com (the Null-modem emulator) and my program was working fine. I even tested it with HyperTerminal and that seemed to work fine too.
However when I interfaced the software with the original device (an output received from a control system), the data received was garbled. It contained special characters. The same device when connected with Hyper Terminal produced the desired output. I changed the baud rates, parity etc but the data received was the same set of garbage characters.
I have used the DataReceived event of the SerialPort component and used the following line of code to capture data:

    string data = portRecieve.ReadExisting();

Can somebody tell me where am i missing out? In the current environment, the output from the device is directly connected with a dot matrix printer which prints whatever is received on the port. The printer seems to catch what is being sent but my code couldn't.
If you ever encountered a similar scenario, Please share your findings.
Thanks

A: 

It sounds to me like the device is putting the printer into some special graphics mode. If so, there is likely to be escape sequences in the data being sent to the printer, ie. character sequences starying with an escape (27, 0x1B) character. In this case, you'll have to look at the printer manual to see what the commands do. Alternatively, you might be able to tell the device to use a simple ASCII only printer, rather than a intellifent one.

Simon Callan
The printer is a normal Epson dot matrix printer and printing doesn't seem to be in graphical mode. (as I can see the same output on Hyperterminal)
Burhan
It's possible that Hyperterminal is interpreting the codes being sent to it, and therefore you're not seeing them on the screen.What we realyl need is a medium size example of the data being received.
Simon Callan
+1  A: 

I can think of the following reasons why the data might apperar garbled:

  • If there is a bad physical connection, you can sometimes just get garbage (rather than nothing at all). Try unplugging and replugging the leads - and check that you have the correct lead (e.g. do you need a nullmodem?). It looks as though you have this covered by checking in HyperTerminal.

  • If the baud rate, stop bits, parity are not correct - sounds like you have this one covered

  • You are trying to receive the data as a string. If it is not sent as plain text, or if your encoding is wrong, then it could easily appear garbage-like. Try using a binary receive and examine the raw data that you are receiving. This will tell you whether the data is just wrong or the .net conversion is screwing it up - eliminate the middle man!

Jason Williams
Yes, I will now experiement with Binary Receive.
Burhan