Skip to content

Instantly share code, notes, and snippets.

@projectweekend
Last active October 4, 2023 02:22
Show Gist options
  • Save projectweekend/1fae5a8cf2a5b9282f3d to your computer and use it in GitHub Desktop.
Save projectweekend/1fae5a8cf2a5b9282f3d to your computer and use it in GitHub Desktop.
Reading from a serial port in Python
import serial
# this port address is for the serial tx/rx pins on the GPIO header
SERIAL_PORT = '/dev/ttyAMA0'
# be sure to set this to the same rate used on the Arduino
SERIAL_RATE = 9600
def main():
ser = serial.Serial(SERIAL_PORT, SERIAL_RATE)
while True:
# using ser.readline() assumes each line contains a single reading
# sent using Serial.println() on the Arduino
reading = ser.readline().decode('utf-8')
# reading is a string...do whatever you want from here
print(reading)
if __name__ == "__main__":
main()
@HamidSyed298
Copy link

I am trying to read data from Emlid RS2+ via RS232 port baudrate = 115200. My code just gets stuck in "ser.readlines()" line.
I am using Jetson Orin. Any suggestions?

import serial
Serialport = '/dev/ttyACM0'
#Serialport = '/dev/serial/by-id/usb-Emlid_ReachRS2_8243D27C83B34ACE-if02'
ser_rate = 115200
def main():
ser = serial.Serial(Serialport,ser_rate)
while True:
reading = ser.readlines().decode('utf-8')
print(reading)

if name == "main":
main()

I can also provide further details on it if needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment