I have a solar regulator that puts out a 16 byte message which always starts 0xAA.
My goal is to use a Teensy to read that data stream and extract some of the data which I will ultimately display on a small screen. For example byte 4 & 5 contain the solar voltage (LSB & MSB).
I have some basic understanding of the principles which I believe are:
- Receive the serial data into buffer
- Define an array
- Identify the start byte
- Read the bytes from the buffer into the array
- Checksum the data
- Process the relevant bytes
- Send the resulting data to my display
I can do the basics (I think) which is to receive the data and print the bytes to the serial monitor but where I am struggling is how to identify the start marker (0xAA) so that the array always has the correct 16 bytes within it.
Secondly, once I get the correct bytes into the array how do select specific bytes (such as byte 4 & byte 5 to calcualte the solar voltage?
All help appreciated. My starting attempt at code is below.
/*
Votronic Solar Regulator March 2022
UART reader for output to display Battery Voltage, Solar Voltage and Solar Current
RJ11 6P6C on Votronic MPP165
Pin 1 = Signal
Pin 2 = 5v
Pin 3 = 12v
Pin 4 =
Pin 5 = 5v
Pin 6 = Ground
1000 Baud; 8 bit; No parity.
Byte
0 - sync byte 0xAA
1 - ID byte 0x1A
2 - Battery V LSB - U16 10mV/bit
3 - Battery V MSB
4 - Solar V LSB - U16 10mV/bit
5 - Solar V MSB
6 - Solar I LSB - S16 100mA/bit
7 - Solar I MSB
8 - 12 No data
13 - Battery status flags (charge phase - bulk, absorption, float)
14 - Regulator status flags (standby, no solar)
15 - Checksum XOR bytes 1-14
*/
#define solarData Serial1 //set name of data and serial port to use
void setup() {
solarData.begin(1000,SERIAL_8N1); //set baud and data type
Serial.begin(1000);
}
void loop() {
byte incomingByte;
if (solarData.available() > 0)
incomingByte = solarData.read();
Serial.print("UART data: ");
Serial.println(incomingByte, HEX);
delay(1000);
}
//initial thoughts on start marker code below
/*
void readsolarData() {
byte incomingByte;
byte startMarker = 0xAA
if (solarData.available() > 0) {
incomingByte = solarData.read();
if (incomingByte == startMarker)
}
*/