Arduino Library for UART base Displays

Wisecoco Uart Displays
Guide Manual-Wisecoco_2.0.1_20240418.pdf (1.9 MB)
I'm currently working on a project that involves UART communication and I'm looking for a universal library that can handle sending and receiving data, along with checksum (CRC) support. Specifically, I'm wondering if any existing libraries cover these aspects or if companies typically provide libraries for UART communication with CRC. If such a library isn't readily available, I would appreciate any examples or resources that demonstrate how to implement UART communication, including checksum validation.
Thank You : )

Only buy from sellers offering required Arduino libraries.

1 Like

The Serial class and its inheritance structure (Stream capabilities) offers basically what you need to send and received data asynchronously.

The CRC piece is more tricky as it does not mean much in general.

It's always in the context of a protocol and you need to know what the frames look like to know where the CRC information is.

Also checksum types range from simple checksums, like the sum of bytes modulo 8, to more complex methods like CRC-8, CRC-16, CRC-32, CRC-CCITT, CRC-16-CCITT, CRC-16-IBM, CRC-32C, CRC-64, or CRC-16-32,... with various polynomial-based cyclic redundancy checks used for more robust error detection. So it's device specific.

1 Like

Frame: A5 31 00 02 00 00 CC
Data to be checksummed: 31 00 02 00 00
Sum = 0x31 + 0x00 + 0x02 + 0x00 + 0x00 = 0x33
Checksum = 0x33 XOR 0xFF = CC
xor based checksum

OK

so the frame is always 7 byte long and you have 0xA5 as a start marker, the payload is the next 5 bytes and then you have a checksum ?

1 Like

yes correct i will try with serial event i think thats for interrupt like serial events..

For the Arduino this is it:

1 Like

I would advise against that. Handle this yourself in your loop.

I would suggest to study Serial Input Basics to handle this and to handle the frame you might benefit from studying state machines. Here is a small introduction to the topic: Yet another Finite State Machine introduction

1 Like

I had a bit of time, so here is an example you can try in wokwi

you can't send binary with the wokwi serial monitor but your start maker can be obtained by sending the YEN ¥ symbol. So you can engineer frames just using ASCII that will work.

try sending ¥XYZab1¥XYZac0¥XYt12w for example. Those are 3 frames with proper start marker and checksum.

For example the first frame is ¥XYZab1 where ¥ is the start marker (will send 0xA5) then XYZab is the payload and the cksum of these 5 bytes is the ASCII code of 1 so ¥XYZab1 will be a correct frame.

click to see the code
 /* ============================================
  code is placed under the MIT license
  Copyright (c) 2024 J-M-L
  For the Arduino Forum : https://forum.arduino.cc/u/j-m-l

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.
  ===============================================
*/


// test with "¥XYZab1¥XYZac0¥XYt12w" which will match the frame format for 3 frames

const byte startMarker = 0xA5;   // Start marker
const byte payloadLength = 5;    // Length of the payload
byte payload[payloadLength];     // Buffer to hold the payload
byte payloadIndex = 0;           // Current index in the payload buffer
enum State { WAIT_FOR_START, RECEIVE_PAYLOAD, RECEIVE_CHECKSUM, PROCESS_FRAME } currentState = WAIT_FOR_START;

void processSerialData() {
  while (Serial.available()) {
    byte incomingByte = Serial.read(); // Read the incoming byte

    switch (currentState) {
      case WAIT_FOR_START:
        if (incomingByte == startMarker) {
          payloadIndex = 0;              // Start a new frame
          currentState = RECEIVE_PAYLOAD;
        }
        break;

      case RECEIVE_PAYLOAD:
        payload[payloadIndex++] = incomingByte; // Store payload byte
        if (payloadIndex >= payloadLength) {
          currentState = RECEIVE_CHECKSUM;
        }
        break;

      case RECEIVE_CHECKSUM:
        { // compound statement needed because we define a local variable to this case
          byte cksum = 0;
          // Calculate checksum
          for (int i = 0; i < payloadLength; i++) cksum += payload[i];
          cksum ^= 0xFF;

          // Verify checksum
          if (cksum == incomingByte) {
            currentState = PROCESS_FRAME; // Move to processing state
          } else {
            Serial.println("Checksum error");
            Serial.print("Calculated Checksum = "); Serial.println(cksum);
            Serial.print("Expected Checksum = "); Serial.println(incomingByte);
            currentState = WAIT_FOR_START; // Reset state machine
          }
        }
        break;

      case PROCESS_FRAME:
        Serial.print("Frame received correctly: ");
        for (byte b : payload) {
          if (b < 0x10) Serial.write('0'); // Print leading zero for single-digit hex
          Serial.print(b, HEX);
          Serial.write(' ');
        }
        Serial.println();
        currentState = WAIT_FOR_START; // Reset to waiting for the next start marker
        break;
    }
  }
}

void setup() {
  Serial.begin(115200);  // Initialize Serial with baud rate 115200
  Serial.println(F("ready"));
}

void loop() {
  processSerialData();   // Call the function to process serial data
  // here you can do other stuff
}

1 Like

Superb JML for the Detailed guide : )
This enum state concept is new to me :heart:
Will try to implement this method....

Read about state machines. Here is a small introduction to the topic: Yet another Finite State Machine introduction

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.