I have simple code. The library downloaded from github:smarmengol/Modbus-Master-Slave-for-Arduino.
I wanted to store floating point. In the code blow, I type casted to integer value. As I know uint16_t au16data[30];
is integer
#include <avr/wdt.h>
#include"glob.h"
#define ID 1
int8_t state = 0;
Modbus slave(ID, 0, 0);
unsigned long tempus;
// data array for modbus network sharing
uint16_t au16data[30];
void Take_Reading()
{
for(row=0;row<9;row++)
{
int k=(8*array[row][0]+4*array[row][1]+2*array[row][2]+1*array[row][3]);
digitalWrite(Enablepin,array[row][0]);
digitalWrite(SO_enable,array[row][1]);
digitalWrite(S1_enable,array[row][2]);
digitalWrite(S2_enable,array[row][3]);
//delay(100);
Sensor_Value0=analogRead(A0);
Sensor_Value1=analogRead(A5);
Mux1_array[row]=(Sensor_Value0 * ARDUINO_ANALOG_SCALING);
Mux2_array[row]=(Sensor_Value1 * ARDUINO_ANALOG_SCALING);
Current_Value1[row]= (Mux1_array[row]*40)-98;
Current_Value2[row]= (Mux2_array[row]*40)-98;
}
}
void setup()
{
wdt_enable(WDTO_8S);
slave.begin( 9600 );
tempus = millis() + 100;
pinMode(3,OUTPUT);
pinMode(SO_enable, OUTPUT) ;// pin can enable/disable using digital IO 7 of arduino
pinMode(S1_enable, OUTPUT) ;// pin can enable/disable using digital IO 6 of arduino
pinMode(S2_enable, OUTPUT) ;// pin can enable/disable using digital IO 5 of arduino
pinMode(Enablepin, OUTPUT) ;// pin can enable/disable using digital IO 4 of arduino
pinMode(A0, INPUT) ;
pinMode(A5, INPUT) ;
// Serial.begin(9600);
}
void loop()
{
digitalWrite(3,HIGH);
// wdt_reset();
Take_Reading();
state = slave.poll( au16data,30 );
if (state > 4) {
tempus = millis() + 50;
digitalWrite(13, HIGH);
}
if (millis() > tempus) digitalWrite(13, LOW );
Modbus_call();
}
void Modbus_call()
{
/*reading of coil where i can succefully read value*/
au16data[0]=(int)Current_Value1[0];
au16data[1]=(int)Current_Value1[1];
au16data[2]=(int)Current_Value1[2];
au16data[3]=(int)Current_Value1[3];
au16data[4]=(int)Current_Value1[4];
au16data[5]=(int)Current_Value1[5];
au16data[6]=(int)Current_Value1[6];
au16data[7]=(int)Current_Value1[7];
au16data[8]=(int)Current_Value1[8];
au16data[9]=(int)Current_Value2[0];
au16data[10]=(int)Current_Value2[1];
au16data[11]=(int)Current_Value2[2];
au16data[12]=(int)Current_Value2[3];
au16data[16]=(int)Current_Value2[4];
au16data[17]=(int)Current_Value2[5];
au16data[18]=(int)Current_Value2[6];
au16data[19]=(int)Current_Value2[7];
uint16_t calcCRC( au16data[15]);
au16data[13] = slave.getInCnt();
au16data[14] = slave.getOutCnt();
au16data[15] = slave.getErrCnt();
}
Conversion method as below.
CS=30.10
Int_CS=int(CS)
FLOAT_CS=(Int_CS-CS)*100
How to store the Int_CS
& FLOAT_CS
values in au16data[0]
??
I have change code accordingly.
But I get zero. After some time its values don't even show
#include <avr/wdt.h>
#include"glob.h"
#define ID 1
int8_t state = 0;
Modbus slave(ID, 0, 0);
unsigned long tempus;
// data array for modbus network sharing
uint16_t au16data[5];
float auFloatData[30];
void Take_Reading()
{
for(row=0;row<9;row++)
{
int k=(8*array[row][0]+4*array[row][1]+2*array[row][2]+1*array[row][3]);
digitalWrite(Enablepin,array[row][0]);
digitalWrite(SO_enable,array[row][1]);
digitalWrite(S1_enable,array[row][2]);
digitalWrite(S2_enable,array[row][3]);
//delay(100);
Sensor_Value0=analogRead(A0);
Sensor_Value1=analogRead(A5);
Mux1_array[row]=(Sensor_Value0 * ARDUINO_ANALOG_SCALING);
Mux2_array[row]=(Sensor_Value1 * ARDUINO_ANALOG_SCALING);
Current_Value1[row]= (Mux1_array[row]*40)-98;
Current_Value2[row]= (Mux2_array[row]*40)-98;
}
}
void setup()
{
wdt_enable(WDTO_8S);
slave.begin( 9600 );
tempus = millis() + 100;
pinMode(3,OUTPUT);
pinMode(SO_enable, OUTPUT) ;// pin can enable/disable using digital IO 7 of arduino
pinMode(S1_enable, OUTPUT) ;// pin can enable/disable using digital IO 6 of arduino
pinMode(S2_enable, OUTPUT) ;// pin can enable/disable using digital IO 5 of arduino
pinMode(Enablepin, OUTPUT) ;// pin can enable/disable using digital IO 4 of arduino
pinMode(A0, INPUT) ;
pinMode(A5, INPUT) ;
// Serial.begin(9600);
}
void loop()
{
digitalWrite(3,HIGH);
// wdt_reset();
Take_Reading();
state = slave.poll( au16data,5 );
if (state > 4) {
tempus = millis() + 50;
digitalWrite(13, HIGH);
}
if (millis() > tempus) digitalWrite(13, LOW );
Modbus_call();
}
void Modbus_call()
{
/*reading of coil where i can succefully read value*/
auFloatData[20]=Current_Value1[0];
auFloatData[21]=Current_Value1[1];
auFloatData[22]=Current_Value1[3];
auFloatData[4]=Current_Value1[4];
auFloatData[5]=Current_Value1[5];
auFloatData[6]=Current_Value1[6];
auFloatData[7]=Current_Value1[7];
auFloatData[8]=Current_Value1[8];
auFloatData[9]=Current_Value2[0];
auFloatData[10]=Current_Value2[1];
auFloatData[11]=Current_Value2[2];
auFloatData[12]=Current_Value2[3];
auFloatData[16]=Current_Value2[4];
auFloatData[17]=Current_Value2[5];
auFloatData[18]=Current_Value2[6];
auFloatData[19]=Current_Value2[7];
uint16_t calcCRC( au16data[3]);
au16data[1] = slave.getInCnt();
au16data[2] = slave.getOutCnt();
au16data[3] = slave.getErrCnt();
}
uint16_t x =CS*100
it will store 3010. When extracting from uint to float float y = (float)x/100.