Search code examples
pythonpython-3.xarduino

Read and send multiple lines instead of one


I'm having an Arduino setup with a mini displayed. It's programmed so whatever I type into the serial "command line" will be printed to the display.

Foods.txt:

First Line

This works fine if the txt file only has one Line

import serial
import tim

f = open("Foods.txt", "r")
lines = f.readline()

for line in lines:
    ser = serial.Serial("/dev/cu.SLAB_USBtoUART", 115200, timeout=1)
    commandToSend = lines
    ser.write(str(commandToSend).encode())

Output is this this:

First Line

If I add lines to "Foods.txt":

First Line
Second Line
Third Line
Fourth Line

I get this output (first line 11 times):

First Line
First Line
First Line
First Line
First Line
First Line
First Line
First Line
First Line
First Line
First Line

If I change f.readlines() to lines instead of line in order to read multiple lines one by one, it also kind of works but I got the problem that it will not add the carriage return to each line (\r) so after using the script nothing happens

import serial
import time


f = open("Foods.txt", "r")
lines = f.readlines()

for line in lines:

    ser = serial.Serial("/dev/cu.SLAB_USBtoUART", 115200, timeout=1)
    commandToSend = lines
    ser.write(str(commandToSend).encode())

So I Screen into the device and press ender and I get

["first Line\n", "Second Line\n". "Third Line\n","Fourth Line\n"]
["first Line\n", "Second Line\n". "Third Line\n","Fourth Line\n"]
["first Line\n", "Second Line\n". "Third Line\n","Fourth Line\n"].   
["first Line\n", "Second Line\n". "Third Line\n","Fourth Line\n"]

Which means that it send them to the serial console but it didn't press "enter" to send them and as soon as I ran the first script it pressed enter for all of it.

How do I add \r to it?

If I do it like this:

import serial
import time


f = open("Foods.txt", "r")
lines = f.readlines()

for line in lines:

    ser = serial.Serial("/dev/cu.SLAB_USBtoUART", 115200, timeout=1)
    commandToSend = lines\r
    ser.write(str(commandToSend).encode())

I get

    commandToSend = lines\r
                          ^
    SyntaxError: unexpected character after line continuation character

Solution

  • The second approach looks right to me, the problem is that you are setting commandToSend to lines, which is a list of all the lines, what you want to send instead is line (the current line in the list you are looping on).

    So your code should be:

    import serial
    import time
    
    f = open("Foods.txt", "r")
    lines = f.readlines()
    
    for line in lines:
        ser = serial.Serial('/dev/cu.SLAB_USBtoUART', 115200, timeout = 1)
        commandToSend = line
        ser.write(str(commandToSend).encode()).
    

    Also, if you wanted to add a carriage return, you would have to concatenate it as a string, eg:

    line + '\r'
    

    or

    '%s\r' % line
    

    or

    '{}\r'.format(line)
    

    or

    f'{line}\r'
    

    Depending on preferences / Python version