Python Forum
Python error on mentioned Arduino port name
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python error on mentioned Arduino port name
#1
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,
Reply
#2
please post code.
dghosal likes this post
Reply
#3
(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,
Reply
#4
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.
Reply
#5
(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.
Reply
#6
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/commu...munication.

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.
dghosal likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to Get Arduino sensor data over to excel using Python. eh5713 1 1,725 Dec-01-2022, 01:52 PM
Last Post: deanhystad
  python serial port barryjo 2 1,662 Dec-27-2021, 11:09 PM
Last Post: barryjo
  is there a way to mention port range or search for port dynamically with qConnection Creepy 0 1,495 Sep-09-2021, 03:15 PM
Last Post: Creepy
  Unknown error occurred: Port not found NewBeie 0 1,442 Aug-27-2020, 08:50 PM
Last Post: NewBeie
  Port my python program to Raspberry pi seamlessly Hassibayub 1 1,951 Jun-29-2020, 12:58 PM
Last Post: snippsat
  Can't transmit serial fast Python to Arduino pyserial mRKlean 0 2,373 Mar-29-2020, 08:12 PM
Last Post: mRKlean
  About Arduino and Python usb webcam tuts Simurg 1 2,175 Mar-20-2020, 07:25 PM
Last Post: Larz60+
  Help Graphing Arduino Data Real Time in Python nschulz 0 2,547 Mar-12-2020, 06:15 PM
Last Post: nschulz
  Arduino / Python Environment Setup - PIP stevealbright 0 2,270 Feb-24-2019, 10:48 PM
Last Post: stevealbright
  Arduino Read Update Datetime from Python jambuna35 0 2,513 Feb-03-2019, 05:13 PM
Last Post: jambuna35

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020