I have an Arduino microcontroller listening on COM3. Using the arduino IDE and serial monitor works great for sending and receiving data.
I would like to send and receive data with Python, but it is not immediately clear how to do this. (I would do it in C # too, if it were much simpler.)
I found arduino_serial.py , but it works only for Unix. Fortunately, I have Ubuntu 10.10 VBox installed. However, I do not know if this virtual machine can access the serial ports or if special steps are required for this.
I also found pySerial that looks pretty legit. However, I am also not sure how to use it. He wants serial port names. How to find out what are the actual values for them?
For example, pySerial
it mentions that you can "Open the named port at" 19200.8, N, 1, 1s timeout "with the following command:
>>> ser = serial.Serial('/dev/ttyS1', 19200, timeout=1)
But I have no idea how you would know what /dev/ttyS1
is a valid port name.
Is there any good documentation to get you started?
Update . I am using Ubuntu with arduino_serial, but still have problems.
This program runs on Arduino:
void setup() {
Serial.begin(9600);
}
void loop() {
if (Serial.available()) {
Serial.print((char)Serial.read());
}
}
I see that a port is available with the name tty0
:
foo@bar:~/baz$ dmesg | grep tty
[ 0.000000] console [tty0] enabled
Then I try to connect to arduino_serial
:
foo@bar:~/baz$ sudo python
[sudo] password for foo:
Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import arduino_serial
>>> sp = arduino_serial.SerialPort("/dev/tty0", 9600)
>>> sp.write("foo")
>>> sp.read_until("\n")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "arduino_serial.py", line 107, in read_until
n = os.read(self.fd, 1)
OSError: [Errno 11] Resource temporarily unavailable
Why am I getting this error? What am I doing wrong?