To use the node in your project you should have the antoniorruiz/rs485 library installed. Use the “File → Add Library” menu item in XOD IDE if you don’t have it yet. See Using libraries for more info.
C++ implementation
#include<SoftwareSerial.h>
node {
// Internal state variables defined at this level persists across evaluations
Number foo;
uint8_t bar = 5;
void evaluate(Context ctx) {
bar += 42;
if (isSettingUp()) {
// This run once
foo = (Number)(bar + 1);
}
if (!isInputDirty<input_UPD>(ctx))
return;
auto RS485_serial = getValue<input_IN>(ctx);
int SET;
uint8_t Address = getValue<input_Address>(ctx);
uint8_t Function = getValue<input_Function>(ctx);
uint8_t Start_Address_1 = getValue<input_Start_Address_1>(ctx);
uint8_t Start_Address_2 = getValue<input_Start_Address_2>(ctx);
uint8_t Register_Length_1 = getValue<input_Register_Length_1>(ctx);
uint8_t Register_Length_2 = getValue<input_Register_Length_2>(ctx);
uint8_t CRC_L = getValue<input_CRC_L>(ctx);
uint8_t CRC_H = getValue<input_CRC_H>(ctx);
uint8_t ADDR[] = {Address, Function, Start_Address_1, Start_Address_2, Register_Length_1, Register_Length_2, CRC_L, CRC_H};
uint8_t val[2];
int i;
int val1;
int val2;
int val3;
SET = getValue<input_SET>(ctx);
digitalWrite (SET, 1);
delay (1000);
RS485_serial->write(ADDR, 8);
digitalWrite(SET,0);
delay (100);
if(RS485_serial->available ())
{
for (i=0; i<8; i++){
uint8_t RX1 = RS485_serial->read();
if (i==3){
val[0] = RX1;
}
if (i==4){
val[1] = RX1;
}
}
val1 = val[0];
val2 = val1 % 10;
val1 = val1/10;
val1 = val1*4096;
val2 = val2*256;
val3 = val[1];
val3 = val3 + val2 + val1;
emitValue<output_Measurement>(ctx, val3);
emitValue<output_DONE>(ctx, 1);
}
}
}