Pages: [1]   Go Down
Author Topic: SimpleModbusSlave primo tentativo  (Read 502 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 1
Posts: 169
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ciao,
  
Prima prova di Sketch SimpleModbusSlave può funzionare?

Quote
#include <SimpleModbusSlave.h>
#include <OneWire.h>
OneWire  ds(10);  // on pin 10
enum
{      
  TMP1,    
  TOTAL_ERRORS,
  // leave this one
  TOTAL_REGS_SIZE
  
};

unsigned int holdingRegs[TOTAL_REGS_SIZE];

void setup()
{
  modbus_configure(9600, 1, 2, TOTAL_REGS_SIZE);
}

void loop()
{
  float tmp;
  holdingRegs[TOTAL_ERRORS] = modbus_update(holdingRegs);



  holdingRegs[0] = temperatura(tmp);
}


///////////////////////////////////////////////////////////////////////
  //LEGGI Temperatura

float temperatura(float tmp){  
  byte j;
  byte present = 0;
  byte data[12];
  byte addr[8]={16,5,78,183,0,8,0,58};
  float celsius;
ds.reset();
  ds.select(addr);
  ds.write(0x44,1);      
  delay(1000);  

  present = ds.reset();
  ds.select(addr);    
  ds.write(0xBE);      
  for ( j = 0; j< 9; j++) {          
    data [j]  = ds.read();
  }
  

  unsigned int raw = (data[1] <<  8 ) | data[0];
       raw = raw << 3;
 raw = (raw & 0xFFF0) + 12 - data[6];
  celsius = (float)raw / 16.0;
  return celsius;

}

Riesco in questo modo a trasmettere la  temperatura float sul registro 0 avente indirizzo 1?


Grazie

Luca
« Last Edit: October 23, 2012, 04:53:10 am by llluca » Logged

Pages: [1]   Go Up
Jump to: