LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash Serial port Script wont hang up (https://www.linuxquestions.org/questions/linux-newbie-8/bash-serial-port-script-wont-hang-up-837659/)

mmof 10-12-2010 04:54 PM

Bash Serial port Script wont hang up
 
My linux computer runs ubuntu 10.04
I have it hooked up to a device, and the device manual says:

Device Specs:
Baud Rate: 9600
Data Bits: 7
Parity: odd
Stop Bits: 1
Flow control: None

Here is my script:

#!/bin/bash
stty -F /dev/ttyS0 9600 cs7 parenb parodd -cstopb clocal -crtscts -ixon -ixoff
echo "Enter file name [*.txt]:"
read "mytestfile"
echo "200 201" > /dev/ttyS0
cat /dev/ttyS0 > $mytestfile


PROBLEM 1:

The file gets created and most of the data gets saved to it, but the terminal never goes back to the ~ (home directory?) when finished. I can type whatever I want but it wont go anywhere or do anything even when I press enter. I have to click the X to close it and then the prompt comes up saying something like "are you sure you want to close terminal and kill all processes".
How do I fix it?
Reset does not work nor does the "stty sane" command that I read would force it to reset.

PROBLEM 2:

See the 00/01 character in front of 200 and 201, those are <SOH> commands and in the file they are supposed to show up, sometimes they do, sometimes they don't. But there is always supposed to be an <ETX> after each one, and with the way its written now, no <ETX> shows up,
<ETX> is 00/03 i think.

Here is what the output is supposed to look like:

<SOH>
200
BLAH BLAH BLAH DATA DATA
DATA DATA DATA DATA
<ETX>
<SOH>
201
BLAH BLAH BLAH DATA DATA
DATA DATA DATA DATA
<ETX>

yet the first SOH doesn't show up half the time and none of the ETX show up.
If anyone knows why, I would appreciate the push in right direction.

michaelk 10-12-2010 05:47 PM

Welcome to LinuxQuestions!

Might be homework related?
Looks like you are trying to implement some type of file transfer. Some more information about this device and what you are actually trying to do is required to help you.
BTW
SOH - Start Of Heading
STX - Start of Text
ETX - End of Text.
EOT - End of Transfer

So something like a SOH at start of transfer. STX at beginning and ETX at the end of each "line" of data. The 200 looks like number of lines of data sent but need further definition. So basically your program would need to read a number of bytes from the file and then send the STX, send data, ETX characters. You would loop until all bytes are read/transfered and then send the EOT character.

Have you tried CTRL-C to quit the program?

mmof 10-12-2010 06:22 PM

I appreciate the reply,
I am talking to a Veeder Root Tank monitor.

Link to pdf, of the serial manual
http://www.veeder.com/object/576013-635.html

The 200 is the actual command sent to the device.
And I'm new to linux and the only way I got it to talk was to send the soh character. In windows all I had to do was open hyperterminal configure baud rate parity etc. and hit connect, then all I had to press was "ctrl + a" then "200" minus the quotations.

And it would bring up the soh (ctrl + a) symbol then 200 (which is the command) then the data then etx(end of text as you pointed out)

I just tried the CTRL-C like you said, and when I type it in it does return me to the ~ directory, but how do I echo it so that the program does it by itself?
This sounds funny but, the "hello_world" program I did in the book I bought a few weeks ago went back to it by itself. (I'm a newb, I know)
Thanks again for your help, if you need anymore information on the device let me know.

mmof 10-26-2010 01:16 PM

I got it to work!!!!!
The problem was when this line : "cat /dev/ttyS0 > $mytestfile"
would get called, the script would run correctly but it would keep the serial port open, which is why i could never run a single command after wards.

So to fix this I did this:

#!/bin/bash
stty -F /dev/ttyS0 9600 cs7 parenb parodd -cstopb clocal -crtscts -ixon -ixoff
mytestfile=mmof.txt
cat /dev/ttyS0 >> $mytestfile

I have this script run in the background at boot up, and it continues to run the entire time the computer is on.

Then I just run the other script:

#!/bin/bash
stty -F /dev/ttyS0 9600 cs7 parenb parodd -cstopb clocal -crtscts -ixon -ixoff
echo "200 201" > /dev/ttyS0

This is the basic version, I have made a big amount of changes to it, to suit my needs for what the script must do at certain times.
But if anyone is having trouble with cat /dev/ttys0 > textfile.txt
The problem is more than likely when you cat the serial port, it never closes, so the simplest solution to this was to just make a script which always runs in the background. I hope this helps. I searched for so many hours/days/weeks for a solution and last night I thought of this and it worked, and it has been working ever since running ever hour updating the file. I hope this answers others problems with cat and serial port issues.
Thanks, mmof


All times are GMT -5. The time now is 11:52 PM.