-   Linux - Software (
-   -   modem tty access in bash (

timsoft 08-11-2016 10:07 AM

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)

stty -F $MODEM speed 9600
while [running]
 if [ condition1 ]; then
  echo "message1" >$MODEM
  if [ condition2 ]; then
    echo "message2" >$MODEM
    read g
    #do something with $g
done <$MODEM

I would like to somehow configure the modem to respond in the script, without having to manually run

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.

jpollard 08-11-2016 12:07 PM

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.

timsoft 08-11-2016 03:57 PM

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

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.

jpollard 08-11-2016 06:26 PM

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.

timsoft 08-12-2016 05:21 AM

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.

jpollard 08-12-2016 06:47 AM

Congratulations. I was wrong about it falling back to the defaults.

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