Last active
March 17, 2025 13:53
-
-
Save Robotto/48cdc0eb75ccee3a306866d634f76f77 to your computer and use it in GitHub Desktop.
Serial TX from python to arduino (sending raw bytes) - Plays well with this gist: https://gist.github.com/Robotto/55494b012af0c0f9c9d7c048066c9290
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 serial #Pakken hedder pyserial | |
import sys | |
import glob | |
import time | |
def list_ports(): | |
""" Finds all serial ports and returns a list containing them | |
:raises EnvironmentError: | |
On unsupported or unknown platforms | |
:returns: | |
A list of the serial ports available on the system | |
""" | |
if sys.platform.startswith('win'): | |
ports = ['COM%s' % (i + 1) for i in range(254)] | |
elif sys.platform.startswith('linux') or sys.platform.startswith('cygwin'): | |
# this excludes your current terminal "/dev/tty" | |
ports = glob.glob('/dev/tty[A-Za-z]*') | |
elif sys.platform.startswith('darwin'): | |
ports = glob.glob('/dev/tty.*') | |
else: | |
raise EnvironmentError('Unsupported platform') | |
result = [] | |
for port in ports: | |
try: | |
s = serial.Serial(port) # Try to open a port | |
s.close() # Close the port if sucessful | |
del s #delete the port object | |
result.append(port) # Add to list of good ports | |
except (OSError, Exception): # If un sucessful | |
pass | |
return result | |
baudRate=115200 | |
#foundPorts = list_ports() | |
#for port in foundPorts: | |
# print(f'Found port: {port} (#{foundPorts.index(port)})') | |
#choice = int(input('Which #? to open:')) | |
#s = serial.Serial(foundPorts[choice], baudRate, timeout=1) | |
s = serial.Serial("COM30", baudRate, timeout=1) | |
txTime=time.time() | |
position = 0 | |
delta = 1 | |
while(s.is_open): | |
if time.time() - txTime > 0.025: | |
#position = (position + 1)%180 | |
position = position + delta | |
if position >= 180: | |
delta = -1 | |
if position == 0: | |
delta = 1 | |
s.write(bytes([position])) | |
#print(position) | |
txTime=time.time() | |
if(s.in_waiting>0): | |
rxLine=s.readline().decode("ascii").strip() | |
print(rxLine) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Arduino code here: https://gist.github.com/Robotto/55494b012af0c0f9c9d7c048066c9290