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-
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.
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.
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.
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
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..