0
votes

I am using a Raspberry Pi (Revolution Pi by Kunbus) to control multiple arduinos via serial communications (usb). I wanted to know if it was possible to control/communicate with multiple serial ports at once. I know multi serial aruduinos, such as the MultiSerialMega, allow for you to list Serial.begin(9600); and Serial1.begin(9600); to communicate with multiple at once, does this exist on the Raspberry Pi? As it stands I switch ports but the program running on the other arduino stops as soon as I do so.

To summarize: Can I sommunicate with three serial ports in python such as in the MultiSerialMega?

Thanks, Levi

I've tried just adding a one, like the example for the MultiSerialMega, but that does not work (not surprisingly)

import serial
import time

while True:
    ser = serial.Serial("/dev/ttyACM0")
    steps = int(input("How many steps?")
    ser.write(b'%d' %steps)
    ser = serial.Serial("/dev/ttyACM1")
    steps = int(input("How many steps?")
    ser.write(b'%d' %steps)

I am just playing around with stepper motors at the moment, but as soon as I switch serial ports the program ends/motor stops moving.

1

1 Answers

0
votes

From what I understand you want to write to multiple serial ports connected to your Rpi. You should be able to do that with as many ports as you want the same way you do with your Arduinos with multiple hardware UARTs.

Try this:

import serial
import time
import sys
ser1 = serial.Serial("/dev/ttyACM0")
ser2 = serial.Serial("/dev/ttyACM1")
while True:

    steps = input("How many steps for serial 1? (press q to quit")
    if steps == "q" or steps == "Q":
        ser1.close()
        ser2.close()
        sys.exit()
    else:
        ser1.write(int(steps))

    steps = input("How many steps for serial 2? (press q to quit)")
    if steps == "q" or steps == "Q":
        ser1.close()
        ser2.close()
        sys.exit()
    else:
        ser2.write(int(steps))

EDIT: If you happen to have so many Arduinos that you run out of USB ports to connect them you can consider switching to RS485 which is multi-point so you can connect up to 32 serial devices (your Arduinos) to a single serial port on your Rpi. See also this link