I have an Arduino microcontroller listening on COM3. Using the arduino IDE and the Serial monitor works fine to send and receive data.
I would like to send and receive data from Python, but it’s not immediately obvious how to do so. (I’d also be fine doing it in C# if it was substantially easier.)
I found arduino_serial.py, but it only works for Unix. Fortunately, I have a Ubuntu 10.10 VBox set up. However, I have no idea if that VM can access serial ports or if special steps are required to do so.
I also found pySerial, which looks pretty legitimate. However, I’m also unsure how to use it. It wants serial port names. How do I find out what valid values for these are?
For example, pySerial mentions that you can “Open 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 that /dev/ttyS1 was a valid port name.
Is there good documentation for getting started on this?
Update: I’m using Ubuntu with arduino_serial, but still having trouble.
This program is running on the Arduino:
void setup() {
Serial.begin(9600);
}
void loop() {
if (Serial.available()) {
Serial.print((char)Serial.read());
}
}
I see that a port called tty0 is available:
foo@bar:~/baz$ dmesg | grep tty
[ 0.000000] console [tty0] enabled
I then try to connect with 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?
I have a project called Yaam on CodePlex that uses C# to send data through the serial port. Check that out for an example. On the C# side (see Yaam\Yaam.xaml.cs), simply use the
SerialPortclass in theSystem.IO.Portsnamespace. Once you instantiate the object and set the properties (baud rate, com port, etc), simply call.Open(). There are also plenty of other examples on the web. Take a look at these: