I have a code in python that sends info to the Arduino mega with serial USB, but the info doesn't get sent until I open the serial port in the Arduino. For now, the code only sends a set number as a test.
When I run the code, I choose the port, then when I enter a number it sends the set numbers. The info I send doesn't get recognized until I open the serial port.
python code:
import serial
import serial.tools.list_ports
from struct import *
def allPorts():
ports = serial.tools.list_ports.comports()
result = {}
count = 0
for port, desc, hwid in sorted(ports):
result[count] = "{}".format(port, desc, hwid)
count += 1
return result
def choose(result):
for port in result.keys():
print(port, result[port])
x = int(input("Choose port: "))
for i in result.keys():
if x == i:
return result[x]
if __name__ == '__main__':
while True:
portChoose = choose(allPorts())
print(portChoose)
try:
arduino = serial.Serial(portChoose, 115200)
except:
print('Port open error')
else:
break
def write_to_arduino(x):
arduino.write("10, 20, 30, 2500,7000\n".encode())
while True:
portChoose = choose(allPorts())
print(portChoose)
try:
arduino = serial.Serial(portChoose, 115200)
except:
print('Port open error')
else:
break
while True:
n = input("Enter value: ")
write_to_arduino(n)
isn't this a common problem, 2 devices coming up at different times and not knowing if the other is ready.
shouldn't each repeatedly send a ping every second until there is a response. doesn't matter which. presumably the one that come up last will most likely send a ping that is received and receive a response for. it's ok if both send pings at the same time
once a response is received, regular data transmission can occur.
another approach is to repeatedly send a msg until it is acknowledged.
are you suggesting that the arduino doesn't receive anything, until the device you're using to confirm reception, the serial monitor, is opened? (are you perhaps expecting that anything queue in the serial interface (i.e. Serial.print()) before the serial monitor is opened will appear once the serial monitor is opened)
not sure i understand this correctly. but an alternate approach to comfirm reception is to briefly turn on an LED (e.g. LED_BUILTIN) whenever the arduino recognizes that something is available
I didn't mention it earlier because it wasn't important, but the data is being sent to my mega that displays the info on a 3.5 inch screen. i can send you the full code but it isn't really important.
So I know when the data arrives, because the numbers on the screen change.
i tried testing sending something to the Arduino from the PC
using the following code to toggle an LED whenever it receives a string of characters (e.g. readByteUntil ()).
i don't have a terminal emulator, using cygwin i just did echo "greg" > com4 to send something out the interface to the arduino
the LED toggles
without the IDE running
with the IDE running without the serial monitor open
but not with the serial monitor open because the IDE has opened that interface. i get "-bash: /dev/com4: Permission denied"
The info is the same so i see it only once. I think maybe when i send the data it getting into some sort of a queue and when i open the serial montior only then it works itself out
you mean it just appears on the serial monitor once
this is not the way a proper serial interface should work, but it's windows
i think your python script is not properly opening the serial interface on the PC.
just to be clear, the Arduino doesn't have an interface that needs to be opened (i.e. for some specific application, there's on one program on the Arduino). but the Arduino IDE needs to open the specific "com" port on the PC to download/program the Arduino or to use the Serial monitor.
i agree, that it sound like the python data is being queued in some buffer for the same port used by and opened by the Arduino IDE. i don't understand why it should be sitting there and not transmitted unless the python code is waiting for a ready to send (RTS) signal
it's been a while since i worked with python. why do i see more than one serial.Serial() (which assume opens the interface)? and why is the in a while loop, will the "else" case be executed if it succeeds?
looks like you've been working with a "bandaged up" piece of code. why not try a simpler piece of code that simply hardcodes the port and data/msg, and simply opens the port and sends the data/msg once a second? and can have the script print progress on the PC
I used older code that i wrote, when you run the script it first list you all of the ports and let you choose and break out of the loop when it connect,(that why the else). after that i run code that send serial to the arduino