2

Here's the scenario - I have a C# application that reads from a COM port. Most of the time I use devices with a serial adapter and on a machine with a serial ports. However, machines with serial ports are increasingly difficult to come by so I have started using machines with a USB/Serial connection.

In some cases, the C# code that I have will work just fine with a "true" serial connection but fail with a USB/serial connection. The data comes in fragmented with the first part of the data coming in (like maybe the first 1 or 2 characters) and nothing else.

I'm using something basic like comport.ReadExisting() to pick up the data from the port. Is this part of the problem? Are there other methods that would guarantee that all the data would be read in a single string?

Finally, I want to add that I've already played around with some of the USB/serial settings in device manager AND the data comes in fine when using good, ole' hyperterminal . . . so it has to be something in the code.

2
  • Do you expect the data being received to have a protocol. This could be as simple as a string terminated with a newline, or some fixed amount of data. Commented Jul 27, 2011 at 13:43
  • If you are using the DataReceived event properly you should be fine. Seeing the code would help. Commented Jul 27, 2011 at 19:11

2 Answers 2

3

With USB-serial converters, you MUST set your receive timeout, because the data can sit in the USB device for a long time where Windows doesn't know about it. Real serial ports hold data in the 16550 FIFO where the Windows driver can see it.

Sign up to request clarification or add additional context in comments.

3 Comments

Im assuming that you're referring to the comport.ReadTimeout which is already set to 20000 (ie 20 seconds)??? I couldnt find any other timeouts that are close to this
@Jim: In the Win32 API there are timeout until first character, timeout between characters, and total timeout. Not sure whether the .NET SerialPort class gives you all of those. I didn't use it for very long before it became so painful that I made my own wrapper (in C++/CLI) around the Win32 serial port APIs.
Timeout in between characters sounds like its along the right lines since I'm getting some data but not all. But I have so much "invested" in using the .NET library since it works fine with most hardware configs - its just the USB/serial connections that are problematic
1

Most of the times you would want to use the SerialPort.DataReceived event ( http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.datareceived.aspx ).

This requires you to combine the chunks of data manually since you can receive parts of your string, but once you detect the boundary of a single 'record' you can fire of processing that record while letting the IO receive more events.

This allows some asynchronous IO which doesn't block your threads, and thus allows more efficient handling of the data in general. If it helps in your specific situation I don't know, but it has helped me in the past dealing with data reading issues, low speeds, thread pooling, and lock-ups.

1 Comment

I'm already using that event, that's where I'm using comport.ReadExisting which is supposed to clear out the buffer, but again, I'm only getting fractions of the data

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.