1

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.

Output file

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(); 

 }
1
  • 1
    if you need 2 numbers after comma then you can uint16_t x =CS*100 it will store 3010. When extracting from uint to float float y = (float)x/100.
    – Martynas
    Commented Oct 31, 2014 at 6:35

1 Answer 1

1
  1. Sizes

Integer Types (int, long and long long)

Size of Boolean type is 1 byte(s)

Number of bits in a character: 8 Size of character types is 1 byte Signed char min: -128 max: 127 Unsigned char min: 0 max: 255 Default char is unsigned

Size of short int types is 2 bytes Signed short min: -32768 max: 32767 Unsigned short min: 0 max: 65535

Size of int types is 4 bytes Signed int min: -2147483648 max: 2147483647 Unsigned int min: 0 max: 4294967295

Size of long int types is 4 bytes Signed long min: -2147483648 max: 2147483647 Unsigned long min: 0 max: 4294967295

Size of long long types is 8 bytes Signed long long min: -9223372036854775808 max: 9223372036854775807 Unsigned long long min: 0 max: 18446744073709551615 Note that int and long are the same size and if you want a 64 bit integer then you need to use long long (or unsigned long long).

  1. What do you want to achieve with

.

CS=30.10 // 30.10
Int_CS=int(CS) // 30
FLOAT_CS = (Int_CS-CS)*100 //-10.00 because 30 - 30.10 = -0.1 and -0.1 * 100 = -10.00

CS is already float, why you want to convert it to int and back to float? If value of int is higher than 255 you have to save it in 2 bytes(highByte and lowByte), float is 32bit(4bytes) value.

I think you have to do like this:

float CS = 30.10;
au16data[0]= (int)(CS*100);//3010.00 > 3010

And for better way use :

float auFloatData[30]; // 
...
auFloatData[0]=Current_Value1[0];
7
  • Minor correction: Default char is signed in gcc (compiler shipped with arduino). You can pass -funsigned-char compiler flag to have unsigned as default.
    – user2973
    Commented Oct 31, 2014 at 9:20
  • @Martynas But i have already tried that.when then it zero at end it print only 301 not 3010. SO it will difficult to recognize .when it turns out 3 digit value.
    – AMPS
    Commented Oct 31, 2014 at 10:10
  • @AMPS What is the range of CS?
    – Martynas
    Commented Oct 31, 2014 at 10:23
  • it measure 0~25 A , for understanding i given example above.
    – AMPS
    Commented Oct 31, 2014 at 10:31
  • What accuracy do you need?
    – Martynas
    Commented Oct 31, 2014 at 10:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.