Serial ports on Windows or Ubuntu VBox to talk with Arduino with Python

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, pySerialit 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/ttyS1is 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?

+3
4

pySerial Python. , , pySerial - .

, Arduino COM3, :

import serial
ser = serial.Serial("COM3", 19200, timeout=1)
ser.write("Whatever")

Linux , Arduino:

dmesg | grep tty

, : [ 7.944654] usb 1-1.6: FTDI USB Serial Device converter now attached to ttyUSB0

, Arduino ttyUSB0. , , Arduino Linux:

import serial
ser = serial.Serial("/dev/ttyUSB0", 19200, timeout=1)
ser.write("Whatever")

. 9600 Arduino, , serial.Serial("COM3") serial.Serial("/dev/ttyUSB0") - .

EDIT: , , . , IMMEDIATELY serial.Serial() . , , :

import serial
import time
ser = serial.Serial("/dev/ttyUSB0", 19200, timeout=1)
time.sleep(1.5)
ser.write("Whatever")

, , , .

+3

COM1 Windows,/dev/ttyS0- > COM1. code Python Quadcopter, Windows, Linux ( ) Pyserial.

COM3 Pyserial Windows. USB-to-serial ( VirtualBox). USB-, /dev/ttyUSBxx.

+1

" , , /dev/ttyS 1 ".

PySerial . "" (/dev/ttySX Linux, COMX Windows). . , , , , .

( socat com0com), USB, glob ( , , dev/ttySX). , pySerial . :

import glob, os

import serial

USB_SERIAL_GLOB = "/dev/ttyUSB*"

def try_open(port, args = (), kwargs = {}):
    try:
        port = serial.Serial(port, *args, **kwargs)
    except serial.SerialException:
        return None
    else:
        return port

def serial_scan(max_range = 32, args = (), kwargs = {}):
    for i in range(max_range):
        port = try_open(i, args, kwargs)
        if port is not None:
            yield port

    # Look for USB serial ports:     
    if os.name == 'posix':
        for fn in glob.glob(USB_SERIAL_GLOB):
            port = try_open(fn)
            if port is not None:
                yield port

if __name__ == "__main__":
    for port in serial_scan(kwargs = {'baudrate':9600, 'timeout':0.5}):
        port.close()
        print "Found: %s" % port.name
0
source

Source: https://habr.com/ru/post/1795919/


All Articles