LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This 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


Reply
  Search this Thread
Old 08-11-2016, 09:07 AM   #1
timsoft
Member
 
Registered: Oct 2004
Location: scotland
Distribution: slackware 15.0 64bit, 14.2 64 and 32bit and arm, ubuntu and rasbian
Posts: 483

Rep: Reputation: 127Reputation: 127
Question 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.
 
Old 08-11-2016, 11:07 AM   #2
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
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.
 
Old 08-11-2016, 02:57 PM   #3
timsoft
Member
 
Registered: Oct 2004
Location: scotland
Distribution: slackware 15.0 64bit, 14.2 64 and 32bit and arm, ubuntu and rasbian
Posts: 483

Original Poster
Rep: Reputation: 127Reputation: 127
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.
 
Old 08-11-2016, 05:26 PM   #4
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
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.

Last edited by jpollard; 08-11-2016 at 05:27 PM.
 
Old 08-12-2016, 04:21 AM   #5
timsoft
Member
 
Registered: Oct 2004
Location: scotland
Distribution: slackware 15.0 64bit, 14.2 64 and 32bit and arm, ubuntu and rasbian
Posts: 483

Original Poster
Rep: Reputation: 127Reputation: 127
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
 
Old 08-12-2016, 05:47 AM   #6
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Congratulations. I was wrong about it falling back to the defaults.
 
  


Reply

Tags
bash, radio, raspberrry pi zero


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
[SOLVED] gain access to tty Deus Absconditus Linux - Newbie 3 09-27-2015 09:28 AM
/bin/bash cant access tty linuxmandrake Ubuntu 1 01-28-2006 03:38 PM
modem / tty prob after a rebuild edM Slackware 4 08-08-2004 06:10 PM
Can't access tty otisthegbs Linux From Scratch 0 06-06-2004 05:53 PM
Bash: How to tell a tty from a pts? m0rl0ck Programming 4 08-21-2003 06:26 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 05:42 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
Open Source Consulting | Domain Registration