Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: slackware 15.0 64bit, 14.2 64 and 32bit and arm, ubuntu and rasbian
Posts: 483
Rep:
modem tty access in bash
I can access a radio modem (slice of radio) using minicom which works just fine, but accessing it using bash/sh only works if I first access using minicom, then quit without resetting the modem. my script is something like this (rough psudo code)
Code:
MODEM=/dev/ttyAMA0
stty -F $MODEM speed 9600
while [running]
do
if [ condition1 ]; then
echo "message1" >$MODEM
fi
if [ condition2 ]; then
echo "message2" >$MODEM
read g
#do something with $g
fi
done <$MODEM
I would like to somehow configure the modem to respond in the script, without having to manually run
Code:
minicom -b 9600 -o -F /dev/ttyAMA0
and the quiting with Ctrl-A q each time
This script is running on rasbian jessie, as root user.
any ideas would be appreciated, thanks.
The usual problem is that once the modem device is closed it goes back to its default configuration... You only have a single input connection - but the output is handled separately. So once the stty exits - it would tend to return to the default configuration.
This is why using minicom works - it can set the configuration of both input and output sides of the serial device, and keep the channels open.
A side benefit is that it prevents any other process for doing things to the serial device by locking access.
Distribution: slackware 15.0 64bit, 14.2 64 and 32bit and arm, ubuntu and rasbian
Posts: 483
Original Poster
Rep:
do you any suggestions on how I can set the device and leave it open. ?
I couldn't find any options in stty to do that, although running
Code:
stty -F /dev/ttyAMA0 -a
seems to show that the baud rate is still set after running stty -F /dev/ttyAMA0 [various options...] the first time. Maybe there is something in raspbian jessie which is interfering with the serial port, but allows minicom to leave the modem "open". I am more used to slackware myself, and am very rusty on serial port io. I could probably make my script run minicom and quit, but it seems a very ham-fisted way of talking to a serial port in what should be a straightforward operation.
I should re-mention that if I run minicom first and then quit without resetting the port, then my script works fine.
I just want to achieve "whatever minicom does" in my script.
I think minicom sets other flags besides just the baud rates (modem signals, parity, control flow, escape handling for modem control...) - but then minicom is designed to handle serial lines. Bash isn't.
Serial lines are not that easy to set up. They are asynchronous devices... and bash does not work that way. This is why minicom uses a pty to interface with the user - it provides synchronization between input and output (both from the user and from the serial line).
You can use the "expect" tool to interface... but it isn't easy either.
Distribution: slackware 15.0 64bit, 14.2 64 and 32bit and arm, ubuntu and rasbian
Posts: 483
Original Poster
Rep:
Well, I've solved the problem, so for the benefit of anyone else, here is my solution.
1. run minicom as usual ( minicom -b 9600 -o -F /dev/ttyAMA0 ), and demonstrate that comms work.
2. quit minicom without resetting the modem/port ( Ctrl-A q )
3. run stty -F /dev/ttyAMA0 -g >config.txt
4. take the config text, and in the script, use stty -F $MODEM `cat config.txt`
(I just pasted the contents of the text after $MODEM as it is tidier)
5. ta-da everything just works.
to prove it, go into minicom and edit resetting the modem ( Ctrl-A X )
the script still works, where as before it did not.
If I manually close the port (using minicom) then try echo "hi">/dev/ttyAMA0
it fails as expected, then Ctrl-C and run the script, I get an warning about not being able to set everything,
but it still works, and after quitting the script, running it again there is no warning.
thanks jpollard for for the comments. I had feared that I had to do some "extra modem initialisation", as that is shown in some of the minicom options, but it was not necessary. stty did all that was needed.
Last edited by timsoft; 08-12-2016 at 07:24 AM.
Reason: minor markup error
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.