Coder Social home page Coder Social logo

auto485's Introduction

Auto485 - Automatic RS485 Wrapper Class

Auto485 is a small helper library that takes some of the tedium out of RS485 communication. It presents an identical interface to the standard Serial object, so you can .println(...), .available(), .read() and so on, while under the hood, Auto485 will handle toggling between receive and transmit mode on the MAX485 or compatible chip.

If you prefer to do things manually, there is an explicit .set_mode(..) function that lets you toggle between tranmit and receive mode.

Requirements

  • An Arduino -- http://arduino.cc/
  • A RS485 transceiver
  • (optional) An RS485 to USB converter on your PC

Installation

Download the ZIP archive (https://github.com/madleech/Auto485/zipball/master) and extract it to your Arduino folder under libraries/RS485.

Restart the Arduino IDE and you should see in File > Examples > Auto485 entires for 'echo' or so on.

Code Examples

Here is the 'echo' example program, included in the download:

#include <Auto485.h>

#define DE_PIN 2
#define RE_PIN 3

Auto485 bus(DE_PIN, RE_PIN); // new Auto485 wrapper using DE_PIN & RE_PIN to toggle read/write mode on the MAX485

void setup() {
  bus.begin(9600); // open bus at 9600bps
  bus.println("Hello world, now listening"); // at the end of println, we return to listening
}

void loop() {
  if (bus.available() > 0) {
    while (bus.available() > 0)
      bus.write(bus.read()); // mode -> transmit
    bus.println(); // mode -> receive
  }
  delay(1000);
}

We start off by defining the Driver Enable pin (DE_PIN), and Receiver Enable pin (RE_PIN). Usually these pins will be connected together on the MAX485, so you can just use the same pin here.

Next we initialise the bus at 9600bps, just like you would with Serial.begin(9600). Then we print out some text using .println(...). Println handles the bus state for us, changing to TX mode then back to RX mode.

In our main loop we check if any input is available, and if it is, we set the bus to TX mode, write out the data, and return to RX mode.

Documentation

Auto485(int DE_pin) Creates a new Auto485 object. The Driver and Receiver Enable pins on the MAX485 are connected together.

Auto485(int DE_pin, int RE_pin) Creates a new Auto485 object. The Driver and Receiver Enable pins on the MAX485 are connected to separate pins on the Arduino.

Auto485(int DE_pin, int RE_pin, HardwareSerial serial_port) Creates a new Auto485 object using the specified serial port. Usually the defaults are fine (Serial on most boards, Serial1 on the Leonardo and other USBCOM boards). If you are using a Mega with multiple serial ports this lets you choose which serial port to use.

Auto485::TX, Auto485::RX These are constants to let you toggle the mode of the bus.

begin(baud), begin(baud, config) Initiate a serial connection at the given speed, and optionally with the given config settings (e.g. SERIAL_8N2 to use two stop bits, etc).

set_mode(Auto485::TX), set_mode(Auto485::RX) Manually change to transmit or receive mode. When returning to receive mode, the function will pause until all pending serial data has been sent.

write(...), print(...) When in receive mode, the first call to any output functions will change to transmit mode, then send out the data as expected. It handles all the formatting options of the regular Arduino print and write functions.

flush() Finish writing data, then switch to receive mode. Usually the serial writing functions happen asynchronously, with no delay while the data is sent out the serial port. When we're operating in half duplex mode though, we need to wait for the data to finish being sent before we change the mode of the bus. By calling .flush() we ensure there is no unsent data in the buffer. Once all pending data has been sent, we automatically switch back to receive mode!

println(...) Like the write(...) and print(...) functions, calling println(...) will automatically switch to transmit mode. Unlike the lower-level functions though, println will return to receive mode at the end of the line. This means you can easily print simple messages to the bus and everything operates as expected, but if you're sending data byte-by-byte, that functionality if there too and Auto485 won't toggle between RX and TX for every single byte you send.

License

MIT license, see LICENSE file.

auto485's People

Contributors

k4kfh avatar madleech avatar per1234 avatar smz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

auto485's Issues

println() function doesn't return Max485 to listening mode

For me your "echo" example only partially works. I am using a MAX485 chip (not a module, just the DIP IC) wired up with a 120 ohm resistor between A/B. I can get messages from the Arduino on my PC, but I can't get the Arduino to echo back a message sent from my PC.

Edit: After poking around with the oscilloscope, it seems that the println function that says "hello world" at the beginning of the sketch is the culprit. Once that runs, the TX/RX control pin never returns to LOW, so the MAX485 stays in TX mode forever until you call set_mode(Auto485::RX) manually. Adding set_mode(Auto485::RX) after the initial println() call solves everything and the sketch works perfectly.

Compile error for ESP8266

arduino/libraries/madleech-Auto485-3ff0421/Auto485.cpp: In member function 'void Auto485::begin(long unsigned int, uint8_t)':
arduino/libraries/madleech-Auto485-3ff0421/Auto485.cpp:58:28: error: invalid conversion from 'uint8_t {aka unsigned char}' to 'SerialConfig' [-fpermissive]
_serial.begin(baud, config);

                        ^

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.