Arduino Port Error

Hi,

I'm trying to formulate a python code for my Arduino Leonardo board to send high signal to the camera to acquire image only when it receives machine trigger pulse at the same time. I am using the 'arduino-python3' library to upload and control my board via python. i.e. I have a separate .ino file containing the sketch part that I upload to the Arduino board with the help of the python script using the arduino-python3 library.

But so far I have the following error:

File "/Users/QuasarGroup/anaconda3/lib/python3.10/site-packages/serial/serialutil.py", line 268, in port

  • raise ValueError('"port" must be None or a string, not {}'.format(type(port)))*
    ValueError: "port" must be None or a string, not <class 'int'>

I double-checked in my python script that I have used the right port name as per what I can see from the 'Tool' section's Port option (in my Arduino IDE). I have it mentioned in my python script as-

***board_port = '/dev/cu.usbmodem14201' ***
baud_rate = 9600

Is there anything wrong in that mention?
Can anyone please help with any possible solution-idea(s) regarding this?

many thanks.
Best,

It's not clear from your description what fails? The upload or the control of the uploaded sketch..

If it's the upload that fails:
When you do an upload to a board with native USB (like your Leonardo), the IDE issues a software reset (open and close port with a baud rate of 1200 baud). The result is that the boot loader is invoked; in Windows that will result in a different serial port (no idea about Linux/Mac).

You will have to simulate that process in your python code.

If it's the control of the sketch running on the Leonardo, please provide your python and Arduino code.

Is this true with Python?

(I've not tried it - so just wondering)

I'd have thought that would give a different error?

The message quoted seems to that the port has been specified using an invalid format - not that its value is invalid?

Please post the script so that others can see & check it!

A python script should follow the same procedure when it comes to uploads.

I have no idea about python / python error messages. And we have no insight in OP's code :wink:

Thanks @sterretje and @awneil
Verify and uploads work fine.

This is my .ino file (trigger_test_with_python.ino):

const int machineTriggerPin = 2;  // Input pin for machine trigger
const int cameraTriggerPin = 7;   // Output pin to trigger the camera

void setup() {
  pinMode(machineTriggerPin, INPUT);     // Set the machine trigger pin as input
  pinMode(cameraTriggerPin, OUTPUT);     // Set the camera trigger pin as output
  digitalWrite(cameraTriggerPin, LOW);  
  Serial.begin(9600); 
}

void loop() {
  if (digitalRead(machineTriggerPin) == HIGH) {
    digitalWrite(cameraTriggerPin, HIGH);
    delay(100);
    digitalWrite(cameraTriggerPin, LOW);
    Serial.println("Camera triggered!");
    
    while (digitalRead(machineTriggerPin) == HIGH) {
    }
  }
}

and this is my python script:

from Arduino import Arduino
import serial
import time

# Define the board's port and baud rate
print("line 5")
board_port = '/dev/cu.usbmodem14201'  # in accordance with the Arduino's port
baud_rate = 9600

print("line 9")

# Path to the Arduino sketch file
sketch_file = 'trigger_test_with_python.ino' 

print("line 14")

def main():

    print("line 18")
    arduino = Arduino(board_port, baud_rate)
    #arduino = serial.Serial(board_port, baud_rate, timeout=1)
    print("line 21")
    
    try:
        arduino.upload_sketch(sketch_file)
        print("Sketch uploaded successfully!")

        while True:
            user_input = input("Press 'T' and Enter to trigger the camera: ")
            if user_input.strip().lower() == 't':
                arduino.serial_write(b'H')  # Send high signal command to Arduino
                print("Camera trigger command sent")
            
    except KeyboardInterrupt:
        arduino.close()
        print("Serial connection closed")

if __name__ == "__main__":
    main()

I specified print line statements to check where the programs actually encounters issue. It seems that he python error comes after line 18.

Thanks.

That is, indeed, the first time you attempt to use board_port:

    print("line 18")
    arduino = Arduino(board_port, baud_rate)

So what does the documentation for that Arduino() constructor say about the type it requires for its first parameter?

I had the same problem after replacing my old arduino for a r4 WIFI version.
The solution was very obvious in fact. I was using the old usb c charging cable, and got the port error. that cable was rather cheap, so i changed the cable for a professional usb C 8 pin version.
the new arduino r4 is probably more unforgiving for cheapo usb cables.

i hope this solves most of the port errors.

Hi,
Not sure what you meant, so am I doing any mistake there? @awneil
because as per the serial port documentation (ser = serial.Serial(serial_port, baud_rate, timeout=1))...
Here I previously define the board port and then mention that on line 19 inside the parenthesis as the first argument.
thanks

oh is it so @willemhendrikstaal ? :no_mouth:
then I need to check with a different cable, although my current one is a new and of good quality..
thanks!

You mean this?

#arduino = serial.Serial(board_port, baud_rate, timeout=1)

But you say the error is happening here:

arduino = Arduino(board_port, baud_rate)

You need to check that the type expected by Arduino() as its first parameter is the same type as expected by serial.Serial() for its 1st parameter.

yes, the error happens right after line18, that's correct. @awneil
by that <<(ser = serial.Serial(serial_port, baud_rate, timeout=1))...>> I referred the documentation.
Moreover, if you kindly see the error message as I mentioned earlier-
... ValueError: "port" must be None or a string, not <class 'int'>
i.e. it complains about the integer factor. But my mentioned board_port is '/dev/cu.usbmodem14201' i.e a string, so that should suffices the argument specification!
that is why I'm confused.. :frowning:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.