Hello.
I'm use Non-blocking code from Nick Gammon (Gammon Forum : Electronics : Microprocessors : RS485 communications)
Master:
#include <RS485_non_blocking.h>
#include <SoftwareSerial.h>
#define RS485_RX_PIN 6
#define RS485_TX_PIN 5
SoftwareSerial rs485(RS485_RX_PIN, RS485_TX_PIN); // receive pin, transmit pin
struct // packet data structure
{
byte thisDeviceID;
byte destinationID;
int value;
String msg;
} data;
size_t fWrite(const byte what)
{
return rs485.write(what);
}
RS485 network(NULL, NULL, fWrite, 0);
void setup()
{
Serial.begin(9600);
rs485.begin(9600); // software serial for talking to other devices
network.begin(); // initialize the RS485 library
} // end of setup
void loop()
{
//const byte msg[] = "Hello world"; // works fine, but why "byte"?
//network.sendMsg(msg, sizeof (msg));
memset(&data, 0, sizeof(data));
data.thisDeviceID = 3;
data.destinationID = 0;
data.value = 8;
data.msg = "Test point";
network.sendMsg((byte *) &data, sizeof data);
delay (1000);
} // end of loop
Slave:
#include <RS485_non_blocking.h>
#include <SoftwareSerial.h>
#define RS485_RX_PIN 6
#define RS485_TX_PIN 5
SoftwareSerial rs485(RS485_RX_PIN, RS485_TX_PIN); // receive pin, transmit pin
struct // packet data sctructure
{
byte thisDeviceID;
byte destinationID;
int value;
String msg;
} data;
int fAvailable()
{
return rs485.available();
}
int fRead()
{
return rs485.read();
}
RS485 network(fRead, fAvailable, NULL, 20);
void setup()
{
Serial.begin(9600);
rs485.begin(9600); // software serial for talking to other devices
network.begin(); // initialize the RS485 library
} // end of setup
void loop()
{
if (network.update())
{
// Serial.write(network.getData(), network.getLength()); works fine with byte msg[] = "Hello world"
// Serial.println();
memset(&data, 0, sizeof (data));
int len = network.getLength();
if (len > sizeof (data)) len = sizeof (data);
memcpy(&data, network.getData(), len);
Serial.println(data.thisDeviceID);
Serial.println(data.destinationID);
Serial.println(data.value);
Serial.println(data.msg);
Serial.println();
}
} // end of loop
Result:
3
0
8
<< here empty line instead text
Where is mistake?