I’ve found a MAX232 in my junk box so have built a simple interface and the serial reading is now working fine.
rx_bytes=Serial3.readString();
for (int index=0; index <= 35; index++)
{
Serial.write(rx_bytes[index]);
}
Serial.println();
Is reliably delivering me a string like:
L|.|00C6E|.|.|6803|0015|0001|0B93|00
This code also works reliably and gives me the decimal of a four character hex string:
DAC_Value_String = rx_bytes.substring(5,9);
Serial.println(DAC_Value_String);
DAC_Value_String.toCharArray(DAC_Value_Chars, 5);
DAC_Value_Dec = strtoul (DAC_Value_Chars, NULL, 16);
Serial.println(DAC_Value_Dec);
However, when I try and extract the last two charaters in my string (currently 00) using this:
Holdover_Counter_String = rx_bytes.substring(34);
Serial.println(Holdover_Counter_String);
The first time round the loop I get 00 the second time I always get a blank string then the third time round the loop the program stops.
I dont understand why:
Serial.println(sizeof(rx_bytes));
always returns 6 when I have to loop 0 to 35 to print out the full string contents.
Seems I am now very confused over strings & chars and suspect I am indexing outside an array bounds somehow, but I can’t see where.
Here’s my complete code that crashes on itteration three, if I comment out the last two lines it runs reliably:
void setup() {
// put your setup code here, to run once:
Serial3.begin(4800,SERIAL_8N1);
Serial.begin(4800);
}
String rx_bytes = "";
char rx_bytes_chars[40];
String FLL_Status = "";
String Alarm_Latch = "";
String DAC_Value_String = "";
String Freq_Adj_Sign_String = "";
String Fine_Course_Freq_Adj_String = "";
String Sampled_Frequency_String = "";
String Sample_Counter_String = "";
String Accum_Freq_Diff_String = "";
String Holdover_Counter_String = "";
char DAC_Value_Chars[4];
unsigned long DAC_Value_Dec = 0;
void loop() {
// put your main code here, to run repeatedly:
if (Serial3.available())
{
rx_bytes=Serial3.readString();
for (int index=0; index <= 35; index++)
{
Serial.write(rx_bytes[index]);
}
Serial.println();
switch (rx_bytes[0]) // FLL Status
{
case 'L':
FLL_Status = "Locked";
break;
case 'U':
FLL_Status = "Unlocked";
break;
case 'H':
FLL_Status = "Holdover";
break;
case 'D':
FLL_Status = "Disabled";
break;
default:
FLL_Status = "Unknown";
break;
}
Serial.println(FLL_Status);
switch (rx_bytes[2]) // Alarm Latch
{
case 'U':
Alarm_Latch = "Unlocked";
break;
case 'B':
Alarm_Latch = "Bottom";
break;
case 'T':
Alarm_Latch = "Top";
break;
case 'H':
Alarm_Latch = "Holdover";
break;
default:
Alarm_Latch = "None";
break;
}
Serial.println(Alarm_Latch);
DAC_Value_String = rx_bytes.substring(5,9);
Serial.println(DAC_Value_String);
DAC_Value_String.toCharArray(DAC_Value_Chars, 5);
DAC_Value_Dec = strtoul (DAC_Value_Chars, NULL, 16);
Serial.println(DAC_Value_Dec);
switch (rx_bytes[11])
{
case '+':
Freq_Adj_Sign_String = "Increasing";
break;
case '-':
Freq_Adj_Sign_String = "Decreasing";
break;
case '=':
Freq_Adj_Sign_String = "Equal";
break;
default:
Freq_Adj_Sign_String = "Averaging Cycle";
break;
}
Serial.println(Freq_Adj_Sign_String);
switch (rx_bytes[13])
{
case 'C':
Fine_Course_Freq_Adj_String = "Coarse";
break;
case 'F':
Fine_Course_Freq_Adj_String = "Fine";
break;
default:
Fine_Course_Freq_Adj_String = "Averaging Cycle";
break;
}
Serial.println(Fine_Course_Freq_Adj_String);
Sampled_Frequency_String = rx_bytes.substring(14,18);
Serial.println(Sampled_Frequency_String);
Sample_Counter_String = rx_bytes.substring(19,23);
Serial.println(Sample_Counter_String);
Accum_Freq_Diff_String = rx_bytes.substring(24,28);
Serial.println(Accum_Freq_Diff_String);
Serial.println(sizeof(rx_bytes));
Holdover_Counter_String = rx_bytes.substring(34);
Serial.println(Holdover_Counter_String);
}
}