import serial
# find your own usb serial port by "ls /dev" and note I changed the Firmata on my Arduino to use 9600 bps
ser=serial.Serial('/dev/ttyUSB0',9600,timeout=1)
# 0x91 is the digital write message (0x90) + "port" 1 (on larger board, there might be more ports), the second two bytes are a bitmask of the digital I/O pins in terms of if they should be on or off
# turn on all digital outputs
ser.write(chr(0x91)+chr(0x7F)+chr(0x7F))
# turn off all digital outputs
ser.write(chr(0x91)+chr(0x00)+chr(0x00))
If that isn't exciting enough, let's enable this over a TCP socket using tcp_serial_redirect.py. One on terminal:
python ./tcp_serial_redirect.py -p /dev/tty.usbserial-A4000QBg -P 6666
--- TCP/IP to Serial redirector --- type Ctrl-C / BREAK to quit
--- /dev/tty.usbserial-A4000QBg 9600,8,N,1 ---
Waiting for connection on 6666...
On the other:
from socket import *
s=socket(AF_INET, SOCK_STREAM)
s.connect(('127.0.0.1',6666))
And you know it is working because terminal 1 will say:
Connected by ('127.0.0.1', 50961)
Then on terminal 2, you can turn the Arduino pin 13 LED on/off with:
s.send(chr(0x91)+chr(0x20)+chr(0))
s.send(chr(0x91)+chr(0x00)+chr(0))
(I should note that for now on I am using BlogTrog CodeWindow for my Python)
No comments:
Post a Comment