I'm new to arduino but have exp in python. Im trying to send a hello world message from my py snippet to Arduino's Serial Monitor. What am i doing wrong? I've attached both codes below. But I can do only open one at once (either execute the py or open the serial monitor) or else it gives me access denied errors on either sides. Thanks.
If you are using an AVR based Arduino, like the Uno R3, classic Nano, etc. don't use String objects, as they cause memory problems and program crashes.
I am not a Python coder but you COM5 refers to the communications port on your windows PC, not the serial port on the Arduino. I believe referencing the proper port and it will work. This is just a SWAG as I do not use windows.
On any PC, in general only one program can use a serial com port at a time.
It should not be a problem for the Python program to allocate and use the serial port assigned to the Arduino (after the Arduino program has been uploaded, and is running), but then you cannot use the Arduino serial monitor.
So of course you will not be able to see the results of Serial.print() from the Arduino, unless your Python program is also listening to the serial com port, receives that message, and displays it on the PC screen.
As it is, your setup can send serial commands to the Arduino, such as turn on/off an LED.
Yes, as I already stated, the Python program has to listen to the serial port, receive the data, and print it to the screen. I'm sure you can find many examples for Python, posted on line.
The Arduino IDE has nothing to do with this process.
My original purpose is to receive a 4 bit binary text from py (where im converting dec to binary) into arduino (where ill be using the 4bit to light up 4 led's accordingly).
@chaitanyajoshi something similar to this cropped up a few days ago and the solution turned out to be a missing newline character, I am not suggesting that is the problem here but rather the post may contain information you might find useful.
The post contains a Python/Arduino script that has a means to echo characters back to the PC (Python environment) as a way to confirm sending and receiving.
Just some additional information not related to "I can do only open one at once".
Which Arduino? Most Arduinos go through a reset cycle when you open the serial port and invoke the bootloader.
The bootloader runs for a few seconds before passing control to the loaded sketch. During that time any data that you send from python will be lost. Add a delay before the line ser.write(tex.encode('utf-8')). 2 seconds will probably enough; if not, make it 5 seconds.