Solved

Serial Port

Posted on 2014-01-07
6
551 Views
Last Modified: 2014-01-17
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.
0
Comment
Question by:l2Bravo
  • 3
  • 3
6 Comments
 
LVL 13

Expert Comment

by:akb
ID: 39764350
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.
0
 

Author Comment

by:l2Bravo
ID: 39771283
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.
0
 
LVL 13

Expert Comment

by:akb
ID: 39772397
Do you have a timeout set for the ReadLine()? It may be waiting forever for a response which isn't coming.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:l2Bravo
ID: 39776578
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?
0
 
LVL 13

Accepted Solution

by:
akb earned 500 total points
ID: 39778042
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.
0
 

Author Closing Comment

by:l2Bravo
ID: 39788436
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.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Code works but it's slow 28 70
How useful is the free version of Selenium? 3 35
C# bracket error 3 31
C# Gridview 1 32
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question