0.5.0 RS485

This commit is contained in:
Rob Tillaart 2024-02-03 12:28:23 +01:00
parent 1c1d6d4ab0
commit c106e4c062
13 changed files with 181 additions and 91 deletions

View File

@ -7,39 +7,39 @@
// URL: https://github.com/RobTillaart/RS485
#define NUL 0x00 // NULL char
#define SOH 0x01 // Start Of Header
#define STX 0x02 // Start of Text
#define ETX 0x03 // End of Text
#define EOT 0x04 // End of Transmission
#define ENQ 0x05 // ENQuiry
#define ACK 0x06 // ACKnowledge
#define BEL 0x07 // Bell
#define BS 0x08 // Back Space
#define TAB 0x09 // TAB char
#define LF 0x0A // Line Feed
#define VT 0x0B // Vertical TAB
#define FF 0x0C // Form Feed
#define CR 0x0D // Carriage Return
#define SO 0x0E // Shift Out
#define SI 0x0F // Shift In
#define DLE 0x10 // Data Link Escape
#define DC1 0x11 // Device Control 1
#define DC2 0x12 // Device Control 2
#define DC3 0x13 // Device Control 3
#define DC4 0x14 // Device Control 4
#define NAK 0x15 // NOT ACKnowledge
#define SYN 0x16 // Synchronous idle
#define ETB 0x17 // End of transmission block
#define CAN 0x18 // CANcel
#define EM 0x19 // End of Medium
#define SUB 0x1A // Substitute
#define ESC 0x1B // Escape
#define FS 0x1C // File Separator
#define GS 0x1D // Group Separator
#define RS 0x1E // Record Separator
#define US 0x1F // Unit Separator
#define DEL 0x7F // DELete
#define ASCII_NUL 0x00 // NULL char
#define ASCII_SOH 0x01 // Start Of Header
#define ASCII_STX 0x02 // Start of Text
#define ASCII_ETX 0x03 // End of Text
#define ASCII_EOT 0x04 // End of Transmission
#define ASCII_ENQ 0x05 // ENQuiry
#define ASCII_ACK 0x06 // ACKnowledge
#define ASCII_BEL 0x07 // Bell
#define ASCII_BS 0x08 // Back Space
#define ASCII_TAB 0x09 // TAB char
#define ASCII_LF 0x0A // Line Feed
#define ASCII_VT 0x0B // Vertical TAB
#define ASCII_FF 0x0C // Form Feed
#define ASCII_CR 0x0D // Carriage Return
#define ASCII_SO 0x0E // Shift Out
#define ASCII_SI 0x0F // Shift In
#define ASCII_DLE 0x10 // Data Link Escape
#define ASCII_DC1 0x11 // Device Control 1
#define ASCII_DC2 0x12 // Device Control 2
#define ASCII_DC3 0x13 // Device Control 3
#define ASCII_DC4 0x14 // Device Control 4
#define ASCII_NAK 0x15 // NOT ACKnowledge
#define ASCII_SYN 0x16 // Synchronous idle
#define ASCII_ETB 0x17 // End of transmission block
#define ASCII_CAN 0x18 // CANcel
#define ASCII_EM 0x19 // End of Medium
#define ASCII_SUB 0x1A // Substitute
#define ASCII_ESC 0x1B // Escape
#define ASCII_FS 0x1C // File Separator
#define ASCII_GS 0x1D // Group Separator
#define ASCII_RS 0x1E // Record Separator
#define ASCII_US 0x1F // Unit Separator
#define ASCII_DEL 0x7F // DELete
// -- END OF FILE --

View File

@ -6,6 +6,16 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).
## [0.5.0] - 2024-02-01
- merge PR #28, Fix for FS conflict on ESP32 (kudos to DoomHammer)
- all ASCII control characters get a **ASCII_** prefix.
- update MESSAGES.MD with new control characters.
- add unit test for the new ASCII control characters.
- update readme.md.
- add new control characters to keywords.txt
----
## [0.4.0] - 2023-12-18
- fix **write(array, length)**
- add two examples (ack_nack + controller)

