Sending data from python to Serial 2 on Arduino

Hello there,

I'm doing a project where I'm sending data serially from Python to Arduino to control a robot. The

code is working and all; However, I want to try and send the data to Serial 2 instead of standard Serial. I

fail to see how that is possible. Any help would be highly appreciated.

Here is part of my python code:

import socket
import serial

arduino_port = 'COM4'


#functions:
def check_arduino_connectivity():
    global connected, count, ser
    connected = False
    count = 0 # help execute the print statement in serial.SerialException only once.
    while connected == False:
        try:
            ser = serial.Serial(arduino_port,9600)  # establish serial connection with Arduino
            print("Arduino Connected!")
            connected = True
            count = 0
        except serial.SerialException: # if arduino is not connected:
            if count == 0:
                print("Arduino is not connected to port. Please connect it to proceed")
                connected = False
            count = 1

Does your Arduino have a Serial2 or even a Serial1 besides Serial?

What type of Arduino do you have?

How is that second serial port connected to the device that is running the python code?

If you have two serial ports connected to the Arduino, just use that instead of COM4.

Mahmoud_Matar:
I want to try and send the data to Serial 2 instead of standard Serial.

You could try two Wixels, which are basically wireless usb.

  • One goes in the PC usb and is seen by serial monitor or any terminal or application (I've used Processing) just as COMx as normal.

  • The other goes on the Arduino, wired into any serial of your choice. I have used it on software serial and on Serial1 of a micro.

Whandall:
Does your Arduino have a Serial2 or even a Serial1 besides Serial?

What type of Arduino do you have?

Yes it does. I'm using the Mega2560.

Whandall:
How is that second serial port connected to the device that is running the python code?

If you have two serial ports connected to the Arduino, just use that instead of COM4.

All I'm doing is sending characters as serial data from Python to the standard serial using the function

'ser.write()' ('ser' is the link variable defined in the code above). I'm then using Serial.read() in my Arduino

code to acquire it. What I want is to use Serial2.read() instead. I fail to see how that is possible.

Here is the part of the Arduino code associated:

 if (Serial.available()) {

    key_stroke = Serial.read(); //reads the serial characters recieved from the raspberry pi

    if (key_stroke == 'F') {
      delay(10); // time gap before motor switches.
      Serial.println("Moving forward");
      robot_forward();
    }
    else if (key_stroke == 'R') {
      delay(10);
      Serial.println("Moving right");
      robot_right();
    }

and so on..

When you say Serial2 I assume you are using an Arduino Mega.

You could connect a PC to Serial2 using a USB-TTL cable.

...R

Robin2:
When you say Serial2 I assume you are using an Arduino Mega.

You could connect a PC to Serial2 using a USB-TTL cable.

...R

I guess that would work. But, is there any other way? I mean coding wise.

You can not reroute the connection to Serial with software to another Serialx on a Mega.

Whandall:
You can not reroute the connection to Serial with software to another Serialx.

Erm :blush: alright. I guess TTL cable is the only option then. Thanks!

Mahmoud_Matar:
Erm :blush: alright. I guess TTL cable is the only option then. Thanks!

Did you see my Wixel, wireless usb, suggestion earlier?

If it's a drive around robot, as opposed to a static pick and place style thing, the cost of Wixels to give remote control may be worth it.

Hal_Posoyan:
Did you see my Wixel, wireless usb, suggestion earlier?

If it's a drive around robot, as opposed to a static pick and place style thing, the cost of Wixels to give remote control may be worth it.

I'm sorry I completely missed your post. Can you please explain further? I've never used Wixel.

Its literally wireless usb. It has a usb connector and tx and rx pins. So you plug one into the pc... It declares itself as COMx. The other, you wire by its tx and rx pins to the Arduino Serialx.

Your Arduino talks to its Arduino oops Wixel on Serialx and your pc application talks to COMx.

The Wixels talk to each other wirelessly as wireless usb.

Oops, fixed error in previous post.