I have the following code running on an atmega328 (ArduinoUno).
#include <stdint.h>
#include <avr/interrupt.h>
#include <stdbool.h>
#include <util/delay.h>
volatile bool receiveQ = true;
volatile bool transmitQ = true;
ISR(USART_RX_vect)
{
uint8_t byte = UDR0;
if( !receiveQ && byte != 19)
{
return;
}
switch ( byte)
{
case 19:
receiveQ = true;
transmitQ = false;
break;
case 17:
receiveQ = false;
transmitQ = true;
break;
// process incoming data
default:
break;
}
}
void transmitByte(uint8_t byte)
{
while( !(UCSR0A & 1 << UDRE0));
UDR0 = byte;
}
void transmitString(char *pstr)
{
while( *pstr != 0)
{
transmitByte(*pstr);
pstr++;
}
}
int main(void)
{
UBRR0H = 0;
UBRR0L = 103;
UCSR0C |= 1 << UCSZ01 | 1 << UCSZ00;
UCSR0B |= 1 << RXEN0 | 1 << TXEN0 | 1 << RXCIE0;
sei();
while( 1)
{
if( transmitQ)
{
transmitByte(19);
transmitString("data");
transmitByte(17);
transmitQ = false;
};
_delay_ms(1000);
}
return 0;
}
I want it to implement XOnXOff handshaking, and my question is if I am doing it correctly.
When I set up communication with PuTTY, I first see "data" that is sent by the Arduino. I then send XOff (Ctrl+S), some data, and XOn (Ctrl+Q), following which the Arduino sends the next "data". And I can keep doing this again and again.
The reason I am wondering if I'm doing this correctly is because I have to manually send the XOff and XOn bytes from PuTTY when transmitting data, although I have set the flow control to be XOnXOff.
However, PuTTY strips the XOff and XOn bytes and only gives "data" when it is receiving data.
Update
The secnario is for real-time feedback control. During each time period, the serial input to the atmega328 is the reference signal. (If no new data is received it uses the previous data.) The serial output from the atmega328 is the sensor value (which is also used by the controller.) I am using the XOn and XOff characters to signal when to start sending the data and when transmission is complete.