Post topics, source code that relate to the Arduino Platform

User avatar
By hary
#84162 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 :
Code: Select allvoid 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 allSerial.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 ?
User avatar
By QuickFix
#84170 From the core reference:
Reference wrote:At boot, Serial uses UART0, which is mapped to pins GPIO1 (TX) and GPIO3 (RX).
Serial may be remapped to GPIO15 (TX) and GPIO13 (RX) by calling Serial.swap() after Serial.begin.
Calling swap again maps UART0 back to GPIO1 and GPIO3.

Serial1 uses UART1, TX pin is GPIO2.
UART1 can not be used to receive data because normally its RX pin is occupied for flash chip connection.

If Serial1 is not used and Serial is not swapped - TX for UART0 can be mapped to GPIO2 instead by calling Serial.set_tx(2) after Serial.begin or directly with Serial.begin(baud, config, mode, 2).
User avatar
By Bonzo
#84172 From one of my old posts:

Thank you for the info Good Science.

I finally have something working; the main problem was an incompetent programmer - myself

I persuaded a experienced programmer to come over and take a look and my main problem was only reading one character; anyway that is now sorted.

I am using the Serial.swap() command but had some hiccups - at one point the swap was out of sinc and it was sending the data to the sensor and crashing it.

Notes:
Put a delay after the swap - delay(100) seems to short and I am currently using delay(500)

Depending what you are doing before calling the swap you may need to put a delay before it as well. I was loosing a Serial.print()

Currently the sensor and serial monitor are both on the same baudrate

I can now send and receive the data over pins 7 & 8 to the sensor and power the project and see the serial output through the USB connector.