Hello,
is there a way, a kind of status byte, to get the activity on the UART line, which indicates byte reception in progress?
thx
Michel
Hello,
is there a way, a kind of status byte, to get the activity on the UART line, which indicates byte reception in progress?
thx
Michel
when there is at least one byte in the receive buffer you have Serial.available() > 0
thank you for the response
Possible to get the status during the byte reception? or have to wait until the full byte is received?
imho after the first byte is in the buffer.
But honestly is it relevant if it is on the first bit or the last? What do you want to solve?
ok. Thank you
Detect if a byte transmission is in progress. I have many microcontrollers sharing the same RX/TX line.
I don't see anything in the USART registers that would indicate that a start bit has come in.
One thing that might work is to connect the RX pin to an interrupt pin. If you record the time in microseconds of the latest FALLING edge you could calculate if a byte started and did not complete. Or connect RX to the InputCapture pin of one of the timers. That will give you a timing precision of 1/16th of a microsecond.
You can't do this safely without a mutex lock. You can sample a line just before a transmission begins, see that it's clear and grab the resource. But in the meanwhile, the sender of the transmission has no idea of that, and begins to transmit over your transmission.
What you need is a software managed sharing protocol that commands or receives permission from other clients to claim ownership of the line before transmitting.
I am assuming it is for communication from one to another or several processors on a common bus. You can use something like a 27K resistor from the RS232 line or directly if logic to one of the ports. You can read it and determine what is happening. You can add external hardware such as a retriggerable one shot and monitor that output. My recommendation is to use something designed to do this. If this is something important consider looking at CAN, it does all of the hard work for you.
Then this is a design issue. You need to have
Consider your code samples the data line during the transmission of a byte that is binary zero. There is a start bit followed by 8 zero bits followed by one or two stop bits. If you tested during the time the zero bits were in flight, what would your test find? The line would be showing no activity, but in actuality it was during the transmission of a single byte.
Thinking about an Uno. The Tx line that feeds into the Rx line is held high by default.
You can catch a falling edge with an interrupt. That will certainly trigger before the first byte is complete.
Here's a terrible example, but it gets the point across:
void print_receiving() {
Serial.write('*');
}
void setup() {
Serial.begin(115200);
while (not Serial.available()) {
delay(1);
}
attachInterrupt(digitalPinToInterrupt(2), print_receiving, FALLING) ;
}
void loop() {
if (Serial.available()) {
char inStr = Serial.read();
Serial.write(inStr);
}
}
And will trigger several more time for that byte, depending on the number of 1 bits.
So, use two bytes, one to set and one to ignore if set.
OP just asked about the first bit.
Or detach it, or any other procedure to otherwise ignore it until needed again.
At startup, or at some random time when you want to TX, whether a given zero state is a start bit, or a data bit, is not possible to know. Because the timing state of other devices on the bus is not known. Post #9 gets it right.
Thank for your response.
I wanted to avoid the use of an extra pin (interrupt pin). Do you know, beyond the Arduino, if native APIs of microcontrollers (ESP32, RP2040...) supports this feature (using USART registers or something else)?
Anyway, if using extra pin as interrupt, I like the idea of recording time, or the use of timer from John and the sample script from er_name_not_found.
Just be aware that my program was not refined in any way, and that you should be sure that you know more about using interrupts or read some of the many quality examples out there.
Post you're results, please. This is an interesting topic.
It's not part of the usual serial paradigm, generally as much functionality as possible is handed to the UART hardware module in the MCU, as possible. Timing, frame detection, bit alignment, noise rejection and so on are expected to run independently of the applications simple need for an incoming character. So it's highly unlikely that you will find the feature you are looking for, in the vast majority of MCU's.
That in itself should be a big red flag for you.
if one wants a light flickering as data does it's thing post#8 has a good idea.
I think what you need to do is connect the incoming line to 2 pins, the RX pin and a pin that gives you an interrupt on a low-going edge, and enable the usual serial library. Then you set up a global variable, a flag you could call "character_in_progress".
Then when the low-going edge is seen, the interrupt sets the flag, disables itself, and exits. You can do more work in the interrupt if you want, or test the flag anywhere in your program.
When a character is detected via Serial.available(), you clear the character_in_progress flag and re-enable interrupts from the low-going edge. This resets the software for the next character.
There is a potential bug here, that if response to the Serial.available() test is delayed, with multiple characters waiting to be processed, character_in_progress would have stayed set since the first character started. You'd need to consider whether that situation could ever occur, or whether it would matter if it did.
So what you mean is to have an extra pin (interrupt pin) connected to a retriggerable one shot and monitor that output? do you have an example/sketch of schematics? thx