Python Forum

Full Version: Trouble writing over serial
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello all,

I am trying to get my BeagleBone Black to communicate with a Microcontroller (CC1310 Launchpad) over serial.
When I connect the Microcontroller to Windows, I can Use Putty and make a serial connection to (For example COM 13 and baud rate 9600) and I send "90000" it will send back something through the terminal window.
Now I am trying to do the same but with a Python program. Here is my attempt:
import datetime
import serial
import os

def pwr_solenoid(solenoid0=0, solenoid1=0, solenoid2=0, solenoid3=0):
    # Defaults are for low signal values

    # compile output
    output = '9{solenoid0}{solenoid1}{solenoid2}{solenoid3}' \
        .format(solenoid0=solenoid0, solenoid1=solenoid1, solenoid2=solenoid2, solenoid3=solenoid3).encode()

    with serial.Serial('/dev/ttyACM0', baudrate=9600) as ser:
        print("created connection to '/dev/tty/ACM0'\n")
        print("going to send:\t'{}'".format(output))
        ser.close()   # I don't think you need this, but you would need to double check

    # for testing to console

def read_from_uart():
    # read from the serial connection
        with serial.Serial('/dev/ttyACM0') as ser:
                if not ser.in_waiting:
                    raise Exception("No Data")

                val = ser.readline()
                data = "{dts}\t\t{val}\n".format(dts=datetime.datetime.utcnow(), val=val)
                # output to file
                filename = "{name}.txt".format(name=datetime.datetime.utcnow().strftime("%a%d%m%Y"))
                with open(filename, "+w") as file:
                print("data received: {data}".format(data=data))

            except (serial.SerialException, FileNotFoundError):
                print("Serialexception, mocking data *****")
                val = "85"
                data = "{dts}\t\t{val}\n".format(dts=datetime.datetime.utcnow(), val=val)
                print("data received: {data}".format(data=data))
                print("Unexpected error")
    except (serial.SerialException, FileNotFoundError):
        print("Serialexception, mocking data *****")
        val = "85"
        data = "{dts}\t\t{val}\n".format(dts=datetime.datetime.utcnow(), val=val)
        print("data received: {data}".format(data=data))

def main():

    wait = True
    print("Enter solenoid values in form of 1/0 (1 == HIGH, 0 == LOW) or 'exit' to exit\n")
    print("default value will remain '0000' until altered...\n")
    print("Note, after altered values are input, it will wait for input from UART||USB\n")

    solenoid = '0000'
    while wait:
        solenoid = input("please input solenoid signal values.\n")

        if solenoid.lower() == "exit":
        elif solenoid.lower() == "ports":
            ports = [tuple(p) for p in list(]
        elif solenoid.isdigit():
            # checks to make sure all characters are digits (does not check for correctness of
            # individual characters
            pwr_solenoid(solenoid0=solenoid[0:1], solenoid1=solenoid[1:2], solenoid2=solenoid[2:3], solenoid3=solenoid[3:4])

if __name__ == "__main__":
The program runs up to the point where it prints the output that is to be written over Serial , then says unexpected error, then prompts me to enter another input again. Any help would be appreciated there seems to be no syntax error in the code.

you get "Unexpected error" from exception handler in line #46.
Could you try to print any meaningful data/variables, before the exception happens?