E
E
Egor Mikheev2016-10-30 11:08:31
Python
Egor Mikheev, 2016-10-30 11:08:31

How to get rid of delay when transmitting data to arduino via serial port?

Hello, I just can’t find a solution to a simple problem of activating a pin on an arduino through a function call in python.
The problem is that there is a delay of half a second due to (as I understand it) waiting for the microcontroller to be ready. Can delays be avoided?
Python

import serial, time

ON = ":00000008f8"

ser = serial.Serial('COM3', 57600)
time.sleep(0.28) // если убираю здесь, то пин не активируется
#ser.flush()
ser.write(ON)
time.sleep(1)
ser.close()

Arduino:
const int pin =  2;

void pulseLed() {
   digitalWrite(pin, HIGH);        // sets the pin on
   delay(200);        // pauses for 50 microseconds      
   digitalWrite(pin, LOW);         // sets the pin off
   delay(200);       // pauses for 50 microseconds
  }

void setup() {
  pinMode(pin, OUTPUT);           // set pin to input
  // initialize serial:
  Serial.begin(57600);
}

void loop()
{
  // serial read section
  while (Serial.available()) // this will be skipped if no data present, leading to
  {
    if (Serial.available() >0)
    {
      pulseLed();
    }
  }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
vanyamba-electronics, 2016-11-07
@ogregor

When the host accesses the serial port of the Arduino (calling the serial.open() function), a so-called soft reset of the microcontroller occurs. This starts the Arduino bootloader, which waits for some time to see if a special sequence of characters comes from the host to launch the new firmware download program.
Since the special sequence is not received, the bootloader transfers control to the user firmware, running your sketch.
If you want the microcontroller to wait for the program to run on the host and not reset when the serial port is opened, then you should remove the soft reset jumper on your Arduino board.
Without this jumper, when uploading a sketch to the Arduino IDE, you will need to manually press the reset button to start the bootloader.

E
Egor Mikheev, 2016-10-30
@ogregor

As an option, do the initialization of the port at the beginning of the script, and transfer data already where necessary, then there will be no delays.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question