LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-02-2022, 03:41 PM   #1
Wawa
LQ Newbie
 
Registered: Oct 2009
Location: Alsace - Yooooo !
Distribution: Kubuntu
Posts: 17

Rep: Reputation: 1
Bash and serial port


Hello there !

I try to use bash for send datas to an external computer.

I have try this :

Code:
#!/bin/bash

file=$1
port="/dev/ttyUSB0"

while IFS= read -r line
do
	echo -n "$line" > $port
	sleep 0.5
done < "$file"
This work well but is to slow.

As soon the external computer has get the datas and made his job he send a single ">" (no crlf, no eol, etc...) to the serial port.
I think I can read the serial port and as soon as I get a ">" send the next line.

So I try

Code:
#!/bin/bash

file=$1
port="/dev/ttyUSB0"

while IFS= read -r line
do
	echo -n "$line" > $port
	while read -N1 char; do
	 	echo $char # just for debug purpose
	done < $port
done < "$file"
First problem I see is that the "read" instruction read again the data which just has been send by the previous line. So if by hazard the send data include a ">" the program will fail... Also this seems to remove datas from the serial port buffer.

And the "while read... done < $port" look to be an endless loop.

How could I change the code to do the job ?

Thanks for the help.
Philippe

Last edited by Wawa; 11-02-2022 at 06:03 PM. Reason: Add informations
 
Old 11-02-2022, 06:58 PM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 23,677

Rep: Reputation: 5184Reputation: 5184Reputation: 5184Reputation: 5184Reputation: 5184Reputation: 5184Reputation: 5184Reputation: 5184Reputation: 5184Reputation: 5184Reputation: 5184
IMHO bash is not the best language for accessing serial port data and can be unreliable. Here is a quick python script. Change serial port parameters as necessary. It will wait for a single character response and then continue sending data. The serial module is not installed by default.

Code:
#!/usr/bin/python3

import serial
import sys

if len(sys.argv) == 1:
    print("Missing command line argument")
    quit()

myfile=sys.argv[1]

serialport=serial.Serial(port="/dev/ttyUSB0",baudrate=9600,bytesize=8,timeout=2,stopbits=serial.STOPBITS_ONE)

file1 = open(myfile, 'r')
while True:
    # Get next line from file
    line = file1.readline()
    serialport.write(line.encode())
    # if line is empty
    # end of file is reached
    if not line:
        break
    while (serialport.in_waiting == 0):
          pass
file1.close()
 
2 members found this post helpful.
Old 11-03-2022, 02:27 AM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,387
Blog Entries: 1

Rep: Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665
@OP I guess you should open the device only once, e.g.
Code:
#!/bin/bash

file="$1"
port="/dev/ttyUSB0"
exec 7<>"$port"
# stty <&7 some-serial-options

while IFS= read -r line
do
    echo -n "$line" >&7
    while read -u7 -N1 char; do
        echo $char # just for debug purpose
    done
done <"$file"
 
Old 11-03-2022, 10:38 AM   #4
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,007
Blog Entries: 4

Rep: Reputation: 3607Reputation: 3607Reputation: 3607Reputation: 3607Reputation: 3607Reputation: 3607Reputation: 3607Reputation: 3607Reputation: 3607Reputation: 3607Reputation: 3607
I heartily agree with @michaelk: "use a real language." Also, avail yourself fully of the many packages that are available for use in those languages.

Quote:
Originally Posted by wisdom:

Actum Ne Agas: Do Not Do A Thing Already Done.
If you mark the script file as "executable" and it begins with a so-called "shebang" #! in the first line, specifying the location of a language interpreter, then that interpreter will silently be invoked to run the script and the user will be none the wiser. (The env command exists to make this easier.)

While "bash" has very rudimentary scripting capability, it never was intended to be used for "real work." (Only the Korn shell, "ksh," had a serious built-in language.) The elegantly-simple "shebang" mechanism means that it doesn't need to.
 
  


Reply


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
How to Communicate Virtual Serial Port on Host(windows) to Communicate with Virtual Serial Port on Guest System(Linux) aquamarine Linux - Newbie 2 09-16-2016 03:48 PM
[SOLVED] Serial terminal connected to USB Serial Port on Ubuntu 12.04 809areacode Linux - General 3 07-18-2013 11:06 PM
Is a USBtty (USB serial port) treated the same as tty (normal serial port) in C? spudgunner Programming 1 11-12-2010 02:19 PM
Bash Sleep vs crontab and bash serial port nutthick Programming 4 06-01-2006 03:42 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:13 PM.

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