309 lines
5.8 KiB
C++
Raw Normal View History

2022-05-24 17:24:57 +02:00
//
// FILE: RS485.cpp
// AUTHOR: Rob Tillaart
// DATE: 30-okt-2017
2023-05-11 11:14:17 +02:00
// VERSION: 0.2.5
2022-05-24 17:24:57 +02:00
// PURPOSE: Arduino library for RS485 modules (MAX485)
// URL: https://github.com/RobTillaart/RS485
#include "RS485.h"
2022-11-23 16:14:02 +01:00
// - a stream - SWserial of HW serial (preferred)
// - a pin for TX- enable line
// - slave ID, or 0 as master (or don't care)
2022-05-24 17:24:57 +02:00
RS485::RS485(Stream * stream, uint8_t sendPin, uint8_t deviceID)
{
_stream = stream;
_sendPin = sendPin;
_deviceID = deviceID;
2022-05-24 17:24:57 +02:00
pinMode(_sendPin, OUTPUT);
2022-05-25 12:32:10 +02:00
setRXmode(); // receiver mode
2022-05-24 17:24:57 +02:00
}
2023-05-11 11:14:17 +02:00
// 0.3.0
// void RS485::begin(uint32_t baudRate)
// {
// _stream->begin(baudRate);
// setMicrosPerByte(baudRate);
// }
void RS485::setMicrosPerByte(uint32_t baudRate)
{
// count 11 bits time per byte
_microsPerByte = (11 * 1000000) / baudRate ;
}
uint32_t RS485::getMicrosPerByte()
{
return _microsPerByte;
}
uint8_t RS485::getDeviceID()
{
return _deviceID;
}
///////////////////////////////////////////////////////
//
// STREAM INTERFACE
//
2022-05-24 17:24:57 +02:00
int RS485::available()
{
return _stream->available();
}
2022-11-23 16:14:02 +01:00
2022-05-24 17:24:57 +02:00
int RS485::read()
{
return _stream->read();
}
2022-11-23 16:14:02 +01:00
2022-05-24 17:24:57 +02:00
int RS485::peek()
{
return _stream->peek();
}
2022-11-23 16:14:02 +01:00
2022-05-24 17:24:57 +02:00
void RS485::flush()
{
_stream->flush();
}
2022-11-23 16:14:02 +01:00
2022-05-24 17:24:57 +02:00
size_t RS485::write(uint8_t c)
{
2022-05-25 12:32:10 +02:00
setTXmode(); // transmit mode
2022-05-24 17:24:57 +02:00
size_t n = _stream->write(c);
delayMicroseconds(_microsPerByte);
2022-05-25 12:32:10 +02:00
setRXmode(); // receiver mode
2022-05-24 17:24:57 +02:00
return n;
}
2022-05-26 08:56:53 +02:00
size_t RS485::write(char * array, uint8_t length)
{
return write((uint8_t *)array, length);
}
///////////////////////////////////////////////////////
//
// discussion about write and yield see
2022-05-26 08:56:53 +02:00
// - https://github.com/RobTillaart/RS485/issues/2
//
2022-11-23 16:14:02 +01:00
// #define RS485_YIELD_ENABLE 1
2022-05-26 08:56:53 +02:00
#ifdef RS485_YIELD_ENABLE
2022-11-23 16:14:02 +01:00
// smallest and slightly fastest.
2022-05-26 08:56:53 +02:00
size_t RS485::write(uint8_t * array, uint8_t length)
{
uint8_t n = 0;
for (uint8_t i = 0; i < length; i++)
{
n += write(array[i]);
yield();
}
return n;
}
#else
2022-05-26 08:56:53 +02:00
// 0.2.1 version
2022-05-26 08:56:53 +02:00
// no yield() calls - might be blocking...
2022-05-24 17:24:57 +02:00
size_t RS485::write(uint8_t * array, uint8_t length)
{
2022-05-25 12:32:10 +02:00
setTXmode(); // transmit mode
2022-05-24 17:24:57 +02:00
size_t n = _stream->write(array, length);
2022-05-26 08:56:53 +02:00
delayMicroseconds(length * _microsPerByte);
2022-05-25 12:32:10 +02:00
setRXmode(); // receiver mode
2022-05-24 17:24:57 +02:00
return n;
}
2022-05-26 08:56:53 +02:00
#endif
2022-05-24 17:24:57 +02:00
///////////////////////////////////////////////////////
//
2023-05-11 11:14:17 +02:00
// EXPERIMENTAL - use at own risk.
2022-05-24 17:24:57 +02:00
//
///////////////////////////////////////////////////////
//
2023-02-05 20:00:17 +01:00
// commando value meaning
2022-05-24 17:24:57 +02:00
//
// SOH 0x01 start of header
// STX 0x02 start of text
// ETX 0x03 end of text
2023-02-05 20:00:17 +01:00
// EOT 0x04 end of transmission
2022-05-24 17:24:57 +02:00
//
2023-02-05 20:00:17 +01:00
// optional
2022-05-24 17:24:57 +02:00
// ACK 0x06 ACKnowledge
// NAK 0x15 Not Acknowledge
// CAN 0x18 CANcel
//
///////////////////////////////////////////////////////
//
2023-02-05 20:00:17 +01:00
// A message has the following layout
2022-05-24 17:24:57 +02:00
//
// SOH start of header
// deviceID to
// deviceID sender
// length length of message
2022-11-23 16:14:02 +01:00
// STX start of text
// message idem
// CHECKSUM idem, message only!
// ETX end of text
// EOT end of transmission
2022-05-24 17:24:57 +02:00
//
2022-11-23 16:14:02 +01:00
void RS485::send(uint8_t receiverID, uint8_t msg[], uint8_t len)
2022-05-24 17:24:57 +02:00
{
uint8_t CHKSUM = 0;
2022-11-23 16:14:02 +01:00
setTXmode(); // transmit mode
2022-05-24 17:24:57 +02:00
_stream->write(SOH);
2022-11-23 16:14:02 +01:00
_stream->write(receiverID); // TO
_stream->write(_deviceID); // FROM
_stream->write(len); // LENGTH BODY
2022-05-24 17:24:57 +02:00
_stream->write(STX);
for (int i = 0; i < len; i++)
{
_stream->write(msg[i]);
2022-11-23 16:14:02 +01:00
CHKSUM ^= msg[i]; // Simple XOR checksum.
2022-05-24 17:24:57 +02:00
}
_stream->write(CHKSUM);
_stream->write(ETX);
2022-11-23 16:14:02 +01:00
_stream->write(EOT);
delayMicroseconds((len + 8 + 2) * _microsPerByte); // + 2 to be sure...
setRXmode();
2022-05-24 17:24:57 +02:00
}
2022-11-23 16:14:02 +01:00
2022-05-24 17:24:57 +02:00
// returns true if complete message is captured.
// multiple calls needed as it should be non-blocking.
2022-11-23 16:14:02 +01:00
bool RS485::receive(uint8_t &senderID, uint8_t msg[], uint8_t &msglen)
2022-05-24 17:24:57 +02:00
{
2022-11-23 16:14:02 +01:00
static uint8_t state = 0;
2022-05-24 17:24:57 +02:00
static uint8_t length = 0;
2022-11-23 16:14:02 +01:00
static bool forMe = false;
2023-05-11 11:14:17 +02:00
static uint8_t CHKSUM = 0;
2022-05-24 17:24:57 +02:00
if (_stream->available() == 0) return false;
uint8_t v = _stream->read();
2022-11-23 16:14:02 +01:00
// if (debug) Serial.print(v, HEX);
2023-05-11 11:14:17 +02:00
// Serial.print(state, HEX);
// Serial.print('\t');
// Serial.println(v, HEX);
2022-05-24 17:24:57 +02:00
2022-11-23 16:14:02 +01:00
switch(state)
2022-05-24 17:24:57 +02:00
{
2022-11-23 16:14:02 +01:00
// waiting for new packet
case 0:
if (v == SOH)
{
2023-02-05 20:00:17 +01:00
_bidx = 0; // start new packet
2023-05-11 11:14:17 +02:00
CHKSUM = 0;
2022-11-23 16:14:02 +01:00
state = 1;
}
break;
// extract receiver
case 1:
forMe = ((v == _deviceID) || (v == 255)); // PM or broadcast?
if (not forMe) state = 99;
else state = 2;
break;
// extract sender
case 2:
2023-05-11 11:14:17 +02:00
senderID = v;
2022-11-23 16:14:02 +01:00
state = 3;
break;
// extract length
case 3:
msglen = v;
2023-05-11 11:14:17 +02:00
length = v;
state = 4;
2022-11-23 16:14:02 +01:00
break;
// expect STX
case 4:
if (v == STX) state = 5;
else state = 99;
break;
// extract message
case 5:
if (length == 0)
{
2023-05-11 11:14:17 +02:00
// expect checksum
if (CHKSUM == v) state = 6;
else
{
// debug failing checksum
// Serial.print(CHKSUM, HEX);
// Serial.print('\t');
// Serial.println(v, HEX);
state = 99; // for debug change to state = 6;
}
2022-11-23 16:14:02 +01:00
break;
}
2023-02-05 20:00:17 +01:00
// error handling if v not ASCII ?
2022-11-23 16:14:02 +01:00
_buffer[_bidx++] = v;
CHKSUM ^= v;
length--;
break;
2023-05-11 11:14:17 +02:00
// expect ETX
2022-11-23 16:14:02 +01:00
case 6:
2023-05-11 11:14:17 +02:00
if (v == ETX) state = 7;
2022-11-23 16:14:02 +01:00
else state = 99;
break;
// expect EOT
2023-05-11 11:14:17 +02:00
case 7:
2022-11-23 16:14:02 +01:00
if (v == EOT)
{
2023-05-11 11:14:17 +02:00
msglen = _bidx;
2022-11-23 16:14:02 +01:00
for (int i = 0; i < msglen; i++)
{
msg[i] = _buffer[i];
}
2023-05-11 11:14:17 +02:00
state = 0;
2022-11-23 16:14:02 +01:00
return true;
}
2023-05-11 11:14:17 +02:00
state = 99;
2022-11-23 16:14:02 +01:00
break;
// SKIP until next packet
case 99: // wait for EOT in case of error
if (v == EOT) state = 0;
break;
2022-05-24 17:24:57 +02:00
}
2022-11-23 16:14:02 +01:00
return false;
2022-05-24 17:24:57 +02:00
}
2022-11-23 16:14:02 +01:00
2022-05-24 17:24:57 +02:00
2023-05-11 11:14:17 +02:00
2022-05-24 17:24:57 +02:00
//////////////////////////////////////////////////
//
// PRIVATE
//
2023-02-05 20:00:17 +01:00
// -- END OF FILE --
2022-05-24 17:24:57 +02:00