help talking to arduino using python 3 and pyserial on a raspberrypI

Hi,
I have a small piece of code that can talk to my arduino UNO on win7 64bit - it works fine :slight_smile:
However when I modify it to work on my RaspberryPi (by changing the port name) it doesn't work, but if I open the Arduino IDE and launch the Serial Monitor window then run the code it works, what am I missing?

#! /usr/bin/python3

import serial

#-----------------------------------------------------------------------        
def openSerial (port,baud,bytesize,parity,stopbits,timeout,xonxoff,rtscts,writetimeout,dsrdtr,interchartimeout,message,length):
    try:
        ser = serial.Serial(port,baud,8,parity,stopbits,timeout,xonxoff,rtscts,writetimeout,dsrdtr,interchartimeout)  # open serial port    
        print (ser.portstr+" opened")       # check which port was really used
        print ("Port %s set to %d %s%s%s (%ds timeout)" % (port,baud,bytesize,parity,stopbits,timeout))
        print ("sending '%s'"%message)      
        ser.write(bytes(message, encoding="ascii"))      # write a string
        s=ser.read(length)
        print(str(s,"ascii"))
        ser.close()             # close port
        print ("port closed")
    except serial.SerialException:
        print("failed to open %s"%port)
        pass
    
#-----------------------------------------------------------------------
def main():
    
    #raspberrypi
    openSerial('/dev/ttyACM0',9600,8,serial.PARITY_NONE,1,2,False,True,2,False,None,'^WHORU

output I am expecting (and get on Windows and also get if I have the 'Serial Monitor' window open on the RaspberryPi).

>>> 
COM3 opened
Port COM3 set to 9600 8N1 (2s timeout)
sending '^WHORU

when the 'Serial Monitor' window from the IDE is closed I get a timeout to the ser.read(length) command i.e.

COM3 opened
Port COM3 set to 9600 8N1 (2s timeout)
sending '^WHORU

I am new to Python and linux so hoping it is something obvious, but I haven't been able to find any help whilst googling that fits the problem I have.
from the terminal window on the RPi I have used the command

stty -F /dev/ttyACM0 -a

to see how the serial port is set-up, I have done this with and without the IDE 'Serial Monitor' window open but can't find any differences, also If I change settings in the python code such as the speed and run stty it does reflect the changes. So I am at a real loss what to do and hoping someone can provide some suggestions.
Thanks!

Simon,11)
    #windows
    #openSerial('COM3',9600,8,serial.PARITY_NONE,1,2,False,False,2,True,None,'^WHORU


output I am expecting (and get on Windows and also get if I have the 'Serial Monitor' window open on the RaspberryPi).

§DISCOURSE_HOISTED_CODE_1§


when the 'Serial Monitor' window from the IDE is closed I get a timeout to the ser.read(length) command i.e.

§DISCOURSE_HOISTED_CODE_2§


I am new to Python and linux so hoping it is something obvious, but I haven't been able to find any help whilst googling that fits the problem I have.
from the terminal window on the RPi I have used the command

§DISCOURSE_HOISTED_CODE_3§


to see how the serial port is set-up, I have done this with and without the IDE 'Serial Monitor' window open but can't find any differences, also If I change settings in the python code such as the speed and run stty it does reflect the changes. So I am at a real loss what to do and hoping someone can provide some suggestions.
Thanks!

Simon,11)

#-----------------------------------------------------------------------
if __name__=="__main__":
    main()

output I am expecting (and get on Windows and also get if I have the 'Serial Monitor' window open on the RaspberryPi).

§_DISCOURSE_HOISTED_CODE_1_§

when the 'Serial Monitor' window from the IDE is closed I get a timeout to the ser.read(length) command i.e.

§_DISCOURSE_HOISTED_CODE_2_§

I am new to Python and linux so hoping it is something obvious, but I haven't been able to find any help whilst googling that fits the problem I have.
from the terminal window on the RPi I have used the command

§_DISCOURSE_HOISTED_CODE_3_§

to see how the serial port is set-up, I have done this with and without the IDE 'Serial Monitor' window open but can't find any differences, also If I change settings in the python code such as the speed and run stty it does reflect the changes. So I am at a real loss what to do and hoping someone can provide some suggestions.
Thanks!

Simon
^OK:WHORUHB
port closed


when the 'Serial Monitor' window from the IDE is closed I get a timeout to the ser.read(length) command i.e.

§DISCOURSE_HOISTED_CODE_2§


I am new to Python and linux so hoping it is something obvious, but I haven't been able to find any help whilst googling that fits the problem I have.
from the terminal window on the RPi I have used the command

§DISCOURSE_HOISTED_CODE_3§


to see how the serial port is set-up, I have done this with and without the IDE 'Serial Monitor' window open but can't find any differences, also If I change settings in the python code such as the speed and run stty it does reflect the changes. So I am at a real loss what to do and hoping someone can provide some suggestions.
Thanks!

Simon,11)
    #windows
    #openSerial('COM3',9600,8,serial.PARITY_NONE,1,2,False,False,2,True,None,'^WHORU

output I am expecting (and get on Windows and also get if I have the 'Serial Monitor' window open on the RaspberryPi).

§_DISCOURSE_HOISTED_CODE_1_§

when the 'Serial Monitor' window from the IDE is closed I get a timeout to the ser.read(length) command i.e.

§_DISCOURSE_HOISTED_CODE_2_§

I am new to Python and linux so hoping it is something obvious, but I haven't been able to find any help whilst googling that fits the problem I have.
from the terminal window on the RPi I have used the command

§_DISCOURSE_HOISTED_CODE_3_§

to see how the serial port is set-up, I have done this with and without the IDE 'Serial Monitor' window open but can't find any differences, also If I change settings in the python code such as the speed and run stty it does reflect the changes. So I am at a real loss what to do and hoping someone can provide some suggestions.
Thanks!

Simon,11)

#-----------------------------------------------------------------------
if name=="main":
    main()


output I am expecting (and get on Windows and also get if I have the 'Serial Monitor' window open on the RaspberryPi).

§DISCOURSE_HOISTED_CODE_1§


when the 'Serial Monitor' window from the IDE is closed I get a timeout to the ser.read(length) command i.e.

§DISCOURSE_HOISTED_CODE_2§


I am new to Python and linux so hoping it is something obvious, but I haven't been able to find any help whilst googling that fits the problem I have.
from the terminal window on the RPi I have used the command

§DISCOURSE_HOISTED_CODE_3§


to see how the serial port is set-up, I have done this with and without the IDE 'Serial Monitor' window open but can't find any differences, also If I change settings in the python code such as the speed and run stty it does reflect the changes. So I am at a real loss what to do and hoping someone can provide some suggestions.
Thanks!

Simon

port closed

I am new to Python and linux so hoping it is something obvious, but I haven't been able to find any help whilst googling that fits the problem I have.
from the terminal window on the RPi I have used the command

§_DISCOURSE_HOISTED_CODE_3_§

to see how the serial port is set-up, I have done this with and without the IDE 'Serial Monitor' window open but can't find any differences, also If I change settings in the python code such as the speed and run stty it does reflect the changes. So I am at a real loss what to do and hoping someone can provide some suggestions.
Thanks!

Simon,11)
    #windows
    #openSerial('COM3',9600,8,serial.PARITY_NONE,1,2,False,False,2,True,None,'^WHORU


output I am expecting (and get on Windows and also get if I have the 'Serial Monitor' window open on the RaspberryPi).

§DISCOURSE_HOISTED_CODE_1§


when the 'Serial Monitor' window from the IDE is closed I get a timeout to the ser.read(length) command i.e.

§DISCOURSE_HOISTED_CODE_2§


I am new to Python and linux so hoping it is something obvious, but I haven't been able to find any help whilst googling that fits the problem I have.
from the terminal window on the RPi I have used the command

