GY-63_MS5611/libraries/RS485/RS485.cpp

277 lines
5.3 KiB
C++
Raw Normal View History

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