2nd part
The modified RS485 slave code is as follows
#include "WConstants.h"
#include <NewSoftSerial.h>
#include "RS485_protocol.h"
NewSoftSerial rs485 (2, 3); // receive pin, transmit pin
const byte ENABLE_PIN = 4;
int channel = 1;// Device ID
int Inputvalue =0; // Input value from Master
int ledPin = 12;// digital bit weighted outputs
int ledPin1 = 11;
int ledPin2 = 10;
int ledPin3 = 9;
int ledPin4 = 8;
int ledPin5 = 7;
int ledPin6 = 6;
int ledPin7 = 5;
void fWrite (const byte what)
{
rs485.print (what);
}
int fAvailable ()
{
return rs485.available ();
}
int fRead ()
{
return rs485.read ();
}
void setup()
{
rs485.begin (28800);
pinMode (ENABLE_PIN, OUTPUT); // driver output enable
//Bit weighted gigital outputs
pinMode(ledPin, OUTPUT); // initialize the LED pin as an output:
pinMode(ledPin1, OUTPUT); // initialize the LED pin as an output:
pinMode(ledPin2, OUTPUT); // initialize the LED pin as an output:
pinMode(ledPin3, OUTPUT); // initialize the LED pin as an output:
pinMode(ledPin4, OUTPUT); // initialize the LED pin as an output:
pinMode(ledPin5, OUTPUT); // initialize the LED pin as an output:
pinMode(ledPin6, OUTPUT); // initialize the LED pin as an output:
pinMode(ledPin7, OUTPUT); // initialize the LED pin as an output:
}
void loop()
{
byte buf [20];
byte received = recvMsg (fAvailable, fRead, buf, sizeof (buf) - 1);
if (received)
{
if (buf [0] != channel)
return; // not my device
if (buf [1] != 2)
return; // unknown command
byte msg [] = {
0, // device 0 (master)
3, // turn light on command received
};
delay (5); // give the master a moment to prepare to receive
digitalWrite (ENABLE_PIN, HIGH); // enable sending
sendMsg (fWrite, msg, sizeof msg);
digitalWrite (ENABLE_PIN, LOW); // disable sending
//analogWrite (11, buf [2]); // set light level AS WAS JT
Inputvalue =buf [2];// write register value to a variable JT
// digital bit weighted outputs
digitalWrite(ledPin, (Inputvalue >> 0 )% 2);// LSB
digitalWrite(ledPin1, (Inputvalue >> 1)% 2);
digitalWrite(ledPin2, (Inputvalue >> 2)% 2);
digitalWrite(ledPin3, (Inputvalue >> 3)% 2);
digitalWrite(ledPin4, (Inputvalue >> 4) % 2);
digitalWrite(ledPin5, (Inputvalue >> 5) % 2);
digitalWrite(ledPin6, (Inputvalue >> 6) % 2);
digitalWrite(ledPin7, (Inputvalue >> 7) % 2); // MSB
} // end if something received
} // end of loop
A Picture of the PCB layout for the MAX485 and a drawing showing the various connections will be provided later.
What has been achieved?
You can now drive 8 relays for example which are 1000m away from your PC over 2 wires (twisted pair TWP) a line resistor of 120 ohms will be needed between the A/B wire connections on both ends of the wire link. Look at LVR.com where Jan Alexson explains serial communication and RS485 at great depth- especially calculating the line resistor for a given wire link/distance
(The remote board will obviously require its own power)
Issues
Data type is wrong for data value being transmitted; currently it’s a byte where the input from the host PC is a formatted string/decimal value.
Where the host PC sends O237 the slave works but when the value is less than 100 the slave doesn’t change.
In the slave code the value is parsed to give 100s, 10s and units, it is not needed the value should be able to be used directly. This change has not been implemented yet.