hello people
Nice to meet you I would like said to hope to do well and please be patient whit me, I'm new in Arduino world,
I work with my Arduino one, in this project:
1º I'm reading values from analog inputs, from pressure gauges. (works ok)
2º this values are converted to float reg. (works ok)
3º need send to pc over USB but thought Modbus RTU protocol, it's because going to encapsulated software.
with the Modbus library works when using only coils register, but my problems its because I need send to holding registers, and for that I need write two values with 16bits from ieee 754 simple precision.
for example, register 55 (03 holding registers)
au16Data[54] = word2; //low word var int from iee754,
au16Data[55] = word1; //high word
when my value from sensor it's = 12.4
this values in binary is: 01000001 01000110 01100110 01100110
and this two word must be
word1 = 16710;
word2 = 26214;
those dates from excel attached on the bottom, try to write all code but doesn't work because of discrepacy between excel and Arduino code.
in resume, i need calculate for float to word1 and word2
this is part to mi code:
#include <ModbusRtu.h>
/**
- Modbus object declaration
- u8id : node id = 0 for master, = 1..247 for slave
- u8serno : serial port (use 0 for Serial)
- u8txenpin : 0 for RS-232 and USB-FTDI
- or any pin number > 1 for RS-485
- Version 05/10/2017 probando funcion para calcular iee754
*/
#define ID 9
Modbus slave(ID, 0, 0);
boolean led;
int8_t state = 0;
unsigned long tempus;
// data array for modbus network sharing
uint16_t au16Data[32]={3}; // first value must be set=3 for holding registers
//variables para conversion
int signo=0; //primer bit
int signoM=-1;
float reg=0;
int exponente=0;
int expoSuma=0; //para los siguientes bits
float num1Abs; //uso valor absoluto
float byte1,byte2,byte3, byte4, aux0,aux1,aux3,aux4,aux6;
int word1,word2, aux2,aux5;
float mantissa;
void setup() {
// define i/o
// start communication
slave.begin( 19200 );
tempus = millis() + 100;
digitalWrite(13, HIGH );
}
void calcular(float num1){
num1Abs=abs(num1);
//Convert Float to two words IEEE 754
if (num1>=0)
{
signo=0;
signoM=1;
}else{
signo=1;
signoM=-1;
}
exponente=round(log(num1Abs));
mantissa = (num1*signoM)/pow(2,exponente);
byte1 = (exponente+127)>>1; //para este caso convierte 130 en 65
aux0 = round((mantissa -1)*8388608);
aux1 = aux0/256;
aux2 = aux1;
aux3 = (aux1 - aux2)*256;
aux4 = aux2 /256;
aux5 = aux4;
aux6 = (aux4 - aux5)*256;
byte2 = aux1/256;
byte3 = aux3;
byte4 = aux6;
word1 = word(byte1,byte2);
word2 = word (byte3,byte4);
//End Convert
}
void loop() {
// poll messages
// blink led pin on each valid message
state = slave.poll( au16Data, 64 ); //( , +-cantidad de registros)
if (state > 4) {
tempus = millis() + 50;
digitalWrite(13, HIGH);
}
if (millis() > tempus) digitalWrite(13, LOW );
// get data
reg=reg+1;
calcular(reg);
//grabo los valores calculados
au16Data[54] = word2; //low word var int from iee754,
au16Data[55] = word1; //high word
Serial.println(reg);
Serial.println(word1);
Serial.println(word2);
// diagnose communication
au16Data[6] = slave.getInCnt();
au16Data[7] = slave.getOutCnt();
au16Data[8] = slave.getErrCnt();
//for check number from 0 to 100
if(int(reg)==100){
reg=0;
}
}