With Nicks Gammons RS485 Master code listed below (all be it played with by me)
Gammon Forum : Electronics : Microprocessors : RS485 communications (from Nicks Site)
I want to have the slave send a response that contains data from a variable that when received by the Master can be used in my application,
the slave sends a response to a masters message but it only has a boolean response - successful transmission true/false, switching on pin 13 if false.
How can i get a decimal value passed back
My network works great for sending digital commands and i can change the channel and communicate with other slaves indepentantly.
i now want to iterogate a slave that will respond with value read from an analoge input.
#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 = 0;
int data=0;
const byte LED_PIN = 13;
int incomingByte=0; // for incoming serial data
int thousands=0; int hundreds =0; int tens =0; int ones=0;
int inByte=0;
// callback routines
void fWrite (const byte what)
{
rs485.print (what);
}
int fAvailable ()
{
return rs485.available ();
}
int fRead ()
{
return rs485.read ();
}
void setup()
{
rs485.begin (28800);
Serial.begin(9600);
pinMode (ENABLE_PIN, OUTPUT); // driver output enable
pinMode (LED_PIN, OUTPUT); // built-in LED
} // end of setup
byte old_level = 0;
void loop()
{
// read potentiometer
// byte level = analogRead (0) / 4;// commented out as it is now variable being written
byte level = (incomingByte);
// Serial.print(level);// print to serial monitor - for testing only
if (Serial.available() > 0) { //read value from host PC
hostprotocol();
}
// no change? forget it
if (level == old_level)
return;
// assemble message
byte msg [] = {
1, // device 1
2, // turn light on
level // to what level
};
// send to slave
digitalWrite (ENABLE_PIN, HIGH); // enable sending
sendMsg (fWrite, msg, sizeof msg);
delayMicroseconds (660);
digitalWrite (ENABLE_PIN, LOW); // disable sending
The following code is where i beleive the response would arrive.
i have tried adding lines of code to read a particular buffer location ie Rxdata = (buf [1]);
and then do a println to see content but nothing happens.
any suggestions would be greatly appreciated :-
// receive response
byte buf [20];
byte received = recvMsg (fAvailable, fRead, buf, sizeof buf);
digitalWrite (LED_PIN, received == 0); // turn on LED if error
// only send once per successful change
if (received)
{
if (buf [0] != channel)
return; // not my device
if (buf [1] != 2)
return; // unknown command
byte msg [] = {
0, // device 0 (master)
data, // turn light on command received
//data
};
old_level = level;
}
} // end of loop
rest of master code
void hostprotocol() //Serial commands received from host
{
inByte = Serial.read();
switch (inByte)
{
case 79: //O out
DigitalOut();
break;
}
}
void DigitalOut() {
// send data only when you receive data:
Serial.print("Digital OK");
// only if there are bytes in the serial buffer execute the following code
if(Serial.available()) {
//keep reading and printing from serial untill there are bytes in the serial buffer
//while (Serial.available()>0){
if (Serial.available()>2){ // got 3 characters
// assume comes in as hundreds, tens, ones
hundreds = Serial.read() - 0x30; // read the byte & convert from ASCII to a number
tens = Serial.read() - 0x30;
ones = Serial.read() - 0x30;
// now make into a digit
incomingByte = hundreds*100 + tens*10 + ones;
}
Serial.print(incomingByte);
}
//int value = (incomingByte);
}