Serial Port

I have an application that needs to read from a scale connected via serial port to a PC multiple times per second.  As of now, I'm not controlling the frequency, but executing:

if(!inputPort.IsOpen())
{
    inputPort.Open()
}

string portString=inputPort.ReadLine()
//parse string as needed

This code executes perhaps more than 100 times per second.  I can limit the rate programatically to once every 100 milliseconds, but not less often than that.

Anyway, the behavior that I'm witnessing is an extreme lag in reading from the scale, on the order of 60 seconds.  When the scale does start reading, it seems OK, but it doesn't respond quickly to the initial change (the 60 second lag).  

If needed, I can issue a command to the scale to get  an output from it, but right now I have the scale streaming data and am trying to read the most current line from the scale using the code above.

Any suggestions would be appreciated.

Thanks.
l2BravoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

akbCommented:
I have no experience with C# but plenty with Delphi and scales.
Do you have a NewLine character set or is it the default LineFeed?
Are the scales outputting the correct NewLine character?
Do you have a timeout period set?
Maybe you should set a very short timeout period and display a the time after each timeout. That way you can see if the program is ticking over or if it is stuck waiting for serial input.
As soon as you receive input from the serial port display that. Hopefully this will help track down the problem.
You could also display the ascii value of each character as it is received from the serial port. This may help to confirm you have the correct NewLine characters set.
l2BravoAuthor Commented:
Thanks for the help.  There was a bug in my code where I was executing ReadLine() twice, but while removing the bug helped it still wasn't behaving correctly, so I set the program to operate via command mode.  It now sends a command to the scale and displays the response as soon as it arrives.  The display is now showing the changes very rapidly.  

The one problem I have now is correctly exiting the program.  I have the serial ports operating on a continuous background thread, but I think the program is not exiting that thread correctly due to waiting for a response from the port.  Before adding the code to read scales from a serial port, the software closed seamlessly, but now it's hanging up (apparently caught up in writing to or reading from a port).  Do you have any ideas about this issue?

Thanks.
akbCommented:
Do you have a timeout set for the ReadLine()? It may be waiting forever for a response which isn't coming.
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

l2BravoAuthor Commented:
I do have a read timeout of 200 ms for each of the 8 scales I'm using.  I also have a write timeout, as well, of the same duration.  

One thing I think might be problematic is when I close the program, I abort the background thread on which the ports operate, but the datareceived event operates on its own thread, so perhaps I can dispose of the ports when I close the program to release the resources?
akbCommented:
Serial ports can do strange things to software, especially if there is data still sitting in the buffer when you close the ports. A little known fact is that in XP and Win 7 (I don't know about Win 8) if data is sent to a serial port on the PC and no program has the port open it can cause the OS to hang or at least run extremely slow. The only fix I have found is to reboot the OS. It doesn't seem to affect USB serial adapters but it does affect most serial port cards and serial ports on the main board.

It may be worth making sure there is no data on any of the serial ports before closing the program. Don't use ReadLine() for this as there may be data sitting there without an EOL character. Use a command which will read data one character at a time.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
l2BravoAuthor Commented:
Thanks for the tip.  The specific problem I faced was that I was waiting the read and write operations to complete before exiting the program, but using the logic that I was, that just wasn't happening, so the program was hanging.   Your advice helped and the bit about eliminating data on the ports will prove useful.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.