Python Forum
Python error on mentioned Arduino port name - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Python error on mentioned Arduino port name (/thread-40581.html)



Python error on mentioned Arduino port name - dghosal - Aug-22-2023

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,


RE: Python error on mentioned Arduino port name - Larz60+ - Aug-22-2023

please post code.


RE: Python error on mentioned Arduino port name - dghosal - Aug-22-2023

(Aug-22-2023, 10:25 AM)Larz60+ Wrote: please post code.
Thanks.
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) {
}
}
}
[i][/i]


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()
Best,


RE: Python error on mentioned Arduino port name - deanhystad - Aug-22-2023

Please post entire error message and trace. Provide links for an special packages used. Is this the package you are using?

https://pypi.org/project/arduino-python3/

If so, the arguments to this call are in the wrong order.
arduino = Arduino(board_port, baud_rate)
baud rate comes first because board_port is an optional argument.


RE: Python error on mentioned Arduino port name - dghosal - Aug-22-2023

(Aug-22-2023, 02:38 PM)deanhystad Wrote: Please post entire error message and trace. Provide links for an special packages used. Is this the package you are using?

https://pypi.org/project/arduino-python3/

If so, the arguments to this call are in the wrong order.
arduino = Arduino(board_port, baud_rate)
baud rate comes first because board_port is an optional argument.


Hi, thanks.
This was the entire error message:

line 5
line 9
line 14
line 18
Traceback (most recent call last):
File "/Users/QuasarGroup/Documents/CI/GUI_Arduino/arduino_test_with_python.py", line 40, in <module>
main()
File "/Users/QuasarGroup/Documents/CI/GUI_Arduino/arduino_test_with_python.py", line 20, in main
arduino = Arduino(board_port, baud_rate)
File "/Users/QuasarGroup/anaconda3/lib/python3.10/site-packages/Arduino/arduino.py", line 130, in __init__
sr = serial.Serial(port, baud, timeout=timeout)
File "/Users/QuasarGroup/anaconda3/lib/python3.10/site-packages/serial/serialutil.py", line 222, in __init__
self.port = port
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'>


and Yes, that link was correct.
Thanks for suggesting changing the order, but, putting board_port after baud_rate still didn't solve the issue, although the message changed as follows:

line 5
line 9
line 14
line 18
Traceback (most recent call last):
File "/Users/QuasarGroup/anaconda3/lib/python3.10/site-packages/serial/serialposix.py", line 322, in open
self.fd = os.open(self.portstr, os.O_RDWR | os.O_NOCTTY | os.O_NONBLOCK)
OSError: [Errno 16] Resource busy: '/dev/cu.usbmodem14201'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/Users/QuasarGroup/Documents/CI/GUI_Arduino/arduino_test_with_python.py", line 40, in <module>
main()
File "/Users/QuasarGroup/Documents/CI/GUI_Arduino/arduino_test_with_python.py", line 20, in main
arduino = Arduino(baud_rate,board_port)
File "/Users/QuasarGroup/anaconda3/lib/python3.10/site-packages/Arduino/arduino.py", line 130, in __init__
sr = serial.Serial(port, baud, timeout=timeout)
File "/Users/QuasarGroup/anaconda3/lib/python3.10/site-packages/serial/serialutil.py", line 244, in __init__
self.open()
File "/Users/QuasarGroup/anaconda3/lib/python3.10/site-packages/serial/serialposix.py", line 325, in open
raise SerialException(msg.errno, "could not open port {}: {}".format(self._port, msg))
serial.serialutil.SerialException: [Errno 16] could not open port /dev/cu.usbmodem14201: [Errno 16] Resource busy: '/dev/cu.usbmodem14201'


Now, I don't get why it says busy! I just checked with my Arduino IDE, and upload a program; the "/dev/cu.usbmodem14201" port is correctly assigned to the Arduino Leonardo board. It works fine there, but not with python API?

Thanks.


RE: Python error on mentioned Arduino port name - deanhystad - Aug-22-2023

It resolved the problem. Now you just have a different problem.

If your IDE is using the serial port how can your python program use the same serial port? Do you have another serial port? If not, close the IDE and try running your program.

Was just looking at this page:

https://www.learnrobotics.org/blog/communication-between-arduino-python/#:~:text=1%20Download%20and%20install%20Python%202.7.x%20compatible%20with,will%20install%20the%20module%20required%20for%20Serial%20communication.

Under "Testing the Build"

Quote:Now before running the program, close the Arduino IDE. We need the COM port to be available for serial communication, so both programs can’t be running simultaneously.

In the future you might get a better/quicker response on an Arduino forum.