Communication over serial port

Hello, I am working on automated chess project.
I need to send data from Arduino to Python and then process it and send back some data to Arduino. If i understand this mechanism right, I can send data to serial monitor and then my data is in buffer and whenever this data is read, it is removed from serial monitor? What if i send data to serial monitor in Arduino (which have to be read in Python) and a few instructions later i read data from serial monitor in Arduino - is it gonna read data send a few instructions earlier? How can I know whether Python or Arduino is going to read data if they are working at the same time? Unfortunately I cant open serial monitor in Arduino while Python code is working. I hope you understand my problem, I know i made it little bit confusing.

Greetings.

The serial line works both ways so Python can send data to arduino and arduino can send data to Python
There is a small buffer on the Arduino side so as long as your code does come back frequently to check if there is some data waiting you’ll be fine

I would suggest to study Serial Input Basics to understand this better

When the Arduino sends data via the Serial link it neither knows or cares whether it has been read or not, nor can it find out. It is the responsibility of whatever the Arduino is connected to to read the data at the rate that it is sent

So, if the data has not been read then it is not available to be read back by the Arduino

When data is sent to the Arduino over the Serial link it is the Arduino's responsibility to read it fast enough, but it will never read back data that it previously sent whether it has been read or not

You mention the Serial monitor a number of times but, that is only a way of seeing the data and in practice it would not be used in a project such as yours except for information and/or debugging messages. In fact, you should not use the same hardware Serial pins as the Serial monitor because there will be a clash of resources. Depending on which Arduino you have it may have more than one Serial interface or you might need to use SoftwareSerial to create a second iserial nterface

You can't use the Arduino-IDE serial monitor.
Your python-code must establish its own serial connection
to send back and forth data over this serial connection

your-python-code----->------serial_data----->-----Rx_Pin_of_Arduino

Rx_Pin_of-Arduino-------<----serial_data-------<---your-python-code

your-python-code-----<------serial_data-----<-----Tx_Pin_of_Arduino

Will the Python code really be sending to the Tx pin of the Arduino ?

Hi Bob,

thank you for pointing on this typo. No python sending to the Rx-Pin of the arduino.
corrected it

Thanks for the correction

unless you use a different Serial port to communicate with the PC as proposed by @UKHeliBob

I have written it not precise enough.
You can't use the Arduino-IDE serial monitor to "feed_in" the serial data into your python-code

https://pyserial.readthedocs.io/en/latest/shortintro.html

actually you can if you have 2 distinct Serial ports

if you have only a UNO and don't want to use SoftwareSerial, if you don't use the USB cable but a couple FTDI adapter then you can too as the Tx and Rx do not need to go to the same port

Arduino Tx --> FTDI#1 --> Python
Arduino Rx <-- FTDI#2 <--- Serial Monitor

the Mac/PC does see the 2 FTDI adaptors and so you can open a communication with both of them on two distinct com ports

a diode and some smart cabling could also be used probably with USB + one FTDI

can't you use the arduino serial interface with another PC app after closing the Arduino IDE?

sure - even with the IDE open (at least on my Mac) , just don't open the Serial Monitor in the IDE

i did not know that

Serial.begin() causes problems too I believe

how so ?
I'm doing this on my Mac all the time with Python or other terminal software

I don't remember the details and I have not fallen foul of it myself, but I have a recollection of it causing problems but cannot be sure, hence "I believe", which was possibly too strong a phrase to use

It is very possible that the circumstances were different than those pertaining to this topic but as I can't provide a reference then I may well be wrong

OK - yeah a bit of mystery is always possible when the IDE is involved :wink:

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