ponty / pysimavr

python wrapper for simavr which is AVR and arduino simulator
GNU General Public License v3.0
49 stars 14 forks source link

Serial write after things get started? #37

Open gebl opened 7 years ago

gebl commented 7 years ago

Hello,

I'm trying to work with getting python code to write strings over the Serial link over time.

I've set up a loop that looks like this:

while True:
    avr.run()
    r, w, x = select.select([sys.stdin], [], [], 0)
    if r:
      line = sys.stdin.readline()
      avr.uart.send_string(line+"\n")

This works for a short period of time, but, if i wait to type input or try again later, it is never delivered to the arduino.

the arduino code looks like:

void loop() {
  Serial.print("Looping.\\n");
  Serial.print("Enter Value: \\n");
  while(1) {
    while (Serial.available() > 0) {
      char recieved = Serial.read();

      // Process message when new line character is recieved
      if (recieved == '\\n') {
          Serial.println(inData);
          inData = ""; // Clear recieved buffer
        } else {
          inData += recieved; 
       }
   }

Am i missing something? is there a better way to serial write to the arduino sim?

gabe

Vincent14 commented 7 years ago

Uh. Interesting. :(

@ponty ? @Premik ? @SebastianZug ? Someone knows?

Premik commented 7 years ago

Hi, I can imagine you hit some simavr bug. Like this one: https://github.com/buserror/simavr/issues/144 It seems to me like once the xoff signal is generated the uart stops receiving bytes. As a dirty workaround you can try to:

  1. Don't use the while (Serial.available() > 0) call. Instead always just Serial.read(). The return value would be -1 when there are no data.
  2. In python send the data really slooowly. So the xoff never gets a chance to trigger. Replace the avr.uart.send_string(line+"\n") with:
    for c in line+"\n":
            avr.uart.send_string(c)
            time.sleep(0.3)