This commit is contained in:
2022-12-15 15:26:57 +01:00
commit 0939c32c58
56 changed files with 10650 additions and 0 deletions

View File

@ -0,0 +1,25 @@
/*
This file is part of the ArduinoRS485 library.
Copyright (c) 2018 Arduino SA. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef _ARDUINO_RS485_H_INCLUDED
#define _ARDUINO_RS485_H_INCLUDED
#include "RS485.h"
#endif

View File

@ -0,0 +1,191 @@
/*
This file is part of the ArduinoRS485 library.
Copyright (c) 2018 Arduino SA. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "RS485.h"
RS485Class::RS485Class(HardwareSerial& hwSerial, int txPin, int dePin, int rePin) :
_serial(&hwSerial),
_txPin(txPin),
_dePin(dePin),
_rePin(rePin),
_transmisionBegun(false)
{
}
void RS485Class::begin(unsigned long baudrate)
{
begin(baudrate, SERIAL_8N1, RS485_DEFAULT_PRE_DELAY, RS485_DEFAULT_POST_DELAY);
}
void RS485Class::begin(unsigned long baudrate, int predelay, int postdelay)
{
begin(baudrate, SERIAL_8N1, predelay, postdelay);
}
void RS485Class::begin(unsigned long baudrate, uint16_t config)
{
begin(baudrate, config, RS485_DEFAULT_PRE_DELAY, RS485_DEFAULT_POST_DELAY);
}
void RS485Class::begin(unsigned long baudrate, uint16_t config, int predelay, int postdelay)
{
_baudrate = baudrate;
_config = config;
// Set only if not already initialized with ::setDelays
_predelay = _predelay == 0 ? predelay : _predelay;
_postdelay = _postdelay == 0 ? postdelay : _postdelay;
if (_dePin > -1) {
pinMode(_dePin, OUTPUT);
digitalWrite(_dePin, LOW);
}
if (_rePin > -1) {
pinMode(_rePin, OUTPUT);
digitalWrite(_rePin, HIGH);
}
_transmisionBegun = false;
_serial->begin(baudrate, config);
}
void RS485Class::end()
{
_serial->end();
if (_rePin > -1) {
digitalWrite(_rePin, LOW);
pinMode(_dePin, INPUT);
}
if (_dePin > -1) {
digitalWrite(_dePin, LOW);
pinMode(_rePin, INPUT);
}
}
int RS485Class::available()
{
return _serial->available();
}
int RS485Class::peek()
{
return _serial->peek();
}
int RS485Class::read(void)
{
return _serial->read();
}
void RS485Class::flush()
{
return _serial->flush();
}
size_t RS485Class::write(uint8_t b)
{
if (!_transmisionBegun) {
setWriteError();
return 0;
}
return _serial->write(b);
}
RS485Class::operator bool()
{
return true;
}
void RS485Class::beginTransmission()
{
if (_dePin > -1) {
digitalWrite(_dePin, HIGH);
if (_predelay) delayMicroseconds(_predelay);
}
_transmisionBegun = true;
}
void RS485Class::endTransmission()
{
_serial->flush();
if (_dePin > -1) {
if (_postdelay) delayMicroseconds(_postdelay);
digitalWrite(_dePin, LOW);
}
_transmisionBegun = false;
}
void RS485Class::receive()
{
if (_rePin > -1) {
digitalWrite(_rePin, LOW);
}
}
void RS485Class::noReceive()
{
if (_rePin > -1) {
digitalWrite(_rePin, HIGH);
}
}
void RS485Class::sendBreak(unsigned int duration)
{
_serial->flush();
_serial->end();
pinMode(_txPin, OUTPUT);
digitalWrite(_txPin, LOW);
delay(duration);
_serial->begin(_baudrate, _config);
}
void RS485Class::sendBreakMicroseconds(unsigned int duration)
{
_serial->flush();
_serial->end();
pinMode(_txPin, OUTPUT);
digitalWrite(_txPin, LOW);
delayMicroseconds(duration);
_serial->begin(_baudrate, _config);
}
void RS485Class::setPins(int txPin, int dePin, int rePin)
{
_txPin = txPin;
_dePin = dePin;
_rePin = rePin;
}
void RS485Class::setDelays(int predelay, int postdelay)
{
_predelay = predelay;
_postdelay = postdelay;
}
/*
RS485Class RS485(SERIAL_PORT_HARDWARE, RS485_DEFAULT_TX_PIN, RS485_DEFAULT_DE_PIN, RS485_DEFAULT_RE_PIN);
*/

View File

@ -0,0 +1,89 @@
/*
This file is part of the ArduinoRS485 library.
Copyright (c) 2018 Arduino SA. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef _RS485_H_INCLUDED
#define _RS485_H_INCLUDED
#include <Arduino.h>
#ifdef PIN_SERIAL1_TX
#define RS485_DEFAULT_TX_PIN PIN_SERIAL1_TX
#else
#define RS485_DEFAULT_TX_PIN 1
#endif
#ifdef __AVR__
#define RS485_DEFAULT_DE_PIN 2
#define RS485_DEFAULT_RE_PIN -1
#else
#define RS485_DEFAULT_DE_PIN A6
#define RS485_DEFAULT_RE_PIN A5
#endif
#define RS485_DEFAULT_PRE_DELAY 50
#define RS485_DEFAULT_POST_DELAY 50
class RS485Class : public Stream {
public:
RS485Class(HardwareSerial& hwSerial, int txPin, int dePin, int rePin);
virtual void begin(unsigned long baudrate);
virtual void begin(unsigned long baudrate, uint16_t config);
virtual void begin(unsigned long baudrate, int predelay, int postdelay);
virtual void begin(unsigned long baudrate, uint16_t config, int predelay, int postdelay);
virtual void end();
virtual int available();
virtual int peek();
virtual int read(void);
virtual void flush();
virtual size_t write(uint8_t b);
using Print::write; // pull in write(str) and write(buf, size) from Print
virtual operator bool();
void beginTransmission();
void endTransmission();
void receive();
void noReceive();
void sendBreak(unsigned int duration);
void sendBreakMicroseconds(unsigned int duration);
void setPins(int txPin, int dePin, int rePin);
void setDelays(int predelay, int postdelay);
private:
HardwareSerial* _serial;
int _txPin;
int _dePin;
int _rePin;
int _predelay = 0;
int _postdelay = 0;
bool _transmisionBegun;
unsigned long _baudrate;
uint16_t _config;
};
extern RS485Class RS485;
#endif