how to make Serial.swap() working
Posted: Mon Oct 14, 2019 4:25 pm
Hi
I'm using an LoLin ESP8266 NodeMcu V3.
I need a second UART port for communicating with a device over an RS232/UART converter.
I want to keep the embedded UART/USB for programming/debug purpose.
I've been directed to use either softwareserial (witch seems to give me some error at 2400bps) or Serial.swap().
Here is my piece of code that doesn't want to work :
I've read many things about Serial.swap(), but I'm still unclear.
I've sometime seen
I also found this :
https://github.com/esp8266/Arduino/issues/2427
It explains that a lot of care should be taken to have a correct behaviour, but far being easy to understand. Is that all of this really needed to make use of that Serial.swap() ? It's not informed in the documentation.
What line to follow ?
I'm using an LoLin ESP8266 NodeMcu V3.
I need a second UART port for communicating with a device over an RS232/UART converter.
I want to keep the embedded UART/USB for programming/debug purpose.
I've been directed to use either softwareserial (witch seems to give me some error at 2400bps) or Serial.swap().
Here is my piece of code that doesn't want to work :
Code: Select all
void request_inverter_state(const int &periodicite_request = 10000)
{
static unsigned long last_request = 0;
static bool info_flag;
if ((now - last_request > periodicite_request) && !info_flag)
{
Serial.swap();
delay(100);
Serial.write("\rQ1\r");
Serial.swap();
delay(100);
Serial.println("inverter_state requested");
//info_flag = 1;
info_flag = 0;
last_request = now;
}
if (info_flag)
{
char c = ' ';
int length = 40;
char state [41];
char termChar = '\r'; //CR is 13 DEC on ASCII table
byte index = 0;
boolean haveNewData = false;
Serial.swap();
if (Serial.available())
{
c = Serial.read();
if (c != termChar)
{
state[index] = c;
index = index + 1;
}
else
{
state[index] = '\0';
index = 0;
haveNewData = true;
info_flag = 0;
}
}
Serial.swap();
if (haveNewData)
{
Serial.println (state);
client.publish("inverter/state", state);
haveNewData = 0;
}
}
}
I've read many things about Serial.swap(), but I'm still unclear.
I've sometime seen
Code: Select all
Serial.swap(15);
I also found this :
https://github.com/esp8266/Arduino/issues/2427
It explains that a lot of care should be taken to have a correct behaviour, but far being easy to understand. Is that all of this really needed to make use of that Serial.swap() ? It's not informed in the documentation.
What line to follow ?