Go Down

Topic: [python] can't initialize PC<->Arduino connection (Read 1 time) previous topic - next topic

roipoussiere

Hi all!

I can't initialize a PC<->Arduino communication with my Python script (using pySerial library).
The goal is to send a 'START' message to the Arduino, and when it recieved it, the Arduino send back 'START_INIT'. Very simple :)

On the Arduino side, I have this :
Code: [Select]
void setup() {
  Serial.begin(9600);
  String line = "";
  while (line != "START") {
    line = readLine();
  }
  Serial.println("START_INIT");
}

void loop() {

}

String readLine() {
String message = "";

while (Serial.available()) {
delay(3);
if (Serial.available() > 0) {
char c = Serial.read();
message += c;
if (c == '\n') {
break;
}
}
}
delay(50);
return message;
}


I tested this program, sending 'START' with the Arduino GUI and it works, so no problem here I think.

To the Python side, I have this :

Code: [Select]
#!/usr/bin/python
# -*- coding: utf-8 -*-

SERIAL_BAUDS = 9600

import serial
from serial.tools import list_ports

port = list_ports.comports()[-1][0]
print 'Begining serial connexion using port ' + port + '.'
ser = serial.Serial(port, SERIAL_BAUDS)
ser.write('START\n')

while True:
print '> ' + ser.readline()

ser.close()


Then, when I run the script I don't get the 'START_INIT' message.
Some idea ?
Thanks!
N'attendez pas de savoir pour entreprendre.
Drawall, le robot qui dessine : http://drawall.cc/

PaulS

Quote
Some idea ?

The usual. Opening the serial port resets the Arduino, unless you've modified it to not reset when the serial port is opened.

You don't wait for the Arduino to be ready before you jam data at it. Then, you don't do anything in loop(), or resend the START message, so the Arduino will never acknowledge that it got it.
The art of getting good answers lies in asking good questions.

Robin2

Two or three hours spent thinking and reading documentation solves most programming problems.

roipoussiere

Thanks a lot for this explanation!
I have loaded the Robin2 demo and now I can recieve my START message!

However there is an another problem: when I unplug and plug again the Arduino, the script don't gets nothing.
Strangely, it works again when I load the program on the board, or when I start/close a serial communication on the Arduino IDE.

Roipou
N'attendez pas de savoir pour entreprendre.
Drawall, le robot qui dessine : http://drawall.cc/

Robin2


However there is an another problem: when I unplug and plug again the Arduino, the script don't gets nothing.


I'm guessing here because your description doesn't have enough detail ...

If you disconnect the USB cable before your software closes the relevant Com Port when you reattach the USB cable it will appear as a different Com Port. Your software will still be trying to work with the first one so you need to restart your PC program and make sure it is connecting to the correct Port.

Long story short .... don't disconnect the USB cable :)

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

roipoussiere

Ok, maybe I could parse the available serial ports list to find the good one.

Thanks for your help,

Roipou
N'attendez pas de savoir pour entreprendre.
Drawall, le robot qui dessine : http://drawall.cc/

Go Up