KEMBAR78
DyNet Arduino RS485 Library | PDF | Computer Programming | Computer Architecture
0% found this document useful (0 votes)
411 views5 pages

DyNet Arduino RS485 Library

This document contains code for a DyNet library that allows an Arduino to communicate with a Dynalite RS485 bus. The library uses a MAX485 IC and SoftwareSerial to send and receive 8-byte opcode messages. It handles half-duplex communication using the RE and DE pins and calculates checksums for sent and received opcodes. The library provides functions for initializing communication, sending opcodes, and receiving opcodes and calling a callback function.

Uploaded by

Jay Jes Jessie
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
411 views5 pages

DyNet Arduino RS485 Library

This document contains code for a DyNet library that allows an Arduino to communicate with a Dynalite RS485 bus. The library uses a MAX485 IC and SoftwareSerial to send and receive 8-byte opcode messages. It handles half-duplex communication using the RE and DE pins and calculates checksums for sent and received opcodes. The library provides functions for initializing communication, sending opcodes, and receiving opcodes and calling a callback function.

Uploaded by

Jay Jes Jessie
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

58

DyNet.cpp
@@ -0,0 +1,58 @@
+#include <Arduino.h>
+#include "DyNet.h"
+
+DyNet::DyNet() {
+

_rs485 = NULL;

+}
+
+DyNet::~DyNet () {
+

if (_rs485 != NULL) delete _rs485;

+}
+
+void DyNet::begin (int roPin, int rePin, int dePin, int diPin) {
+

_rePin = rePin;

_dePin = dePin;

pinMode (roPin, INPUT);

pinMode (rePin, OUTPUT);

pinMode (dePin, OUTPUT);

pinMode (diPin, OUTPUT);

_rs485 = new SoftwareSerial (roPin, diPin);

_rs485->begin (9600);

rxMode ();

+}
+
+void DyNet::rxMode () {
+

digitalWrite (_dePin, LOW);

digitalWrite (_rePin, LOW);

_rs485->listen();

+}
+
+void DyNet::txMode () {
+

digitalWrite (_rePin, HIGH);

digitalWrite (_dePin, HIGH);

+}
+
+void DyNet::receiveOpCode (void (*fn)(byte*)) {
+

delay (10);

byte opCode[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };

for (int i = 0; i < 8; i++) {

if (_rs485->available ())

opCode[i] = _rs485->read ();

else

return;

delay (1);

if (checksum (opCode)) fn (opCode);

+}
+
+void DyNet::sendOpCode (byte *opCode) {
+

delay (10);

setchecksum (opCode);

txMode ();

_rs485->write (opCode, 8);

rxMode ();

+}
+
+byte DyNet::checksum (byte *opCode) { return (~(opCode[0] + opCode[1] + opCode[2]
+ opCode[3] + opCode[4] + opCode[5] + opCode[6]) + 1) & 0xFF; }
+void DyNet::setchecksum (byte *opCode) { opCode[7] = checksum (opCode); }
+bool DyNet::checkchecksum (byte *opCode) { return opCode[7] == checksum (opCode); }

DyNet.h
@@ -0,0 +1,27 @@
+#ifndef DYNET_H
+#define DYNET_H
+
+#include <Arduino.h>
+#include <SoftwareSerial.h>
+
+class DyNet {
+
+

public:

DyNet ();

~DyNet ();

int _rePin, _dePin;

void begin (int roPin, int rePin, int dePin, int diPin);

void receiveOpCode (void (*)(byte*));

void sendOpCode (byte*);

+
+

private:

SoftwareSerial* _rs485;

byte checksum (byte*);

void setchecksum (byte*);

bool checkchecksum (byte*);

void rxMode ();

void txMode ();

+
+};
+
+#endif

README
@@ -0,0 +1,39 @@
+Philips Dynalite DyNet I/O library
+---------------------------------+
+This library will use a MAX485 IC together with the SoftwareSerial library
+to send and receive Dynalite OpCodes directly onto the DyNet RS485 bus.
+
+The library requires an Aurdino with with a correctly wired MAX485 IC,
+using all 4 connections (RO, RE, DE, DI).

The DyNet bus uses 9600 baud,

+8 data bits, 1 start bit, 1 stop bit and no parity and it is strictly
+half duplex.

The functions in this library ensure half duplex by using

+the RE/DE pins.


+
+This libarary will also calculate the 8th checksum byte of OpCodes to be
+sent.

Any value in the opcode at this position is ignored and overwritten.

+Similarly, incoming data will be checked and not handed back to the calling
+function if the checksum value is wrong.
+
+
+Initialisation
+-------------+To use the library, simply create an instance of the DyNet object, and then
+call the begin function indicating which pins are allocated to each of the
+relevant pins on the MAX485.
+
+This library is built to be small and fast, and not double and triple check
+that everything has been set up correctly.

If you do NOT call begin, there's

+nothing stopping you from calling the other functions anyway and all kinds
+of unexpected things may happen.

It's a microcontroller.

+
+Functions
+--------+sendOpCode: Expects an 8 byte array that is the OpCode.

It is simply fired

+onto the serial bus after calculating the OpCode checksum.


+
+receiveOpCode: This will check the wire for awaiting incoming data that might
+be waiting in the buffer.

It'll fetch a complete OpCode provided all bytes

+are no more than 1ms apart (as per DyNet spec) and if the checksum works out,
+it'll call the supplied function with the opcode.
+function from the DDMC810 object.

For me this is the process

You might also like