Reputation: 41
I try to light up an led strip with Python sending the information to the USB port that has an Arduino and led strip on it. I am having problems with the USB port. I don't know which one is the right one and how can I translate this port to Python language.
I found this python code and modified it:
import serial
r = "255"
g = "0"
b = "0"
i = "255"
data = r + "," + g +"," + b + "," + i print(data)
ser = serial.Serial(port='/dev/ttyUSB0',baudrate =
9600,parity=serial.PARITY_NONE,stopbits=serial.STOPBITS_ONE,bytesize=serial.EIGHTBITS,timeout=1)
ser.write(data)
ser.close()
So the problem is I don't know if it is the USB0 I tried from 0 to 3 and didn't work.
I tried lsusb and I can see the Arduino:
Bus 001 Device 005: ID 2341:0043 Arduino SA Uno R3 (CDC ACM)
So it should be the 01? but it was not working then I tried this command: python -m serial.tools.list_ports
and I get these ports
/dev/ttyACM0
/dev/ttyAMA0
Tried to put them in place of port='/dev/ttyUSB0' and get permission error:
Error could not open port /dev/ttyAMA0: [Errno 13] Permission denied: '/dev/ttyAMA0'
solution
sudo chmod 666 /dev/ttyAMA0
And after doing it again I got:
raise TypeError('unicode strings are not supported, please encode to bytes: {!r}'.format(seq))
TypeError: unicode strings are not supported, please encode to bytes: '255,0,0,255'
Before I even continue I am not sure that this is the right USB port. So can someone tell me how can I detect them for example like the lsusb and understand how can I translate it to Python code.
Thank you all.
SOLUTION See last device with this linux command
dmesg | grep "tty"
[ 8.150923] cdc_acm 1-1.1:1.0: ttyACM0: USB ACM device
Upvotes: 0
Views: 419
Reputation: 41
Thank you furas and Mark
furas yes the way to send the data is right. But my main question was about the usb port i was not sure witch one is the right one to place in the code. Mark when i put the command it say can not access there is no such directory.
SOLUTION Found this linux command in internet and they was saying that the last listed one should be the right one so this is the answer:
dmesg | grep "tty"
[ 8.150923] cdc_acm 1-1.1:1.0: ttyACM0: USB ACM device
After seeng that the last tty is ttyACM0 i put port='/dev/ttyACM0' and it works. I have no idea why but is needed the 2 sec sleep. Because if it is less the led is not turning on.
data = r + "," + g + "," + b + "," + "255"
ser = serial.Serial(port='/dev/ttyACM0', baudrate=9600, parity=serial.PARITY_NONE,stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS, timeout=1)
time.sleep(2)
ser.write(data.encode())
ser.close()
Upvotes: 0