Go Down

Topic: Arduino Micro only executes code when serial is open from PC side (Read 234 times) previous topic - next topic

AreaScout

Hi and Hello

I have a problem with my Micro, if i use Serial1.available() the code on the Micro stops to execute if i don't have any Software connected ( port opened ) to it, is this because Serial1.available() is non-block / block or something else ? Please help


void customEvent()
{
   while (Serial1.available())
   {
            ... some code
   }
}


RG

AreaScout


Ok, i have found a personal solution to bypass the problem, the data from Serial1 is coming from an external Chip which is also powered by the Arduino Micro, so if the PC Application is finished collecting data and closing the port, i power down the chip so there is no data arriving to Serial1 anymore and then the code execution is not stopping and i can continue to process other jobs :)

wonder what you are doing if you need continues Serial1 data without PC Application opening a port ...


ballscrewbob

The "while serial" code waits for the USB port and is not always needed for most boards.

If you remove that (so long as its not a board that depends on it) then it will start the sketch and send data all day long without waiting for the USB to a computer to be connected.



 
It may not be the answer you were looking for but its the one I am giving based on either experience, educated guess, google or the fact that you gave nothing to go with in the first place so I used my wonky crystal ball.

pert

The "while serial" code waits for the USB port and is not always needed for most boards.
That would be
Code: [Select]
while(!Serial);
but AreaScout's is talking about Serial1, not Serial. Of course the !Serial code will produce exactly the symptoms described on a board with native USB, such as the Micro.

wonder what you are doing if you need continues Serial1 data without PC Application opening a port ...
That's not very clear but obviously you write your code differently.

AreaScout

That would be
Code: [Select]
while(!Serial);
but AreaScout's is talking about Serial1, not Serial. Of course the !Serial code will produce exactly the symptoms described on a board with native USB, such as the Micro.
That's not very clear but obviously you write your code differently.
exactly :)

the 'while(!Serial);' is not my problem, i don't use it, the problem is if i close the port as long data are arriving on Serial1 the execution of any other code stops ( about 1 or 2 seconds after disconnect )

RG

pert

The thing is, if you don't ever read the available data in a statement like this:
Code: [Select]
while (Serial1.available())
then it will always evaluate true and so will loop forever (unless you break from it). So it really depends on what this mysterious "...some code" is.

If you just need to discard the data you are free to do dummy reads:
Code: [Select]

// clear the read buffer
while (Serial1.available())
{
  Serial1.read();
}

AreaScout


i will write an example code, but that will take an hour or so before i have some time, thank you so far !

AreaScout

ok here is the code, i have shrinkt it down, it is way more complex, but basically the Micro Chip which is connected to the Arduino get's firmware updates, reads settings and writes status messages, so the Arduino acts as 'meet in the middle' doing parsing and checksum stuff.

The example will start with the Pin 13 blinking like a heartbeat, it is reading data from serial1 and sends it to virtual serial, if you start the serial monitor and close it, 2 or 3 seconds later the heartbeat will stop, it will resurrect if you restart the serial monitor, but this only happens if data are available on serial1


Code: [Select]

void setup()
{
  Serial.begin(38400, SERIAL_8N1);
  Serial1.begin(38400, SERIAL_8N1);
    
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop()
{
  digitalWrite(LED_BUILTIN, HIGH);
  delay(100);                      
  digitalWrite(LED_BUILTIN, LOW);
  delay(100);
}

void serialEvent()
{
  while (Serial.available()){
    Serial1.write(Serial.read());
  }
}

void serialEvent1()
{
  while (Serial1.available()){
    Serial.write(Serial1.read());
  }
}



is it that way that if Serial.write() tries to send data, but the port is closed it blocks ?

RG

AreaScout


@all

Well at the end this was very easy to fix.

Code: [Select]

if(Serial.availableForWrite())
{
       Serial.write(Char);
}

Go Up