XOn/XOff flow control

Post your cool example code here.
Post Reply
ag123
Posts: 1653
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

XOn/XOff flow control

Post by ag123 »

https://en.wikipedia.org/wiki/Software_flow_control

Code: Select all

#include <Arduino.h>

enum State {
	XOff = 0,
	XOn
};

State state;

void setup(){
	state = State::XOff;
	pinMode(PA0, INPUT_ANALOG);
};

void loop() {
	// this part is the command processor
	if(Serial.available()) {
		int c = Serial.read();
		switch(c) {
			case 17: // Ctrl Q
				state = State::XOn;
				break;
			case 19: // Ctrl S
				state = State::XOff;
				break;
		}

	}
	if (state == State::XOn) {
		Serial.println(analogRead(PA0));
	}
}
This is only on the device side, doesn't handle receive from host side
non ideal but simple to implement, requires "escaping" ctrl-Q, ctrl-S for some implementations.
for 'one way' implementations (e.g. only the device respond to commands), this probably 'just works'
the benefit is this can be tested out in most ordinary serial terminals apps, e.g. https://www.putty.org/
Post Reply

Return to “Code snippets”