Solved

Serial Port

Posted on 2014-01-07
6
557 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

 

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and 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.
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…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

707 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