Skip to main content

Modbus slave data into word

I have simple code.Library downloaded from here https://github.com/smarmengol/Modbus-Master-Slave-for-Arduino

I wanted to store floating point.In below code 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]??

AMPS
  • 467
  • 11
  • 22