Arduino Forum

Using Arduino => Interfacing w/ Software on the Computer => Topic started by: roipoussiere on Mar 25, 2014, 11:14 pm

Title: [python] can't initialize PC<->Arduino connection
Post by: roipoussiere on Mar 25, 2014, 11:14 pm
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!
Title: Re: [python] can't initialize PC<->Arduino connection
Post by: PaulS on Mar 25, 2014, 11:19 pm
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.
Title: Re: [python] can't initialize PC<->Arduino connection
Post by: Robin2 on Mar 26, 2014, 11:29 am
The demo that I included in this Thread may be of interest.
http://forum.arduino.cc/index.php?topic=225329.0 (http://forum.arduino.cc/index.php?topic=225329.0)

...R
Title: Re: [python] can't initialize PC<->Arduino connection
Post by: roipoussiere on Mar 28, 2014, 12:01 am
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
Title: Re: [python] can't initialize PC<->Arduino connection
Post by: Robin2 on Mar 28, 2014, 10:08 am

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
Title: Re: [python] can't initialize PC<->Arduino connection
Post by: roipoussiere on Mar 28, 2014, 10:46 pm
Ok, maybe I could parse the available serial ports list to find the good one.

Thanks for your help,

Roipou