From c106e4c0620f533f8ba7e8d0188d94390103c354 Mon Sep 17 00:00:00 2001 From: Rob Tillaart Date: Sat, 3 Feb 2024 12:28:23 +0100 Subject: [PATCH] 0.5.0 RS485 --- libraries/RS485/ASCII_CONTROL.h | 66 +++++++++---------- libraries/RS485/CHANGELOG.md | 10 +++ libraries/RS485/LICENSE | 2 +- libraries/RS485/MESSAGES.MD | 38 +++++------ libraries/RS485/README.md | 4 ++ libraries/RS485/RS485.cpp | 60 ++++++++--------- libraries/RS485/RS485.h | 4 +- .../RS485_master_ACK_NACK.ino | 4 +- .../RS485_slave_ACK_NACK.ino | 4 +- libraries/RS485/keywords.txt | 38 +++++++++++ libraries/RS485/library.json | 2 +- libraries/RS485/library.properties | 2 +- libraries/RS485/test/unit_test_001.cpp | 38 +++++++++++ 13 files changed, 181 insertions(+), 91 deletions(-) diff --git a/libraries/RS485/ASCII_CONTROL.h b/libraries/RS485/ASCII_CONTROL.h index da21ca48..d1f41217 100644 --- a/libraries/RS485/ASCII_CONTROL.h +++ b/libraries/RS485/ASCII_CONTROL.h @@ -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 -- diff --git a/libraries/RS485/CHANGELOG.md b/libraries/RS485/CHANGELOG.md index 3084dd6d..e02f454d 100644 --- a/libraries/RS485/CHANGELOG.md +++ b/libraries/RS485/CHANGELOG.md @@ -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) diff --git a/libraries/RS485/LICENSE b/libraries/RS485/LICENSE index 562b0ffe..a4357241 100644 --- a/libraries/RS485/LICENSE +++ b/libraries/RS485/LICENSE @@ -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 diff --git a/libraries/RS485/MESSAGES.MD b/libraries/RS485/MESSAGES.MD index 8d594c07..16dcaf36 100644 --- a/libraries/RS485/MESSAGES.MD +++ b/libraries/RS485/MESSAGES.MD @@ -31,15 +31,15 @@ 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 +| commando | value | meaning | +|:-----------:|:-------:|:----------| +| 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 } diff --git a/libraries/RS485/README.md b/libraries/RS485/README.md index fd519392..0663af53 100644 --- a/libraries/RS485/README.md +++ b/libraries/RS485/README.md @@ -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 diff --git a/libraries/RS485/RS485.cpp b/libraries/RS485/RS485.cpp index d14bcb72..7e10fc88 100644 --- a/libraries/RS485/RS485.cpp +++ b/libraries/RS485/RS485.cpp @@ -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 -// deviceID to -// deviceID sender -// length length of message -// STX start of text -// message idem -// CHECKSUM idem, message only! -// ETX end of text -// EOT end of transmission +// ASCII_SOH start of header +// deviceID to +// deviceID sender +// length length of message +// ASCII_STX start of text +// message idem +// CHECKSUM idem, message only! +// 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; } diff --git a/libraries/RS485/RS485.h b/libraries/RS485/RS485.h index 0fb9c9ab..1cb60c94 100644 --- a/libraries/RS485/RS485.h +++ b/libraries/RS485/RS485.h @@ -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 diff --git a/libraries/RS485/examples/RS485_master_ACK_NACK/RS485_master_ACK_NACK.ino b/libraries/RS485/examples/RS485_master_ACK_NACK/RS485_master_ACK_NACK.ino index 5147a0c3..54e31b93 100644 --- a/libraries/RS485/examples/RS485_master_ACK_NACK/RS485_master_ACK_NACK.ino +++ b/libraries/RS485/examples/RS485_master_ACK_NACK/RS485_master_ACK_NACK.ino @@ -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: diff --git a/libraries/RS485/examples/RS485_slave_ACK_NACK/RS485_slave_ACK_NACK.ino b/libraries/RS485/examples/RS485_slave_ACK_NACK/RS485_slave_ACK_NACK.ino index bad797b1..e4aa902d 100644 --- a/libraries/RS485/examples/RS485_slave_ACK_NACK/RS485_slave_ACK_NACK.ino +++ b/libraries/RS485/examples/RS485_slave_ACK_NACK/RS485_slave_ACK_NACK.ino @@ -43,11 +43,11 @@ void loop() if (isPrintable(c)) { - rs485.write(ACK); + rs485.write(ASCII_ACK); } else { - rs485.write(NAK); + rs485.write(ASCII_NAK); } } diff --git a/libraries/RS485/keywords.txt b/libraries/RS485/keywords.txt index db7f6c2b..00a39808 100644 --- a/libraries/RS485/keywords.txt +++ b/libraries/RS485/keywords.txt @@ -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 \ No newline at end of file diff --git a/libraries/RS485/library.json b/libraries/RS485/library.json index 29412f68..b97ac9d9 100644 --- a/libraries/RS485/library.json +++ b/libraries/RS485/library.json @@ -15,7 +15,7 @@ "type": "git", "url": "https://github.com/RobTillaart/RS485" }, - "version": "0.4.0", + "version": "0.5.0", "license": "MIT", "frameworks": "*", "platforms": "*", diff --git a/libraries/RS485/library.properties b/libraries/RS485/library.properties index d91774b2..19b4814f 100644 --- a/libraries/RS485/library.properties +++ b/libraries/RS485/library.properties @@ -1,5 +1,5 @@ name=RS485 -version=0.4.0 +version=0.5.0 author=Rob Tillaart maintainer=Rob Tillaart sentence=RS485 library for Arduino. diff --git a/libraries/RS485/test/unit_test_001.cpp b/libraries/RS485/test/unit_test_001.cpp index ebc29d51..ac37c411 100644 --- a/libraries/RS485/test/unit_test_001.cpp +++ b/libraries/RS485/test/unit_test_001.cpp @@ -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);