LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-10-2009, 08:33 PM   #1
vinst
LQ Newbie
 
Registered: May 2009
Posts: 5

Rep: Reputation: 0
Inconsistency in transferring commands over serial port


I have been reading data(non-canonical input) off a serial port in a loop. The device I am connected to sends the data in a bursty manner and I have to read using the loop. The device sends some data when initially powered on which I am supposed to read. I can then send "commands" to the device over the serial port. The "command" is supposed to be "debug d 35 5 0\r"
After I send the command there is some data that is sent back by the device to show that the command was successful.

This command should elicit the right response from the device but it responds correctly some times while other times it does not. (when I try to write the consecutive commands)

If I send two consecutive commands after reading the initial data and the command feedback I face some problems.

I am not sure what the reason could be for this. I have tried flushing the input and output buffers using tcflush before writing the data to the serial port but this problem still persists.

I would be really grateful if someone could throw any ideas about tackling this problem
 
Old 06-11-2009, 08:44 AM   #2
FLLinux
Member
 
Registered: Jul 2004
Location: USA
Distribution: Fedora 9, LFS 6.3, Unbuntu 8.04, Slax 6.0.7
Posts: 145

Rep: Reputation: 15
Only things i can think of are that you baud rate might be incorrect or you are sending commands to close together for the device to process correctly.

Can you look at the output from the device on a something like hyperterm (forgot what the Linux program for this is) and see if you can send the commands that way.
 
Old 06-11-2009, 09:22 AM   #3
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
@FLLinux:
minicom,I suppose
 
Old 06-11-2009, 09:54 AM   #4
FLLinux
Member
 
Registered: Jul 2004
Location: USA
Distribution: Fedora 9, LFS 6.3, Unbuntu 8.04, Slax 6.0.7
Posts: 145

Rep: Reputation: 15
Yeah that is it. Try connecting to your device using that and see what kind of respsones you are getting back.
 
Old 06-11-2009, 10:43 AM   #5
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,397
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
When programming this type of arrangement, especially when using the serial port in non-canonical 'raw' mode, your code needs to parse the incoming data into discrete records. Since the serial driver in raw mode does not distinguish any character(s) as record delimiters, your code will need to accumulate and parse the incoming data. This may require multiple read() calls, some of which may return nothing. The prudent use of timeouts (use VMIN & VTIME) and/or select() to determine when to read data may result in more consistent results. Proper use of commands and command order and timing is device-specific, and undefined behavior may result, as you have seen. It goes without saying that proper line protocol (bit rate, parity, etc.) is required. One should not overlook cabling, especially proper ground return and handshake/flow-control signaling when diagnosing intermittent behavior.
Use of a known-good utility to allow interactive communication with the device can be a significant aid. Someone mentioned minicom, which is often distributed with Linux. I prefer C-Kermit for such applications.
--- rod.
 
Old 06-11-2009, 07:27 PM   #6
vinst
LQ Newbie
 
Registered: May 2009
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by theNbomr View Post
When programming this type of arrangement, especially when using the serial port in non-canonical 'raw' mode, your code needs to parse the incoming data into discrete records. Since the serial driver in raw mode does not distinguish any character(s) as record delimiters, your code will need to accumulate and parse the incoming data. This may require multiple read() calls, some of which may return nothing. The prudent use of timeouts (use VMIN & VTIME) and/or select() to determine when to read data may result in more consistent results. Proper use of commands and command order and timing is device-specific, and undefined behavior may result, as you have seen. It goes without saying that proper line protocol (bit rate, parity, etc.) is required. One should not overlook cabling, especially proper ground return and handshake/flow-control signaling when diagnosing intermittent behavior.
Use of a known-good utility to allow interactive communication with the device can be a significant aid. Someone mentioned minicom, which is often distributed with Linux. I prefer C-Kermit for such applications.
--- rod.
Thank you for your help. I have set VMIN and VTIME and also the specified baudrate for the device. But there still appears to be some problem. I have temporarily solved it by repeatedly sending the command to the device which responds after two or three times which is ok for my application.

C-Kermit was useful in this regard.The device didnt seem to register the sent command straight away and needed some tine or repeated transmissions.

SO maybe it is something with the device .

Thank you very much
 
Old 06-11-2009, 11:38 PM   #7
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,397
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
What is the nature of your device? If it is some kind of test & measurement gear, it may take some time to acquire a reading before it returns data to you.

--- rod.
 
Old 06-12-2009, 06:33 PM   #8
vinst
LQ Newbie
 
Registered: May 2009
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by theNbomr View Post
What is the nature of your device? If it is some kind of test & measurement gear, it may take some time to acquire a reading before it returns data to you.

--- rod.

The device is a wavelength selective switch (you can think of it as a simple switch for an incoming signal). And it does respond instantly as I checked it using C-kermit.

Timing also doesnt seem to be the issue because I made the program idle for some time before and after sending data to the device but there isnt any difference. The problem as it appears to me is that some of the characters are not being properly written into the serial port. But I have also set the baudrate and the parity settings correctly so I am not exactly sure what is happening.

If I try to send the command repeatedly then I do end up with the desired behavior.
 
Old 06-13-2009, 12:13 AM   #9
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,397
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Perhaps a flush() on the open filehandle after writing the data. I am not sure how/if the driver might buffer outgoing data, especially in raw mode. When you send data to the device, is the entire commend issued in a single write() call? If you have another computer that can read/write serial data by running something like C-Kermit, or even a second serial port on the computer you're using, you might be able to use it to approximate a simulation of your device, and better characterize the fault you are witnessing.
--- rod.
 
Old 06-15-2009, 03:25 PM   #10
vinst
LQ Newbie
 
Registered: May 2009
Posts: 5

Original Poster
Rep: Reputation: 0
Hello Rod

I write to the data in one single write command. And I was flushing the buffers after some time. I am able to get the device working but it takes two or three write commands(in a loop) sometimes. \

But your idea with a second serial port is great. I will try that out

Thank you very much for all your help
 
Old 06-15-2009, 07:13 PM   #11
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,397
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Okay, one last thought. Do you use any kind of command terminator, such as the typical carriage-return &/or linefeed? Is it possible that your serial port configuration is translating one or more of those? One can envision a scenario like the device receiving the full command string, minus the terminator, and not replying until it ultimately times out waiting for nothing but the terminator. Or, perhaps the command data is reaching the device intact, but your receiving code is dropping data or the driver is holding it in a buffer en-route to your process.
There is a tool, serlook, which allows you to observe the comings and goings of serial data with some timestamps, and a little closer look at the data, particularly 'invisible' data like CRs & LFs. I have only toyed with it, but haven't used it on any real project, yet. It might also be a useful diagnostic tool for your problem.
If the serial-port code is sufficiently isolated, perhaps you could post the relevant code fragments for inspection by others.
--- rod.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
linux serial port to router console port connection? frankie_fix Linux - General 3 02-26-2007 10:32 PM
Parallel Port & Serial Port device identification helpmeforlinux Linux - Hardware 3 01-02-2007 02:15 AM
IBM AIX 5.2/5.3 serial port/tty0 port vinod_balani AIX 2 02-19-2006 04:38 PM
Using serial port card(PCMCIA) with IPAQ running Linux, can't find ttyS0 port d2army Linux - Laptop and Netbook 0 11-12-2005 09:07 PM
serial port (ttyS1) - Sending commands via terminal... HELP!!! Nalorin Linux - Networking 2 08-21-2005 11:22 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration