I'm working on a project that an ESP32 module needs to read some value from a sensor and send it to a python code using pySerial. My code in constantly reading from the serial, as I can send data to it at any time. The problem is that the python program cannot be stopped because it's a server, but i still want to upload code whenever I want to the ESP32 module. Also, I'm sending data via serial on the void setup() function, so I need the python code to be running when the code is uploaded to the board, that is, when the setup() function runs.
I know I can't upload my code to the board while the serial port is being used by pySerial, but is there any way around? Like closing the connection while I'm uploading the code or when there is no data to be sent?
Here is the code that I'm using:
import serial
import json
ser = serial.Serial("COM6", 115200)
while(1):
if(ser.in_waiting > 0):
leitura = ser.read_until(b'}').decode('ascii')
data = json.loads(leitura)
handleRequest(data)
Related
I have a python script that creates a serial connection to my Arduino-Mega (ramps1. 4). I am using the pyserial library, with which I send G-Code commands via the COM. There "Marlin 2.0x" reads the input and acts on any G-Code.
So far everything works. I can write any G-Code via serial.write() and Marlin understands it. But unfortunately I had to add a time delay if I want to act on multiple commands.
Is there a nice way of circumventing that?
Here is an example code of Extruding 1mm of Filament twice.
import serial
import time
ser1 = serial.Serial('COM3', 250000)
time.sleep(1)
ser1.write(('G92 E1\n').encode())
time.sleep(1)
ser1.write(('G92 E1\n').encode())
Ideally it'd look like this without the delays:
import serial
import time
ser1 = serial.Serial('COM3', 250000)
ser1.write(('G92 E1\n').encode())
ser1.write(('G92 E1\n').encode())
But then commands get skipped.
I'm working with a python program on a Windows PC running under Anaconda that is talking to multiple USB-attached arduino megas. I can create a string on the python side and successfully encode it as bytes and send it to the arduino where it is correctly interpreted. I can read a string from the arduino into the python program and correctly interpret it. The issue I'm having is when I try to read from one arduino and then send that message back out to another arduino. Example:
Python side code:
response = ser1.readline() #Read from arduino #1
ser2.write(response) #Write the response read from #1 out to #2
Arduino side (for serial 2):
if (Serial.available()>0) {
newStr = Serial.readString();
}
The Serial.readString() never completes; the arduino just hangs at that point.
I'm sure it's something stupidly simple, but python is a new language for me so I haven't been able to figure it out.
I am working on an existing project.
Until now, a PC software controls an Arduino Due.
The PC software sends serial commands to the Arduino Due.
What I am trying to do, is to replace the PC software with a python script. Python 3.5.
So I am working with pyserial.
The problem seems to be that the python script does not send all the characters to the Arduino Due. It misses some final characters.
The difficult parts to understand are the following:
When I am sending the characters, from the python script, to another PC terminal instead of the Arduino, then I can successfully collect all the characters from the terminal, I am using Bray's terminal.
When I am sending the same string from my terminal to the Arduino Due, the Arduino Due successfully collects the data sent.
It seems as if only the Python to Arduino does not work, while
Python to PC termimal is working and
PC terminal to Arduino is working
I open the serial port like this:
my_port = serial.Serial('COM6', 115200)
while connected != True:
if my_port.is_open == 1:
connected = True
Can anyone provide any insight?
Thanks.
edit: I just noticed that when the python script sends the data, then the debug serial port I am using sends corrupted data.
Solved it.
I noticed that the debug serial was also sending less characters and I thought that there maybe a reset going on.
So I am now sending my arrays from a thread after each button press.
What I was doing, is that I was sending it directly after connection.
I'm developping a script with Pyserial to send data to a microcontroller, the microcontroller then read the data, process them, and send back some debug information to my python script.
My python script was working without any issue when i was just reading the data from the microcontroller. Now that I need to send data to the microcontroller and start reading just after that, the data i'm reading are not complete.
I should receive something like [TAG1],10,11,12,[TAG1],13,14,15\n but sometimes I don't received the beginning of the data but only the end like 1,12,[TAG1],13,14,15\n
I'm basically doing:
serial.write(dataOut)
dataIn = serial.read(sizeOfTheDataToReceive)
The issue does not come from the microcontroller I'm sure of that, if I'm using putty to send/receive my data I always see the full data.
I tried to add some delay in my microcontroller code to send the data 10s after receiving the data from python, but still it's not working everytime.
Do you have any idea what can cause it ? The COM port is opened when the python script start and is closed at the end of the script.
You need to clear your read and write buffers:
serial.flushInput()
serial.flushOutput()
Then read the data byte-wise:
serial.write(dataOut)
time.Sleep(0.3)
s=''
while serial.inWaiting()>0:
b=serial.read(1)
# time.Sleep(0.1)
s += b
I'm currently attempting to write over USB serial to an Arduino Nano of mine using Python. However, what I've discovered is that (using the exact same code), the code works perfectly when I type it into IDLE, but when I save it to a file and attempt to run from there, for some reason the Arduino is never receiving the data. I've checked and in both locations the correct version of Python is being used (2.7.9) (I unfortunately can't use Python 3 due to other libraries I'm using).
The code I'm using:
import serial
ser = serial.Serial(port='/dev/ttyUSB0', baudrate=9600)
ser.write('0')
print ser.readline()
When I run it in IDLE just by typing in the lines individually, the correct behavior is seen: the Arduino responds (turning a servo) and echoes back the data it was sent, which is printed correctly. Running from a saved file however, the servo does not respond and no echo is received.
Any ideas?
I somehow missed this answer on SO before (pySerial write() works fine in Python interpreter, but not Python script), but it turns out that I needed to add a time.sleep(2) after opening the serial port. My guess is that in IDLE the time it took for me to type the next line accounted for this delay, but it was happening instantly in code.