0

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

0

3 Answers 3

2

How did you set

SerialPort.DiscardNull
SerialPort.Encoding

And maybe show us an example of the special chars you are receiving.

2
  • DiscardNull is set to False<br/> Encoding is set to set to Default<br/> the chars received are mixed, a few nul-chr(0), some chr(128).
    – Burhan
    Oct 11, 2009 at 16:25
  • I would suggest leaving Discard off and experiment with Encoding first. Could the device be sending UTF-8? Oct 11, 2009 at 17:14
1

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!

1
  • Yes, I will now experiement with Binary Receive.
    – Burhan
    Oct 12, 2009 at 5:31
0

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.

2
  • 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
    Oct 12, 2009 at 5:32
  • 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. Oct 12, 2009 at 17:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.