Handling UART characters and Strings in Arduino

bdoan

Well-known member
I am wanting to compile text buffers for each UART port.
With all other IDE and compilers that I have used in the past, this method worked fine.

But apparently Arduino handles strings differently.
I want to be able to create a string from characters received on a port and terminate the string when I receive a 0x0D (cr)
I also want to test and parse the string based on character positions.

Please advise.

Code:
void USBrx()
{
char USBrxData[20],BLErxData[20],DSPrxData[20];
char receivedChar;
int index;
if (Serial.available() > 0)   // USB UART RX  = 0
    {
    receivedChar = Serial.read();
    if (receivedChar == 0x0D)
      {
      USBrxData[USBrxptr+1] = 0;
      DecodeCmd();
      }
    else
      USBrxData[USBrxptr++] = receivedChar;
    }
for (index=0;index<20;index++)
  USBrxData[index] = 0;     // clear buffer


// *********************************************************************
void DecodeCmd() 
{
int ptr;
unsigned short DACvolts;
for (ptr=0;ptr<20;ptr++)
  Serial.print(USBrxData[ptr]);
if (USBrxData[0] == '<') // BLE serial
  {
  SendToBLE();
  Serial.print("toBLE");
}
else
if (USBrxData[0] == '>') // DSP serial
  {
  Serial.print(">");
  SendToDSP();
  }
else
if (USBrxData[0] == 'R')
  {
  int sensorValue = analogRead(A17);
  float voltage = sensorValue * (5.0 / 1023.0);
  Serial.println(voltage);
  }
else
if (USBrxData[0] == 'V')
  {
  DACvolts = 100*(USBrxData[1]-48) + 10*(USBrxData[2]-48) + USBrxData[3]-48;
  WriteToDAC(DACvolts);
  Serial.print(DACvolts);
  }
for (ptr=0;ptr<20;ptr++)    // clear rx buffer
  USBrxData[ptr] = 0;
USBrxptr = 0;
}

}
 
On Teensy with native USB Serial is not a UART shared port.
if (Serial.available() > 0) // USB UART RX = 0
UART ports are Serial1 , Serial2, ...

This page may add more info: https://www.pjrc.com/teensy/td_uart.html
Does this change the way I handle characters and strings? The ports work properly but my problem is with compiling character buffers from individual characters and testing specific positions in the buffers for matching characters. Also retransmitting these buffers into another port.
 
No, the ports work the same - but the proper naming for setup and use is required.

From the p#1 code snippet (incomplete code) the only thing obvious was indicated in the post #2.

Set up a simple example to test and if you don't find it to work as expected post the code and note what is or is not working as expected.
 
if (USBrxData[0] == '>') // DSP serial
{
Serial.print(">");
SendToDSP()
}

This is not detected > character at the first position in the string.

Also I cannot print out the string that I have assembled from incoming characters.

My method of building a string from individual characters does not seem to work in Arduino.
 
C strings can certainly be built on Teensy using the Arduino functions.

Can't help more not seeing complete simple code. And knowing where data is coming from and what results are seen on receiving.
 
But apparently Arduino handles strings differently.

Yes, Arduino strings are annoying and a pain to work with. But you're not using them, you're using good old c style null terminated char arrays.
They work just fine.

The only issue I can see in the code you posted is that you'll get a buffer overflow and memory corruption if you go more than 20 characters without a 0x0D.
 
Yes, Arduino strings are annoying and a pain to work with. But you're not using them, you're using good old c style null terminated char arrays.
They work just fine.

The only issue I can see in the code you posted is that you'll get a buffer overflow and memory corruption if you go more than 20 characters without a 0x0D.
I am trying to use code that has worked for me for 20 years on other platforms but does not on Arduino.
I need to be able to compile a text buffer and decode on the basis of individual characters in specific positions.

Please Advise

Code:
// Globals
char USBrxData[20];

//------------------
void USBrx()
{
char RXChar;
int index;
if (Serial.available() > 0) 
    {
    RXChar = Serial.read();        // read a character
    if (RXChar == 0x0D )
          {
          USBrxData[USBrxptr+1] = 0;  // terminate string
          DecodeCmd();                  // Decode command
          }
        else
          USBrxData[USBrxptr++] = RXChar;    //add character to array
        }
    // will add code for buffer overflow
    }
}

void DecodeCmd()
{
int ptr;
unsigned short DACvolts;
Serial.print(USBrxptr);                // indicate length of string in USBrxData
if (USBrxData[0] == 'C')            // !!!!! Doesn't work !!!!
//                                     // neither does if (USBrxData[1] == 'C')
  {
  Serial.print("isC");
  }
}
 
code that has worked for me for 20 years on other platforms
Posted legacy snippets are not complete code.

Already noted that Serial.available() will be using USB not any UART.

Changed code would use Serial1.available() or Serial2.available(), etc as is appropriate.

It isn't clear what port and what the end goal is for current use so it isn't even possible for another post to show the code that would work.

.available() and .read() do work for detection and read of single ascii characters so they can be used to properly assemble a null terminated 'C' string as noted in p#6. They require the proper .begin() for the desired UART Serial[1-8]. All eight T_4.1 Serial UARTS are tested and known to work when properly connected Rx/Tx/Gnd with the expected baud and parity etc.
 
Posted legacy snippets are not complete code.

Already noted that Serial.available() will be using USB not any UART.

Changed code would use Serial1.available() or Serial2.available(), etc as is appropriate.

It isn't clear what port and what the end goal is for current use so it isn't even possible for another post to show the code that would work.

.available() and .read() do work for detection and read of single ascii characters so they can be used to properly assemble a null terminated 'C' string as noted in p#6. They require the proper .begin() for the desired UART Serial[1-8]. All eight T_4.1 Serial UARTS are tested and known to work when properly connected Rx/Tx/Gnd with the expected baud and parity etc.
Maybe I didn’t make my question very clear. But I’ve got it working using my original method.

Thanks
 
The problem never was with addressing the serial ports. It was with handling character strings.
 
Back
Top