Unable to echo to serial port from a bash script?
I am working on a small bash script, which involves going through devices on several serial ports, sending them a message and listening for their response. What I am using so far is:
Code:
for d in $(ls /dev | grep -E 'ttyUSB|ttyACM') ; Unfortunately, when I launch the script, it seems like nothing is returned from the device - the "testing /dev/ttyUSB0" gets echoed, but after that, I just get an empty line, and get thrown back to the prompt. When I try to execute the same commands manually - namely the (read -n32 -t5 resp < /dev/ttyUSB0 ; echo $resp)& and then echo "id" > /dev/ttyUSB0, I do get a response, however, it is not always the affirmative one. So far it seems like the first attempt gets an affirmative, then the next one doesn't unless I echo anything else to the serial port before running read. Some help in getting the script to work would be appreciated. |
Comments on your script
Code:
#This is ok Code:
d="sda" |
There are a bunch of syntax errors in your script.
I am not a big fan of using bash for serial communications but I found something that works fairly well using file descriptors for a simple test. Untested... Code:
#!/bin/bash |
Quote:
I do suspect there might be some problems with the termination of what gets sent, TBF, since sending the chars manually from a serial monitor works just fine (i, d, enter - the device on the other end will consider either \n, \r or \0 to signify an end of a message) |
Any language that has a serial port library like c or python. In this case it does not matter but the d '' change the end of line character from a newline to nothing.
try: Code:
echo -e "id\n\r" >&5 |
Quote:
Code:
help read Quote:
Code:
for d in $(ls /dev | grep 'tty'); do |
By the way you can echo/redirect to a variable if it is a file or a character device etc.
Code:
var=test.txt |
Ok, so I managed to cobble something together, shamelessly stealing from several examples and possibly following every bad practice under the sun.
This is what I managed: find_thing.sh (run as . find_thing.sh): Code:
#!/bin/bash Code:
send "" # didn't work if I removed either of these I do suppose though that I could at least implement some kind of checking for accepted outputs - maybe in the minicom script, although that was...interesting to work with. |
There are ways via udev to automatically assign a device ID to the same adapter.
If what you have works for you good. I had to do something similar with some test equipment many years ago using Labview using windows. |
All times are GMT -5. The time now is 04:18 PM. |