Created
July 15, 2021 15:02
-
-
Save mjdargen/ca20b12c090d9ac8d9eef68d792c3348 to your computer and use it in GitHub Desktop.
Python serial to arduino
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import time | |
import serial | |
# | |
# | |
# specify the port | |
PORT = "com6" # windows | |
# PORT = "/dev/ttyACM0" # mac/linux | |
BAUD = 9600 # baud rate | |
# connect to serial port | |
try: | |
ser = serial.Serial(PORT, BAUD , timeout=1) | |
except (OSError, serial.SerialException): | |
print(f"Arduino not connected to {PORT}") | |
sys.exit(1) | |
ser.flush() | |
# | |
# | |
# sending/receiving messages to/from Arduino | |
# delay to make sure it was able to establish connection | |
time.sleep(2) | |
# define and encode message | |
msg = "This is the message going to the Arduino." | |
msg = msg.encode('utf-8') | |
# send message | |
ser.write(msg) | |
# receive message | |
response = ser.readline().decode('utf-8') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment