Skip to content

Serial trigger 1 byte is possible???? #5620

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
tv4you2016 opened this issue Sep 1, 2021 · 4 comments · Fixed by #6134
Closed

Serial trigger 1 byte is possible???? #5620

tv4you2016 opened this issue Sep 1, 2021 · 4 comments · Fixed by #6134
Assignees
Labels
Status: In Progress ⚠️ Issue is in progress

Comments

@tv4you2016
Copy link

Hardware:

Board ESP32 Dev Module
Version/Date 2.0.0
IDE name Arduino IDE
Flash Frequency 80Mhz
PSRAM enabled no
Upload Speed 115200
Computer OS Windows 10

Description:

Hey Good afternoon,

I have a doubt, and it is possible to make a trigger for each byte received by Serial.
I'm using the following code but it doesn't.

#include <Arduino.h>


#define PIN_NOT_RE 18   // LOW = Recived
#define PIN_DE 19  // HIGH = SEND

void setup() {
  // put your setup code here, to run once:

  Serial.begin(14400);
  Serial.setRxBufferSize(1);
  Serial.println(Serial.getTimeout()); // print the default value
  Serial.setTimeout(1);
  Serial.println(Serial.getTimeout()); // print the new value
  pinMode(4, OUTPUT);
  pinMode(PIN_NOT_RE, OUTPUT);
  pinMode(PIN_DE, OUTPUT);

  delay(5000);


  digitalWrite(4, HIGH);

  digitalWrite(PIN_NOT_RE, LOW);
  digitalWrite(PIN_DE, LOW);
}


void loop() {

}



void serialEvent() {

  if (Serial.available()) {
    digitalWrite(4, LOW);
    // get the new byte:
    char inChar = (char)Serial.read();
    digitalWrite(4, HIGH);
  }

}

Captura de ecrã 2021-09-01 155422

@tv4you2016 tv4you2016 changed the title Serial trigger 1 byte is possivel???? Serial trigger 1 byte is possible???? Sep 1, 2021
@SuGlider
Copy link
Collaborator

SuGlider commented Sep 2, 2021

@tv4you2016 ,

Please turn the debug level to verbose and check the messages, but be aware that some messages may be sent at baud rate 115200 because this is the default rate at boot time.

I can tell that Serial.setRxBufferSize(1); doesn't work given that the minimum RxBufferSize is 129 bytes.
RxBufferSize must be set before starting Serial port (Serial.begin(...))
Look at https://github.com/espressif/arduino-esp32/blob/master/cores/esp32/HardwareSerial.cpp#L275-L289

Regarding your specific question, I've tested it with this sketch, and it worked fine:

#include <Arduino.h>

void setup() {
  Serial.begin(115200);
  Serial.println();
  Serial.println("Ready to receive bytes on UART.");
  Serial.println("It will output one byte per line with its HEX value.");
  Serial.flush(); 
}

void loop() {
}

void serialEvent() {
  if (Serial.available()) {
    char inChar = (char)Serial.read();
    Serial.print(inChar);
    Serial.print(" - 0x");
    Serial.print(inChar, HEX);
  }
  Serial.println();  // this line indicates that it has read a single byte at a time
}

@rottaran
Copy link

rottaran commented Sep 7, 2021

serialEvent is just called inbetween loop(), see

if (serialEventRun) serialEventRun();
. Neither interrupts nor full buffers involved.

@SuGlider
Copy link
Collaborator

SuGlider commented Sep 8, 2021

serialEvent is just called inbetween loop(), see

if (serialEventRun) serialEventRun();

. Neither interrupts nor full buffers involved.

Yes, this is correct. Arduino has no API to trigger UART events based on ISR or Full buffers.
All it has is the serialEvent called in between loop() as you pointed out - but this is exactly the Arduino specification.
https://www.arduino.cc/en/Tutorial/BuiltInExamples/SerialEvent

@SuGlider
Copy link
Collaborator

A potential solution is being implemented.
It's the Serial.onReceive(function)

As soon as any data arrives UART RX, function() will called.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: In Progress ⚠️ Issue is in progress
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants