How can I read the serial port of my VEX EXP using a USB cable?

I have seen some examples using vex.serial but I see that vex.serial no longer exists, could you help me?

You are trying to use APIs for Python from a different company/product (now unsupported and obsolete). The RobotMesh API is different from the VEXcode API.

What are you trying to do exactly ? Usually the USB serial port is just accessed with normal stdio calls.

1 Like

I tell you, I am a programmer with an emphasis on computer vision, I have a camera connected to my laptop, where I am doing hand recognition and according to the hand gesture I want to control my Vex exp robot, the script that interprets the hand gesture It is written in Python and sends ‘A’ to go forward ‘P’ to stop ‘D’ to turn right and ‘I’ to turn left, now I want to read those characters on the serial port of my vex EXP, for now I want to do it by cable later, I like to do it wirelessly,

I have connected my vex brain to my computer with the USB-C cable.
Can you help me, please? @jpearman

I’ll put together some demo code, but it probably will not be today. When using Python there’s also the additional complication that the USB serial port is also used for REPL, that can be disabled at the beginning of you code, but I need to show you how.

1 Like

I’ll be waiting, thank you very much!!

Here’s some very simple demo code to get you going.

This comment line near the top will disable REPL (you can still print to the output). if using VEXcode or VSCode with the vex extension make sure the user terminal is closed otherwise that will control the serial port.

#vex:disable=repl
code
# ---------------------------------------------------------------------------- #
#                                                                              #
# 	Module:       main.py                                                      #
# 	Author:       james                                                        #
# 	Created:      2/26/2024, 2:53:48 PM                                        #
# 	Description:  EXP project                                                  #
#                                                                              #
# ---------------------------------------------------------------------------- #
#vex:disable=repl

# Library imports
from vex import *

# Brain should be defined by default
brain=Brain()

def serial_monitor():
    try:
      s = open('/dev/serial1','rb')
    except:
      raise Exception('serial port not available')
  
    while True:
        data=s.read(1)
        #print(data)
        if data == b'a' or data == b'A':
            brain.screen.print_at("forward", x=5, y=20)
        if data == b'p' or data == b'P':
            brain.screen.print_at("stop   ", x=5, y=20)
        if data == b'd' or data == b'D':
            brain.screen.print_at("right  ", x=5, y=20)
        if data == b'l' or data == b'L':
            brain.screen.print_at("left   a", x=5, y=20)


        
t1=Thread(serial_monitor)
2 Likes

To do this wirelessly, you can use the same code but with USB hooked to the USB port of your EXP controller. You will always see two serial ports for both the brain and controller, use the second one (often given friendly name user port), the first one is used for admin, program download etc.

Technically BLE could be used as well, but we have never published documentation for using that from a PC.

1 Like

James, you are very kind, thank you very much for your help, when I have the complete project, if you want, I will share the repository here so that other people can replicate it, greetings from Colombia and again, thank you very much!

1 Like

Hi I’m new into VEX and I found this thread very helpful. @jpearman by any chances do you have an example code that does the same in c++?

How would you do this in c++?