§DISCOURSE_HOISTED_CODE_3§


to see how the serial port is set-up, I have done this with and without the IDE 'Serial Monitor' window open but can't find any differences, also If I change settings in the python code such as the speed and run stty it does reflect the changes. So I am at a real loss what to do and hoping someone can provide some suggestions.
Thanks!

Simon,11)

#-----------------------------------------------------------------------
if __name__=="__main__":
    main()

output I am expecting (and get on Windows and also get if I have the 'Serial Monitor' window open on the RaspberryPi).

§_DISCOURSE_HOISTED_CODE_1_§

when the 'Serial Monitor' window from the IDE is closed I get a timeout to the ser.read(length) command i.e.

§_DISCOURSE_HOISTED_CODE_2_§

I am new to Python and linux so hoping it is something obvious, but I haven't been able to find any help whilst googling that fits the problem I have.
from the terminal window on the RPi I have used the command

§_DISCOURSE_HOISTED_CODE_3_§

to see how the serial port is set-up, I have done this with and without the IDE 'Serial Monitor' window open but can't find any differences, also If I change settings in the python code such as the speed and run stty it does reflect the changes. So I am at a real loss what to do and hoping someone can provide some suggestions.
Thanks!

Simon
^OK:WHORUHB
port closed


when the 'Serial Monitor' window from the IDE is closed I get a timeout to the ser.read(length) command i.e.

§DISCOURSE_HOISTED_CODE_2§


I am new to Python and linux so hoping it is something obvious, but I haven't been able to find any help whilst googling that fits the problem I have.
from the terminal window on the RPi I have used the command

§DISCOURSE_HOISTED_CODE_3§


to see how the serial port is set-up, I have done this with and without the IDE 'Serial Monitor' window open but can't find any differences, also If I change settings in the python code such as the speed and run stty it does reflect the changes. So I am at a real loss what to do and hoping someone can provide some suggestions.
Thanks!

Simon,11)
    #windows
    #openSerial('COM3',9600,8,serial.PARITY_NONE,1,2,False,False,2,True,None,'^WHORU

output I am expecting (and get on Windows and also get if I have the 'Serial Monitor' window open on the RaspberryPi).

§_DISCOURSE_HOISTED_CODE_1_§

when the 'Serial Monitor' window from the IDE is closed I get a timeout to the ser.read(length) command i.e.

§_DISCOURSE_HOISTED_CODE_2_§

I am new to Python and linux so hoping it is something obvious, but I haven't been able to find any help whilst googling that fits the problem I have.
from the terminal window on the RPi I have used the command

§_DISCOURSE_HOISTED_CODE_3_§

to see how the serial port is set-up, I have done this with and without the IDE 'Serial Monitor' window open but can't find any differences, also If I change settings in the python code such as the speed and run stty it does reflect the changes. So I am at a real loss what to do and hoping someone can provide some suggestions.
Thanks!

Simon,11)

#-----------------------------------------------------------------------
if name=="main":
    main()


output I am expecting (and get on Windows and also get if I have the 'Serial Monitor' window open on the RaspberryPi).

§DISCOURSE_HOISTED_CODE_1§


when the 'Serial Monitor' window from the IDE is closed I get a timeout to the ser.read(length) command i.e.

§DISCOURSE_HOISTED_CODE_2§


I am new to Python and linux so hoping it is something obvious, but I haven't been able to find any help whilst googling that fits the problem I have.
from the terminal window on the RPi I have used the command

§DISCOURSE_HOISTED_CODE_3§


to see how the serial port is set-up, I have done this with and without the IDE 'Serial Monitor' window open but can't find any differences, also If I change settings in the python code such as the speed and run stty it does reflect the changes. So I am at a real loss what to do and hoping someone can provide some suggestions.
Thanks!

Simon

Hi, allow me to repeat what you said, just to be sure.
If I understand correctly, you installed the IDE on R-Pi as well as on Windows, and when you open serial monitor on the R-Pi, and then launch the python script on the same R-Pi, everything works as on Windows. On the other hand, if you don't open serial monitor the python script cannot communicate with the Arduino.

