0

I am a complete newbie...so out of my depth!

I am trying to get a Pi3 to read an ASCI command string sent by a serial device and connect each string to it's matching a bash script located on the Pi.

I have installed PySerial I have successfully read the string from the Serial device named " [LOOP01] " The bash script i want to match it too works on the Pi when i just go to the containing folder and run it.

My problem is i have no idea how to connect the two!

I will have perhaps 10 command strings i will need. I can make them anything i want.

Any help very gratefully received! Thanks

OK, thanks to KentRunner i can run the commands, but just not sure how to format and read the data from the serial device? I need to monitor it constantly. I can create any ASCII string that i like. Do i need to use readline() and use a \n in my command string?

Here is the code so far...

import subprocess
import serial

ser = serial.Serial('/dev/ttyAMA0', baudrate = 9600, timeout=1)


# map of serial input string to bash script you want to run
serialToScriptMap = {
    "S1": "/var/www/sync/startseamless01",
    "S2": "/var/www/sync/startseamless02"}
    # and so on...

# get the serial input.....
#    I'm just reading it from the user on terminal for test
#    You want to use your PySerial code in here to get the serial input into the 'serialInput' variable
serialInput = raw_input("Serial Input: ")

#   Not sure how to do that correctly! 
#   Also idea is that the data coming in to the Pi is monitored continuously

# now use the serialInput variable to lookup the correct script name from the dictionary
scriptToRun = serialToScriptMap.get(serialInput, "INVALID_SERIAL")

if scriptToRun == "INVALID_SERIAL":
     # we didn't recognise the serial input
     print("The serial input was invalid")
else:
    # valid input, run the script
    subprocess.Popen(scriptToRun)
0

Basically you want a dictionary that is keyed on the serial input string and has values of the scripts to run. That allows you to take the serial input and look up the dictionary to get the script name to run.

Code would look something like this:

import subprocess

# map of serial input string to bash script you want to run
serialToScriptMap = {
    "S1": "/home/pi/script1.sh",
    "S2": "/home/pi/script2.sh"}
    # and so on...

# get the serial input.....
#    I'm just reading it from the user on terminal for test
#    You want to use your PySerial code in here to get the serial input into the 'serialInput' variable
serialInput = raw_input("Serial Input: ")

# now use the serialInput variable to lookup the correct script name from the dictionary
scriptToRun = serialToScriptMap.get(serialInput, "INVALID_SERIAL")

if scriptToRun == "INVALID_SERIAL":
     # we didn't recognise the serial input
     print("The serial input was invalid")
else:
    # valid input, run the script
    subprocess.Popen(scriptToRun)
  • Thank for your help KennetRunner! – Pete W Mar 24 '17 at 13:32
  • That has got me on the road! Still a bit lost as i have to continuously monitor the port, and also don't really know how to best read the ASCII data ? Do i use ser.readline() and add a /n to the control string being sent? ie.[LOOP01]\n – Pete W Mar 24 '17 at 15:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.