Wait on Arduino auto-reset using pySerial

前端 未结 3 1124
礼貌的吻别
礼貌的吻别 2021-02-08 05:54

I\'m trying to read lines from an Arduino board with a very simple code (for the sake of showcasing the problem) on Linux.

Python code:

# arduino.py
impo         


        
相关标签:
3条回答
  • 2021-02-08 06:28

    you need to set your var , try:

    unsigned long ii = 0;
    

    but pay attention that this is a 32 bit var and when it is full ,cause overflow and reboot. for me work. As suggested by @Kobi K add a minimal delay time, for load real data at 9600 boud each char has a duration of 2 ms,

        void loop() {
    
          Serial.println(ii);
          delay(20);
          ii++;
        }
    

    And in python you need to declare a Pyserial like this:

    arduino=serial.Serial('/dev/ttyACM0',9600,timeout=0.0001)
    

    hope this help you

    0 讨论(0)
  • 2021-02-08 06:32

    The Arduino IDE's monitor toggle's the assigned DTR pin of the port when connected. Where this toggling causes a reset on the Arduino. Noting that the DTR is toggled after the Monitor has opened the Serial port and is ready to receive data. In your case the below example should do the same.

    Import serial
    
    arduino = serial.Serial('/dev/ttyS0',
                         baudrate=9600,
                         bytesize=serial.EIGHTBITS,
                         parity=serial.PARITY_NONE,
                         stopbits=serial.STOPBITS_ONE,
                         timeout=1,
                         xonxoff=0,
                         rtscts=0
                         )
    # Toggle DTR to reset Arduino
    arduino.setDTR(False)
    sleep(1)
    # toss any data already received, see
    # http://pyserial.sourceforge.net/pyserial_api.html#serial.Serial.flushInput
    arduino.flushInput()
    arduino.setDTR(True)
    
    with arduino:
        while True:
            print(arduino.readline())
    

    I would also add the compliment to the DTR for the Arduino's with AVR's using built-in USB, such as the Leonoardo, Esplora and alike. The setup() should have the following while, to wait for the USB to be opened by the Host.

    void setup() {
      //Initialize serial and wait for port to open:
      Serial.begin(9600);
      while (!Serial) {
        ; // wait for serial port to connect. Needed for Leonardo only
      }
    }
    

    It will have no effect for FTDI's based UNO's and such.

    0 讨论(0)
  • 2021-02-08 06:44

    I realize this is an old question, but hopefully this can be useful to somebody else out there with the same problem.

    I had an issue where if I used any baudrates other than 9600, the serial connection in python would just receive gibberish all the time, even if Serial.begin(...) is properly set on the arduino and matches the value used in the python code. I read online that the bootloader or watchdog may take a second to load (when the board is power-cycled) and it may send stuff over serial at some specific baudrate (for chip programming possibly). I'm guessing that this is what messes up the serial communication in python.

    Here's the piece of code that gives me reliable results:

    import serial
    from time import sleep
    arduino = serial.Serial('/dev/ttyACM0') # dummy connection to receive all the watchdog gibberish (unplug + replug) and properly reset the arduino
    with arduino: # the reset part is actually optional but the sleep is nice to have either way.
      arduino.setDTR(False)
      sleep(1)
      arduino.flushInput()
      arduino.setDTR(True)
    
    # reopen the serial, but this time with proper baudrate. This is the correct and working connection.
    arduino = serial.Serial('/dev/ttyACM0',baudrate=57600)
    
    with arduino:
        while True:
            print(arduino.readline())
    

    The code used on the arduino side for testing is as simple as this:

    void setup() {
      Serial.begin(57600);
      Serial.println("setup");
    }
    
    void loop() {
      Serial.println("hello");
      delay(200);
    }
    
    0 讨论(0)
提交回复
热议问题