Now, this should be the R-Pi log without serial monitor:

COM3 opened
Port COM3 set to 9600 8N1 (2s timeout)
sending '^WHORU

but it shows COM3. So you have the same problem on Windows?

As for the parameters

    #raspberrypi
    openSerial('/dev/ttyACM0',9600,8,serial.PARITY_NONE,1,2,False,True,2,False,None,'^WHORU

rtscts and dsrdtr are different. These are control parameters that may affect the initiation of serial communications. Is the difference intentional? Did you try other combinations?

port closed


but it shows COM3. So you have the same problem on Windows?

As for the parameters

§DISCOURSE_HOISTED_CODE_1§


rtscts and dsrdtr are different. These are control parameters that may affect the initiation of serial communications. Is the difference intentional? Did you try other combinations?
,11)
    #windows
    #openSerial('COM3',9600,8,serial.PARITY_NONE,1,2,False,False,2,True,None,'^WHORU

rtscts and dsrdtr are different. These are control parameters that may affect the initiation of serial communications. Is the difference intentional? Did you try other combinations?

port closed


but it shows COM3. So you have the same problem on Windows?

As for the parameters

§DISCOURSE_HOISTED_CODE_1§


rtscts and dsrdtr are different. These are control parameters that may affect the initiation of serial communications. Is the difference intentional? Did you try other combinations?
,11)

rtscts and dsrdtr are different. These are control parameters that may affect the initiation of serial communications. Is the difference intentional? Did you try other combinations?

port closed


but it shows COM3. So you have the same problem on Windows?

As for the parameters

§DISCOURSE_HOISTED_CODE_1§


rtscts and dsrdtr are different. These are control parameters that may affect the initiation of serial communications. Is the difference intentional? Did you try other combinations?

This may help: try time.sleep(2) after opening the serial port and before writing to it. This will allow the bootloader to time out and ensure the message goes to your program instead of the bootloader.

-br

Thanks for the replies.

RESOLVED
adding sleep.time(2) after opening the port worked thanks for the suggestion :slight_smile:
RESOLVED

The reason the dtrdsr parameter is different in windows and linux baffles me, but I couldn't get the code to work without setting dsrdtr in windows but didn't have this problem on the RPi with the IDE 'Serial Monitor' window open. The rtscts being different was the result of lost of testing for different combinations, I should have set this back to the same as on windows.

here is the working code on both platforms

#! /usr/bin/python3

import serial
import time
#-----------------------------------------------------------------------        
def openSerial (port,baud,bytesize,parity,stopbits,timeout,xonxoff,rtscts,writetimeout,dsrdtr,interchartimeout,message,length):
    try:
        ser = serial.Serial(port,baud,8,parity,stopbits,timeout,xonxoff,rtscts,writetimeout,dsrdtr,interchartimeout)  # open serial port    
        time.sleep(2) # required on RPi to allow the bootloader to time out
        print (ser.portstr+" opened")       # check which port was really used
        print ("Port %s set to %d %s%s%s (%ds timeout)" % (port,baud,bytesize,parity,stopbits,timeout))
        print ("sending '%s'"%message)      
        ser.write(bytes(message, encoding="ascii"))      # write a string
        s=ser.read(length)
        print(str(s,"ascii"))
        ser.close()             # close port
        print ("port closed")
    except serial.SerialException:
        print("failed to open %s"%port)
        pass
    
#-----------------------------------------------------------------------
def main():
    
    #raspberrypi
    openSerial('/dev/ttyACM0',9600,8,serial.PARITY_NONE,1,2,False,False,2,True,None,'^WHORU

Thanks
Simon,11)
    #windows
    #openSerial('COM3',9600,8,serial.PARITY_NONE,1,2,False,False,2,True,None,'^WHORU


Thanks
Simon,11)

#-----------------------------------------------------------------------
if __name__=="__main__":
    main()

Thanks
Simon