LinuxQuestions.org
Help answer threads with 0 replies.
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 10-14-2003, 08:20 PM   #1
chr15t0
Member
 
Registered: Jun 2002
Location: London
Distribution: Slackware
Posts: 201

Rep: Reputation: 30
Poll on Device::SerialPort killed by ^C


I'm using the Device::SerialPort to listen on /dev/ttyS0 on a linux box. I have the usual 8N1 settings and I'm polling with a loop similar to this:
Code:
for(;;){
        (my $count, my $result) = $PortObj->read(500);
        print "$result\n";
}
The serial cable is connected to another Slackware box and I'm sending strings like:
Code:
$echo "foo" >> /dev/ttyS0
$echo "bar" >> /dev/ttyS0
That works fine and I see 'foo' or 'bar' fly by on the screen when they come in. The problem occurs when the string sent contains a ^C character (\x03). It always causes the poll to halt. The script doesn't actually die, but the loop ceases and any input which is sent after that point remains undetected until the script is killed and restarted and the serial device is read once again.. at which point a few lines of what was sent after the ^C are spurted out.. and the poll continues.

Does anyone have any idea on how to get around this either with some Device::SerialPort trickery, or perhaps taking control away from that terminal?

thanks
christo

Last edited by chr15t0; 10-14-2003 at 08:25 PM.
 
Old 10-15-2003, 09:01 AM   #2
dkloes
Member
 
Registered: Oct 2003
Posts: 66

Rep: Reputation: 15
You might look at the stty command for options on the serial port settings. You can find the current settings:
stty -a < /dev/ttyS0

Here is a quote I found that may help:

If IGNBRK is set, a break condition detected on input is ignored that is, not put on the input queue and therefore not read by any process. If IGNBRK is not set and BRKINT is set, the break condition will flush the input and output queues, and if the terminal is the controlling terminal of a foreground process group, the break condition will generate a single SIGINT signal to that foreground process group. If neither IGNBRK nor BRKINT is set, a break condition is read as a single 0x00, or if PARMRK is set, as 0xff 0x00 0x00.

An example of setting ignbrk on /dev/ttyS0:
stty -F /dev/ttyS0 ignbrk

Hope this helps. If not, maybe we can come up with some other ideas.
 
Old 10-15-2003, 09:25 AM   #3
chr15t0
Member
 
Registered: Jun 2002
Location: London
Distribution: Slackware
Posts: 201

Original Poster
Rep: Reputation: 30
seems sensible

thanks for replying, dkloes - as it happens, I think you've hit the nail pretty much on the head (to use a strange english idiom).. I actually discussed this problem with a friend last night and he suggested that I do all my logging in raw mode, ie
Code:
$stty -F /dev/ttyS0 -raw
From what I can see, this has actually solved the problem. the docs for stty state that -raw is equivalent to -cooked, which does the following:
Code:
       cooked same  as  brkint ignpar istrip icrnl ixon opost isig icanon, eof
              and eol characters to their default values
Not exactly a wordy description, and it doesn't really explain what the 'default values' are.. but anyway, I think I'm okay now. I'll post back if I have more problems

thanks.,
christo
 
Old 10-15-2003, 03:56 PM   #4
chr15t0
Member
 
Registered: Jun 2002
Location: London
Distribution: Slackware
Posts: 201

Original Poster
Rep: Reputation: 30
Well I have had a fiddle with this, but I seem to be getting only intermittent success. My poll currently looks like this:
Code:
for(;;){
        my $result=$PortObj->input;
        if($result =~ m/foo/){
                $count++;
                my $query="INSERT INTO logs(cl_date, cl_dialled) values(now(),'$count')";
                my $clsth=&db_query($query);
        }
}
I am then sending strings down the serial connection from the other box with:
Code:
bash-2.05b# echo "foo" >> /dev/ttyS0
bash-2.05b# echo "foo" >> /dev/ttyS0
bash-2.05b# echo "foo" >> /dev/ttyS0
heh - you get the idea.. Anyway, if I sent 4 lines, I might only get 1 or 2 new rows in the databse - then perhaps I'll send 8 lines and get nothing, so then I'll restart the logger and send 5 lines.. and I get 3 new entries in the database... What I'm trying to say is that it's totally random whether or not the text gets picked up at the other end. I guess I generally manage to log 10-20% of the strings that get sent down.

I took your advice and I have set IGNBRK - in fact here is the result from stty on /dev/ttyS0:
Code:
root@alexis:/export/serial/# stty -a < /dev/ttyS0
speed 9600 baud; rows 0; columns 0; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; start = ^Q; stop = ^S; susp = ^Z;
rprnt = ^R; werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 0;
-parenb -parodd cs8 hupcl -cstopb cread clocal -crtscts
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr -icrnl -ixon -ixoff -iuclc -ixany -imaxbel
-opost -olcuc -ocrnl -onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
-isig -icanon -iexten -echo -echoe -echok -echonl -noflsh -xcase -tostop -echoprt -echoctl -echoke
now, I wonder if there is some way that Device::SerialPort can be made to detect that Data is ready to send, rather than just polling - am I going about this the wrong way?

thanks for your help,
christo :)

Last edited by chr15t0; 10-15-2003 at 03:57 PM.
 
Old 10-16-2003, 05:28 AM   #5
dkloes
Member
 
Registered: Oct 2003
Posts: 66

Rep: Reputation: 15
The output from your stty is still showing -ignbrk

Also, intr is set to ^C so you may want to set intr to something else. Are we dealing with other control characters in the string as well?

Why >> instead of >??
 
Old 10-16-2003, 09:16 AM   #6
chr15t0
Member
 
Registered: Jun 2002
Location: London
Distribution: Slackware
Posts: 201

Original Poster
Rep: Reputation: 30
I'll keep updating this thread with what I'm getting.. and at the moment, I'm finding that however I send the data and however I receive it, there's always a bunch of missing bytes and usually near the start. I have tried with mincom, I have tried to just cat /dev/ttyS0 at the RX end, I have tried polling with Device::SerialPort and I have tried polling on the device file direct in PERL too - every method has the same results (with the occasional minor variation). I have turned on IGNBREAK, I'm reading & writing in raw mode and I have turned on RTS/CTS handshaking. I am currently testing without polling and just sending & receiving a list of 20 words. I successfully get all 20 words about 20% of the time.. I'm beginning to think this should move into the Linux forum. It's looking like it's not a PERL issue, eh

And just to illustrate that it's not a cabelling problem, if I boot the receiving computer into windows and test with hyperterminal, everything is fine... get that. It works with hyperterminal, but not with minicom. It must be something to do with the stty setup..

anyway,
bed time

christo
 
  


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
Question for serialport joypad!Help! tonewangl Linux - Software 0 06-29-2005 10:23 AM
Poll itsjvivek Linux - General 0 12-10-2002 11:18 PM
A Poll ! super_me General 1 06-08-2002 07:24 AM
POLL: Where do you... glock19 Linux - General 9 04-19-2002 03:05 PM
Poll eraser LQ Suggestions & Feedback 4 08-10-2000 02:01 AM

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

All times are GMT -5. The time now is 07:36 AM.

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