Discussion:
sending to serial port from a script?
'Dave B' g8kbvdave-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org [linuxham]
2014-10-22 13:55:34 UTC
Permalink
Hi...

Working with Lubuntu 12.04

So I can "pre-set" something (a KISS capable TNC on a serial port) in a shell
script used to start another program, before that program starts.


After some googling, I found a page at:-
http://stackoverflow.com/questions/8877269
(and other sites with similar info)

Cross referencing what is said there and the other sites with the contens of 'man
stty', would these work reliably in bash scripts?

#!/bin/bash
# kisson.sh To enter KISS mode.
# 9600bd, 8bits, one stop bit, no parity
stty -F /dev/ttyUSB102 speed 9600 cs8 -cstopb -parity
echo -en '\x13' > /dev/ttyUSB102
echo -en 'KISS ON\x13' > /dev/ttyUSB102
echo -en 'RESTART\x13' > /dev/ttyUSB102

(ttyUSB102, is udev'd to point to whatever the OS assigned to the actual
hardware device as it found it. Works well.)

Also...

#!/bin/bash
# kissoff.sh To exit KISS mode.
# 9600bd, 8bits, one stop bit, no parity
stty -F /dev/ttyUSB102 speed 9600 cs8 -cstopb -parity
# 0xc0, 0xff, 0xc0
echo -en '\x192\x255\x192' > /dev/ttyUSB102

I've not had the chance to try this, yet (my sacrificial 'nix box just committed
suicide, hard disk or RAM issues I think. It was not exactly the newest PC on the
floor. Memtest86 shows OK, running Spinrite now, no obvious issues yet.)

What I've not been able to figure out yet, is how to put a short wait (delay) after
each of the echo commands to the port. (Needed I find, to allow the TNC to
chew on things and sort its knickers out after being told to do something!)

I do know that sending it in one hit 'KISSON\rRESTART\r' (\r == cr byte) sends
it into abject raptures, needing a power off/on cycle to get it out of the mess.

This procedure (psudo code)...


Send a single \r, (empty line)

Wait/give time for the tnc to send its default 'cmd:' prompt

Send KISS ON\r

Wait/give time for the tnc to send its default 'cmd:' prompt

Send RESTART\r


Works 100% of the time. I've proven that with Procom+ under DOS! and even
a GWBASIC program! But that's all on DOS, and I'm wanting to do this under
Linux.

I see crN listed in stty's help text, but no clue where to use that function (a delay
of N 'somethings' after a cr byte, nor are the units of the delay specified.)

Any (sensible) suggestions? :)

I've got another hour or two, before I may get the old 'nix box back. Else even
longer before I get to sit in front of another with a real port, not a VM. Or
reload the OS on the old box again.

73.

Dave G0WBX.



------------------------------------
Posted by: "Dave B" <g8kbvdave-gM/Ye1E23mwN+***@public.gmane.org>
------------------------------------
Andy mm0fmf-PkbjNfxxIARBDgjK7y7TUQ@public.gmane.org [linuxham]
2014-10-22 14:57:01 UTC
Permalink
Post by 'Dave B' g8kbvdave-gM/Ye1E23mwN+***@public.gmane.org [linuxham]
#!/bin/bash
Maybe you don't want use bash what with all the recent vulnerabilities. So as this is Lubuntu which is Ubuntu which Debian you should use dash not bash. Or just sh even.
Post by 'Dave B' g8kbvdave-gM/Ye1E23mwN+***@public.gmane.org [linuxham]
What I've not been able to figure out yet, is how to put a short wait (delay) after
sleep n



where n is the number of seconds to sleep.



Andy
MM0FMF
David Ranch linuxham-fld-U76wwbNhhF3R7s880joybQ@public.gmane.org [linuxham]
2014-10-23 04:16:43 UTC
Permalink
Hello Dave,

Which tnc are you working with? Like you, I tried to use stty to get a tnc in/out of kiss mode but it wasn't reliable. I then found a few C programs that work 100% of the time. I wrote the following up and it includes C examples:

http://www.trinityos.com/HAM/CentosDigitalModes/misc/howto-initialize-D710-kiss.txt

I have a few other examples too if you need them.

--David
KI6ZHD
Dave Baxter g8kbvdave-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org [linuxham]
2014-10-23 08:37:45 UTC
Permalink
Thanks Andy.

I "discovered" the "sleep n" function myself yesterday evening, and now
have that all working nicely. Thanks anyway.

(The script syntax in reality is not exactly as I originally quoted either.
I can post the exact version later if anyone wants, I'm on a mobile not at
the PC just now.)

Hint: Use double quotes, and hex values!

echo -en "some command string\x0d" > /dev/ttyUSB0

Re Bash's recent trouble.
Bash is patched now anyway, but it doesn't matter if you script with it or
not, it exists in the system and is needed by other features and functions,
so if there is any problem remaining, it is still vulnerable regardless of
what I use as the script interpreter.

But point taken. Let the updates in!

73.

Dave G0WBX.

Loading...