View File

@ -1,6 +1,6 @@
MIT License
Copyright (c) 2017-2023 Rob Tillaart
Copyright (c) 2017-2024 Rob Tillaart
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@ -32,14 +32,14 @@ than one iteration.
Possible useful char codes.
| commando | value | meaning |
|:----------:|:-------:|:----------|
| SOH | 0x01 | start of header
| STX | 0x02 | start of text
| ETX | 0x03 | end of text
| EOT | 0x04 | end of transmission
| ACK | 0x06 | ACKnowledge
| NAK | 0x15 | Not Acknowledge
| CAN | 0x18 | CANcel
|:-----------:|:-------:|:----------|
| ASCII_SOH | 0x01 | start of header
| ASCII_STX | 0x02 | start of text
| ASCII_ETX | 0x03 | end of text
| ASCII_EOT | 0x04 | end of transmission
| ASCII_ACK | 0x06 | ACKnowledge
| ASCII_NAK | 0x15 | Not Acknowledge
| ASCII_CAN | 0x18 | CANcel
See also **ASCII_CONTROL.h**
@ -69,7 +69,7 @@ Using a device id to send the command to.
command :=
{
SOH start of header, (attention new command)
ASCII_SOH start of header, (attention new command)
deviceID to (is it for me?) should not equal SOH
command (if so, exec command)
bytes expected (to generate the bytes)
@ -88,7 +88,7 @@ Command and answer have same layout.
Uses device ID's to address specific device.
{
SOH start of header 0x01
ASCII_SOH start of header 0x01
deviceID to
deviceID sender
length length of message
@ -96,7 +96,7 @@ Uses device ID's to address specific device.
optional extend with:
checksum optional
EOT optional (end of transmission)
ASCII_EOT optional (end of transmission)
}
----
@ -107,24 +107,24 @@ More complex package with multiple fields and Checksum / CRC per message.
In fact this is an elaborated variation on previous example.
SOH start of header
ASCII_SOH start of header
deviceID to
deviceID sender
fields 0 or more
length length of field 1
STX start of text
ASCII_STX start of text
message idem
CHECKSUM idem, message only!
ETX end of text
ASCII_ETX end of text
length length of field 2
STX start of text
ASCII_STX start of text
message idem
CHECKSUM idem, message only!
ETX end of text
ASCII_ETX end of text
...
EOT end of transmission
ASCII_EOT end of transmission
----
@ -159,7 +159,7 @@ An example is remote reading a sensor or an analog port.
To improve the reliability the answer can be a "packet".
command = ID
answer = { SOH, IDmaster, IDslave, length, value }
answer = { ASCII_SOH, IDmaster, IDslave, length, value }

View File

@ -28,6 +28,10 @@ needs to be tested.
The 0.2.0 version of the library has no (tested) protocol for multi-byte
messages so the user must implement such on top of this class.
The 0.5.0 version uses new defines for the **ASCII_CONTROL.h** to fix a
name conflict on the ESP32 platform.
The new defines all have got an **ASCII_** prefix, e.g. **ASCII_FS**
#### Connection schema

View File

@ -2,7 +2,7 @@
// FILE: RS485.cpp
// AUTHOR: Rob Tillaart
// DATE: 30-okt-2017
// VERSION: 0.4.0
// VERSION: 0.5.0
// PURPOSE: Arduino library for RS485 modules (MAX485)
// URL: https://github.com/RobTillaart/RS485
@ -124,29 +124,29 @@ size_t RS485::write(uint8_t * array, uint8_t length)
//
// commando value meaning
//
// SOH 0x01 start of header
// STX 0x02 start of text
// ETX 0x03 end of text
// EOT 0x04 end of transmission
// ASCII_SOH 0x01 start of header
// ASCII_STX 0x02 start of text
// ASCII_ETX 0x03 end of text
// ASCII_EOT 0x04 end of transmission
//
// optional
// ACK 0x06 ACKnowledge
// NAK 0x15 Not Acknowledge
// CAN 0x18 CANcel
// ASCII_ACK 0x06 ACKnowledge
// ASCII_NAK 0x15 Not Acknowledge
// ASCII_CAN 0x18 CANcel
//
///////////////////////////////////////////////////////
//
// A message has the following layout
//
// SOH start of header
// ASCII_SOH start of header
// deviceID to
// deviceID sender
// length length of message
// STX start of text
// ASCII_STX start of text
// message idem
// CHECKSUM idem, message only!
// ETX end of text
// EOT end of transmission
// ASCII_ETX end of text
// ASCII_EOT end of transmission
//
size_t RS485::send(uint8_t receiverID, uint8_t msg[], uint8_t len)
@ -155,19 +155,19 @@ size_t RS485::send(uint8_t receiverID, uint8_t msg[], uint8_t len)
uint8_t CHKSUM = 0;
setTXmode(); // transmit mode
_stream->write(SOH);
_stream->write(ASCII_SOH);
n += _stream->write(receiverID); // TO
n += _stream->write(_deviceID); // FROM
n += _stream->write(len); // LENGTH BODY
n += _stream->write(STX);
n += _stream->write(ASCII_STX);
for (int i = 0; i < len; i++)
{
n += _stream->write(msg[i]);
CHKSUM ^= msg[i]; // Simple XOR checksum.
}
n += _stream->write(CHKSUM);
n += _stream->write(ETX);
n += _stream->write(EOT);
n += _stream->write(ASCII_ETX);
n += _stream->write(ASCII_EOT);
_stream->flush();
setRXmode(); // receive mode
@ -196,7 +196,7 @@ bool RS485::receive(uint8_t &senderID, uint8_t msg[], uint8_t &msglen)
{
// waiting for new packet
case 0:
if (v == SOH)
if (v == ASCII_SOH)
{
_bidx = 0; // start new packet
CHKSUM = 0;
@ -224,9 +224,9 @@ bool RS485::receive(uint8_t &senderID, uint8_t msg[], uint8_t &msglen)
state = 4;
break;
// expect STX
// expect ASCII_STX
case 4:
if (v == STX) state = 5;
if (v == ASCII_STX) state = 5;
else state = 99;
break;
@ -252,15 +252,15 @@ bool RS485::receive(uint8_t &senderID, uint8_t msg[], uint8_t &msglen)
length--;
break;
// expect ETX
// expect ASCII_ETX
case 6:
if (v == ETX) state = 7;
if (v == ASCII_ETX) state = 7;
else state = 99;
break;
// expect EOT
// expect ASCII_EOT
case 7:
if (v == EOT)
if (v == ASCII_EOT)
{
msglen = _bidx;
for (int i = 0; i < msglen; i++)
@ -274,8 +274,8 @@ bool RS485::receive(uint8_t &senderID, uint8_t msg[], uint8_t &msglen)
break;
// SKIP until next packet
case 99: // wait for EOT in case of error
if (v == EOT) state = 0;
case 99: // wait for ASCII_EOT in case of error
if (v == ASCII_EOT) state = 0;
break;
}

View File

@ -3,7 +3,7 @@
// FILE: RS485.h
// AUTHOR: Rob Tillaart
// DATE: 30-okt-2017
// VERSION: 0.4.0
// VERSION: 0.5.0
// PURPOSE: Arduino library for RS485 modules
// URL: https://github.com/RobTillaart/RS485
@ -11,7 +11,7 @@
#include "Arduino.h"
#include "ASCII_CONTROL.h"
#define RS485_LIB_VERSION (F("0.4.0"))
#define RS485_LIB_VERSION (F("0.5.0"))
class RS485 : public Stream

View File

@ -49,10 +49,10 @@ void loop()
uint8_t answer = rs485.read();
switch (answer)
{
case ACK:
case ASCII_ACK:
Serial.println(" ACK");
break;
case NAK:
case ASCII_NAK:
Serial.println(" NAK");
break;
default:

View File

@ -43,11 +43,11 @@ void loop()
if (isPrintable(c))
{
rs485.write(ACK);
rs485.write(ASCII_ACK);
}
else
{
rs485.write(NAK);
rs485.write(ASCII_NAK);
}
}

View File

@ -25,3 +25,41 @@ RS485_LIB_VERSION LITERAL1
RS485_YIELD_ENABLE LITERAL1
ASCII_NUL LITERAL1
ASCII_SOH LITERAL1
ASCII_STX LITERAL1
ASCII_ETX LITERAL1
ASCII_EOT LITERAL1
ASCII_ENQ LITERAL1
ASCII_ACK LITERAL1
ASCII_BEL LITERAL1
ASCII_BS LITERAL1
ASCII_TAB LITERAL1
ASCII_LF LITERAL1
ASCII_VT LITERAL1
ASCII_FF LITERAL1
ASCII_CR LITERAL1
ASCII_SO LITERAL1
ASCII_SI LITERAL1
ASCII_DLE LITERAL1
ASCII_DC1 LITERAL1
ASCII_DC2 LITERAL1
ASCII_DC3 LITERAL1
ASCII_DC4 LITERAL1
ASCII_NAK LITERAL1
ASCII_SYN LITERAL1
ASCII_ETB LITERAL1
ASCII_CAN LITERAL1
ASCII_EM LITERAL1
ASCII_SUB LITERAL1
ASCII_ESC LITERAL1
ASCII_FS LITERAL1
ASCII_GS LITERAL1
ASCII_RS LITERAL1
ASCII_US LITERAL1
ASCII_DEL LITERAL1

View File

@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/RS485"
},
"version": "0.4.0",
"version": "0.5.0",
"license": "MIT",
"frameworks": "*",
"platforms": "*",

View File

@ -1,5 +1,5 @@
name=RS485
version=0.4.0
version=0.5.0
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=RS485 library for Arduino.

View File

@ -47,6 +47,44 @@ unittest_teardown()
}
unittest(test_ASCII_CONTROLS)
{
assertEqual(ASCII_NUL, 0x00);
assertEqual(ASCII_SOH, 0x01);
assertEqual(ASCII_STX, 0x02);
assertEqual(ASCII_ETX, 0x03);
assertEqual(ASCII_EOT, 0x04);
assertEqual(ASCII_ENQ, 0x05);
assertEqual(ASCII_ACK, 0x06);
assertEqual(ASCII_BEL, 0x07);
assertEqual(ASCII_BS , 0x08);
assertEqual(ASCII_TAB, 0x09);
assertEqual(ASCII_LF , 0x0A);
assertEqual(ASCII_VT , 0x0B);
assertEqual(ASCII_FF , 0x0C);
assertEqual(ASCII_CR , 0x0D);
assertEqual(ASCII_SO , 0x0E);
assertEqual(ASCII_SI , 0x0F);
assertEqual(ASCII_DLE, 0x10);
assertEqual(ASCII_DC1, 0x11);
assertEqual(ASCII_DC2, 0x12);
assertEqual(ASCII_DC3, 0x13);
assertEqual(ASCII_DC4, 0x14);
assertEqual(ASCII_NAK, 0x15);
assertEqual(ASCII_SYN, 0x16);
assertEqual(ASCII_ETB, 0x17);
assertEqual(ASCII_CAN, 0x18);
assertEqual(ASCII_EM , 0x19);
assertEqual(ASCII_SUB, 0x1A);
assertEqual(ASCII_ESC, 0x1B);
assertEqual(ASCII_FS , 0x1C);
assertEqual(ASCII_GS , 0x1D);
assertEqual(ASCII_RS , 0x1E);
assertEqual(ASCII_US , 0x1F);
assertEqual(ASCII_DEL, 0x7F);
}
unittest(test_constructor)
{
Serial.begin(115200);