void setup()
{
Serial_RS232_Setup();
}
void Serial_RS232_Setup()
{
// Open USB serial communications
Serial.begin( 9600 );
// set the data rate for the SoftwareSerial port
mySerial.begin( 9600 );
}
//waiting to receive data from instrument
void loop()
{
if ( mySerial.available() )
{
serialRead=mySerial.readString();
StrtoChar();
Display_txt();
}
if ( Serial.available() )
{
mySerial.write(Serial.read() );
}
}
//Break received data into array of pointers
void StrtoChar()
{
// Define
String str = serialRead;
// Length (with one extra character for the null terminator)
int str_len = str.length() + 1;
// Prepare the character array (the buffer)
char char_array[str_len];
// Copy it over
str.toCharArray(char_array, str_len);
ptr = strtok(char_array, ","); // delimiter
while (ptr != NULL)
{
message[index] = ptr;
index++;
ptr = strtok(NULL, ",");
Serial.println(index);
}
for(i=0;i<index;i++)
{
Serial.print(i);
Serial.print("-");
Serial.println(message[i]);
}
}
//Print out data
void Display_txt()
{
Serial.print("from display txt");
for(i=0;i<index;i++)
{
Serial.print(i);
Serial.print("-");
Serial.println(message[i]);
}
}
Easiest solution is making char_array global.
Best way is to give your function a reference to your char_array where it can store the result as a function argument.
Basically you cannot return an array from a function like you would with an int or float.
@J-M-L had state a good point, actually I should consider more about the memory usage & size of cString (char s[] ) instead of my token size.
Fortunately, It's should be fine because maximum data size will not exceed more than 400 char. Also, I don't worry the bug because instrument will always output a separator '\r' to separate each line of data.
The maximum data per line will not exceed 400 char with '\r' at end of line.
I was changed the data size with 400 max size, also with '\r' as my separate. I had tried the program with my instrument and it able handle the result well.
char s [400];
int n = Serial. readBytesUntil ('\r', s, sizeof(s));
it looks like readBytesUntil() returns zero if the terminator is not found before reaching the sizeof(s) value. won't the following address that issue?
if (Serial.available ()) {
int n = Serial. readBytesUntil ('\n', s, sizeof(s));
if (0 == n)
n = sizeof(s)-1;
s [n] = 0; // terminate string
but strtok() works fine even when there is no separator which is the typical case when processing the final token which is delimited by a NULL instead of the separator
Serial.readBytesUntil() returns the number of characters read into the buffer. A 0 means that the length parameter <= 0, a time out occurred before any other input, or a termination character was found before any other input.
So you ll get 80 if that was the size of your array.
Ideally you declare the array with one extra byte and read up to sizeof-1
If the LF is not found, readBytesUntil will populate the entire array and return 80 and then you write a null char at index 80 which is the 81st entry and which does not exist, so you overrrun your array (valid indexes are from 0 to 79)