Skip to content

Instantly share code, notes, and snippets.

@projectweekend
Last active October 4, 2023 02:22
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • 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()
@cancelradius
Copy link

Helpful!

@acastanet
Copy link

Thanks!

@SiddheshNan
Copy link

Thanks a lot!

@sdrshnptl
Copy link

Did the job! thanks!

@Sakib1263
Copy link

Nice

@satishgoda
Copy link

Thank you so much.

@Persing2019
Copy link

boa

@Shasmart
Copy link

Thank You so much

@brazen-paradox
Copy link

It is a nice code. I have added few more lines to let the users input the desired device location and baud rate. Feel free to update the same from my fork. happy coding!

@rgf1289
Copy link

rgf1289 commented Nov 7, 2020

Thank you

@HALxmont
Copy link

Thank you!

@biplabro
Copy link

Thank you very much.

@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