0

I want to dump a ROM. The Arduino waits until it receives the key which is one digit. I use this digit as the starting command and as a parameter to tell the Arduino how big the ROM is. This is my Arduino code (simplified for testing):

void setup() {
  Serial.begin(115200);
}

void loop() {
  uint8_t key = 10;

  while(key > 2){
    key = Serial.read();
    key -= '0';
  }

  Serial.print("75 A E8 98 D4 E8 8A E4 E8 EB E4 F9 C3 60 3C 55");
  Serial.println("");
  Serial.print("74 C E8 35 3 E8 79 E1 88 96 9F 0 EB F E8");
  Serial.println("");
  Serial.print("0 E8 9 1 FF 76 19 E8 4F DC 8F 46 19 61 F8 C3");
  Serial.println("");
  Serial.print("E8 5E E1 33 C0 EB 29 B8 1 0 81 BE A0 0 0 51");
  Serial.println("");

  Serial.println("EOF");
}

And this is my Python code to tell the Arduino to start dumping and to read the response:

line = " "

ser = serial.Serial("COM10", 115200, timeout=1)
ser.write("1")
ser.flush()

while line != "EOF":
    line = ser.readline().strip
    print line

ser.close()

The problem: its printing nothing.

PS: already tried different values for timeout but neither 0 or None worked.

EDIT: Everyone else is invited to awnser! I'm using Python 2.7.6 - Anaconda 1.9.2 32bit @ Win7x64

EDIT2 The solution is to add a delay of 2secs (time.speep(2)) before the first write in python. More details here: pySerial works fine in Python interpreter, but not standalone

Community
  • 1
  • 1
milkpirate
  • 267
  • 1
  • 18

1 Answers1

0

Pyserial certainly has no problem reading arduino serial ... I have done it many many times

try this

ser = serial.Serial("COM10", 115200, timeout=5)
ser.flush()
ser.write("1")
print ser.read(1000)
ser.close()

or abstract it further

class MySerial:
    def __init__(self,*args,**kwargs):
       kwargs["timeout"] = kwargs.get("timeout",5) #ensure timeout set
       self.ser = serial.Serial(*args,**kwargs)

    def query(self,cmd):
       self.ser.flush()
       self.ser.write(cmd)
       return self.ser.read(100000)

ser = MySerial("COM10", 115200, timeout=5)
print ser.query("1")

also I am a little curious why you are writing hex to a string all space separated like that instead of just writing in bytes...

you can verify that you can get reads as follows

void setup() {
  Serial.begin(115200);
}

void loop() {
    Serial.read();
    Serial.println("Hello World!\r");
}

this will wait for anything then send the hello world string ... make sure you see that at least then continue tweaking it to behave as you want (EG. get a specific value from the read before continuing ,send a message other than hello world ... my guess is that its not getting past your Serial.read() for whatever reason

Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
  • Copied your code, still printing nothing. Could it be that the arduino sends too fast that python is capturing too late? Are you sure about order of ser.flush() and ser.write("1"). Shouldnt I flush the buffer out after I wrote to it? I read a ROM. So there are plenty of \Null, \EOF, \n, \r,... and other control sequences in the serial stream so I thought it might be better to avoid this by sending it in hex. – milkpirate Sep 15 '14 at 17:23
  • hmmm Ive never had any issue with it ... and I have used it quite a bit – Joran Beasley Sep 15 '14 at 18:54
  • Ok .read() doesnt take parameters but it has a useable return code so: `code void setup() { Serial.begin(115200); } void loop() { while(Serial.read() == -1); Serial.println("Hello World!\r"); }` printed per Putty as desired. But not per PySerial. So I tried: `code void setup() { Serial.begin(115200); } void loop() { Serial.println("Hello World!\r"); }` and its reading fine! So I guess theres a problem about sending with PySerial... – milkpirate Sep 15 '14 at 18:56
  • Its soo strange! I added another USB-Serial converter to "sniff" the TX,RX lines. PySerial is sending fine and the Arduino reacts it should. If I paste the your code in the python console / run it as a script its not working, nothing new. BUT: if I type the code line by line its working like a charm! How does that come? – milkpirate Sep 15 '14 at 19:26