76 lines
1.7 KiB
C
Raw Normal View History

2022-05-24 17:24:57 +02:00
#pragma once
//
// FILE: RS485.h
// 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
// URL: https://github.com/RobTillaart/RS485
#include "Arduino.h"
#include "ASCII_CONTROL.h"
2023-05-11 11:14:17 +02:00
#define RS485_LIB_VERSION (F("0.2.5"))
2022-05-24 17:24:57 +02:00
class RS485 : public Stream
{
public:
RS485(Stream * stream, uint8_t sendPin, uint8_t deviceID = 0);
2023-05-11 11:14:17 +02:00
// sets the baud rate of the stream.
// 0.3.0
// void begin(uint32_t baudRate);
2022-05-24 17:24:57 +02:00
// baudRate must match Serial baudRate
// micros are needed before RS485 stream may be reset
// to (default) receiving mode.
void setMicrosPerByte(uint32_t baudRate);
2023-05-11 11:14:17 +02:00
uint32_t getMicrosPerByte();
uint8_t getDeviceID();
2022-05-24 17:24:57 +02:00
2022-05-26 08:56:53 +02:00
// Stream interface
int available();
int read();
int peek();
void flush();
2022-05-24 17:24:57 +02:00
2023-05-11 11:14:17 +02:00
2022-05-26 08:56:53 +02: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 12:32:10 +02:00
2023-05-11 11:14:17 +02:00
2022-05-26 08:56:53 +02:00
// Mode functions
2022-05-25 12:32:10 +02:00
inline void setTXmode() { digitalWrite(_sendPin, HIGH); };
inline void setRXmode() { digitalWrite(_sendPin, LOW); };
uint8_t getMode() { return digitalRead(_sendPin) == HIGH; };
2022-05-24 17:24:57 +02:00
2023-05-11 11:14:17 +02:00
// EXPERIMENTAL - use at own risk.- (for 0.3.0)
2022-11-23 16:14:02 +01:00
// send ASCII encoded messages from one master to multiple clients.
// msg[] = 32..127
2023-05-11 11:14:17 +02:00
// len = 1..48 ?
2022-11-23 16:14:02 +01:00
void send(uint8_t receiverID, uint8_t msg[], uint8_t len);
bool receive(uint8_t &senderID, uint8_t msg[], uint8_t &len);
2022-05-24 17:24:57 +02:00
private:
Stream * _stream;
uint8_t _sendPin = 0;
uint8_t _deviceID = 0;
uint16_t _microsPerByte = 1000;
2023-05-11 11:14:17 +02:00
// EXPERIMENTAL (for 0.3.0)
2022-11-23 16:14:02 +01:00
uint8_t _bidx = 0;
2023-05-11 11:14:17 +02:00
uint8_t _buffer[50]; // internal receive buffer
2022-05-24 17:24:57 +02:00
};
2023-02-05 20:00:17 +01:00
// -- END OF FILE --
2022-05-24 17:24:57 +02:00