2022-05-24 11:24:57 -04:00
|
|
|
#pragma once
|
|
|
|
//
|
|
|
|
// FILE: RS485.h
|
|
|
|
// AUTHOR: Rob Tillaart
|
|
|
|
// DATE: 30-okt-2017
|
2024-04-16 15:03:41 -04:00
|
|
|
// VERSION: 0.5.1
|
2022-05-24 11:24:57 -04:00
|
|
|
// PURPOSE: Arduino library for RS485 modules
|
|
|
|
// URL: https://github.com/RobTillaart/RS485
|
|
|
|
|
|
|
|
|
|
|
|
#include "Arduino.h"
|
|
|
|
#include "ASCII_CONTROL.h"
|
|
|
|
|
2024-04-16 15:03:41 -04:00
|
|
|
#define RS485_LIB_VERSION (F("0.5.1"))
|
2022-05-24 11:24:57 -04:00
|
|
|
|
|
|
|
|
|
|
|
class RS485 : public Stream
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
RS485(Stream * stream, uint8_t sendPin, uint8_t deviceID = 0);
|
|
|
|
|
2023-05-11 05:14:17 -04:00
|
|
|
uint8_t getDeviceID();
|
2022-05-24 11:24:57 -04:00
|
|
|
|
|
|
|
|
2022-05-26 02:56:53 -04:00
|
|
|
// Stream interface
|
|
|
|
int available();
|
|
|
|
int read();
|
|
|
|
int peek();
|
|
|
|
void flush();
|
2022-05-24 11:24:57 -04:00
|
|
|
|
2023-05-11 05:14:17 -04:00
|
|
|
|
2022-05-26 02:56:53 -04:00
|
|
|
// Write
|
|
|
|
size_t write(uint8_t c);
|
|
|
|
size_t write(char * array, uint8_t length); // wrapper
|
|
|
|
size_t write(uint8_t * array, uint8_t length);
|
2022-05-25 06:32:10 -04:00
|
|
|
|
2023-05-11 05:14:17 -04:00
|
|
|
|
2022-05-26 02:56:53 -04:00
|
|
|
// Mode functions
|
2022-05-25 06:32:10 -04:00
|
|
|
inline void setTXmode() { digitalWrite(_sendPin, HIGH); };
|
|
|
|
inline void setRXmode() { digitalWrite(_sendPin, LOW); };
|
|
|
|
uint8_t getMode() { return digitalRead(_sendPin) == HIGH; };
|
2022-05-24 11:24:57 -04:00
|
|
|
|
|
|
|
|
2023-12-18 05:35:35 -05:00
|
|
|
// EXPERIMENTAL
|
|
|
|
// - in a derived class?
|
|
|
|
// - use at own risk
|
2022-11-23 10:14:02 -05:00
|
|
|
// send ASCII encoded messages from one master to multiple clients.
|
|
|
|
// msg[] = 32..127
|
2023-12-18 05:35:35 -05:00
|
|
|
// len = 1.. 48 (internal receive buffer is 50)
|
2024-04-16 15:03:41 -04:00
|
|
|
size_t send(uint8_t receiverID, uint8_t msg[], uint8_t len);
|
|
|
|
bool receive(uint8_t &senderID, uint8_t msg[], uint8_t &len);
|
2022-11-23 10:14:02 -05:00
|
|
|
|
2023-12-18 05:35:35 -05:00
|
|
|
// EXPERIMENTAL
|
2024-04-16 15:03:41 -04:00
|
|
|
size_t send(uint8_t receiverID, char msg[], uint8_t len);
|
|
|
|
bool receive(uint8_t &senderID, char msg[], uint8_t &len);
|
|
|
|
|
|
|
|
// EXPERIMENTAL
|
|
|
|
void setMicrosPerByte(uint16_t mpb) { _microsPerByte = mpb; };
|
|
|
|
uint16_t getMicrosPerByte() { return _microsPerByte; };
|
2023-12-18 05:35:35 -05:00
|
|
|
|
2022-05-24 11:24:57 -04:00
|
|
|
|
|
|
|
private:
|
|
|
|
Stream * _stream;
|
|
|
|
uint8_t _sendPin = 0;
|
|
|
|
uint8_t _deviceID = 0;
|
2024-04-16 15:03:41 -04:00
|
|
|
uint16_t _microsPerByte = 1100;
|
2022-05-24 11:24:57 -04:00
|
|
|
|
2023-12-18 05:35:35 -05:00
|
|
|
// EXPERIMENTAL
|
2022-11-23 10:14:02 -05:00
|
|
|
uint8_t _bidx = 0;
|
2023-05-11 05:14:17 -04:00
|
|
|
uint8_t _buffer[50]; // internal receive buffer
|
2022-05-24 11:24:57 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2023-02-05 14:00:17 -05:00
|
|
|
// -- END OF FILE --
|
2022-05-24 11:24:57 -04:00
|
|
|
|