Solved

Serial Port

Posted on 2014-01-07
6
547 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now