0
votes

This has been resolved!! I have to wait a few seconds after opening serial port.


I want to execute the python program that is being done with the send button on the Arduino serial monitor.

if (Serial.available() > 0){
    Serial.print(hoge);
}

is written in arduino, and I want to make Serial.available ()> 0 by python program.

I tried...

1. If I send something like A or 3 on the IDE serial monitor, the contents of hoge will be output on the serial monitor.

2. Using pyserial

ser = serial.Serial('/dev/ttyACM0', 115200,timeout=None)
ser.write(str.encode('A'))
data = ser.readline()
print(data)

When this is executed, it waits for reception before ser.read (). After deleting the if (Serial.available ()> 0) of the program on Arduino and executing it, the contents of hoge were printed properly on the terminal.

The contents of hoge are

b'0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0\r\n'

What do I need to write to do the same thing as 'send'? How do I get Serial.available ()> 0 ...?

It would be very helpful if you could tell someone.

1
the contents of hoge are correct? it looks strange. there is no comma among '0\r\n'.. instead of 0,\r,\n.Kay
I think hoge are correct. Becouse, for (int i = 0; i < 159; i++) { Serial.print(data[i]); Serial.print(","); } Serial.println(data[159]);sattyorin
Where is the arduino code? In the loop ()?Kay
yes, In the loop().sattyorin
Try to add flush() after write() in your python code.Kay

1 Answers

0
votes

The first data of 'hoge[]' is NULL character. Therefore there's no chance for 'Serial.print()' function to print out the whole content.

  1. Serial.print(hoge) immediately returns because the first character is NULL character.
  2. ser.readline() is waiting forever because there's no newline incoming from Arduino.

Simply changing the content will solve this issue or use write() function (but if you keep the same content then you need to adapt your python code as well without using readline()).

In your python code, you need to add "time.sleep()" before "ser.write()" so that Arduino is ready to receive the serial data. More than 1 seconds would be required.