254 lines
4.7 KiB
Arduino
254 lines
4.7 KiB
Arduino
|
|
||
|
#define TIMEOUT 1000 /* 1 second */
|
||
|
#define MAX_READ_REGS 125
|
||
|
#define MAX_WRITE_REGS 125
|
||
|
#define MAX_RESPONSE_LENGTH 256
|
||
|
#define PRESET_QUERY_SIZE 256
|
||
|
/* errors */
|
||
|
#define PORT_ERROR -5
|
||
|
#define DEBUG 0
|
||
|
/*pins*/
|
||
|
#define TX_ENABLE 4
|
||
|
#define RELE 9
|
||
|
#define LED 2
|
||
|
|
||
|
///////////////////////////
|
||
|
byte Macel[8]={
|
||
|
0x01, 0x03, 0x00, 0x06, 0x00, 0x02, 0x24, 0x0A};
|
||
|
byte Mrpm[8]={
|
||
|
0x01, 0x03, 0x51, 0x08, 0x00, 0x01, 0x15, 0x34};
|
||
|
byte Mcurrent[8]={
|
||
|
0x01, 0x03, 0x51, 0x07, 0x00, 0x01, 0x25, 0x37};
|
||
|
|
||
|
byte Mfw[10]={
|
||
|
0x01, 0x10, 0x00, 0x05, 0x01, 0x02, 0x12, 0x82, 0x2A, 0xC4};
|
||
|
// 0x01, 0x10, 0x00, 0x05, 0x01, 0x02, 0x00, 0x02, 0x2A, 0xC4};
|
||
|
byte Mstop[10]={
|
||
|
0x01, 0x10, 0x00, 0x05, 0x01, 0x02, 0x12, 0x81, 0x6A, 0xC5};
|
||
|
// 0x01, 0x10, 0x00, 0x05, 0x01, 0x02, 0x00, 0x01, 0x6A, 0xC5};
|
||
|
|
||
|
|
||
|
///////////////////////////////////////////////////////////////
|
||
|
int response_length;
|
||
|
byte data[10];
|
||
|
long int dataRec1;
|
||
|
long int dataRec2;
|
||
|
byte tempByte;
|
||
|
|
||
|
|
||
|
void setup() {
|
||
|
// initialize both serial ports:
|
||
|
pinMode(LED, OUTPUT); // LED
|
||
|
pinMode(TX_ENABLE, OUTPUT); // TX_ENABLE
|
||
|
pinMode(RELE, OUTPUT);
|
||
|
digitalWrite(TX_ENABLE, LOW);
|
||
|
digitalWrite(LED, HIGH);
|
||
|
digitalWrite(RELE, LOW);
|
||
|
Serial.begin(115200);
|
||
|
Serial1.begin(9600);
|
||
|
}
|
||
|
|
||
|
void loop() {
|
||
|
|
||
|
/*
|
||
|
if (Serial1.available()) {
|
||
|
int inByte = Serial1.read();
|
||
|
Serial.println(inByte, HEX);
|
||
|
}
|
||
|
*/
|
||
|
|
||
|
if (Serial.available()) {
|
||
|
|
||
|
modbusProcess();
|
||
|
|
||
|
if(DEBUG)
|
||
|
Serial.println(); // separador
|
||
|
|
||
|
response_length = receive_response(data);
|
||
|
|
||
|
if(response_length)
|
||
|
{
|
||
|
|
||
|
if(DEBUG)
|
||
|
{
|
||
|
Serial.print(" Received:");
|
||
|
Serial.print(response_length);
|
||
|
Serial.print(" Data: ");
|
||
|
for(int i=0; i<10 ; i++)
|
||
|
{
|
||
|
Serial.print(data[i]);
|
||
|
Serial.print(" ");
|
||
|
}
|
||
|
Serial.println();
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
for(int i=0; i<10 ; i++)
|
||
|
Serial.write(data[i]);
|
||
|
|
||
|
Serial.println();
|
||
|
}
|
||
|
|
||
|
|
||
|
if(data[2] == 2) // un dato de 2 bytes
|
||
|
{
|
||
|
dataRec1 = (data[3]<<8) + data[4];
|
||
|
//Serial.print("Dato: ");
|
||
|
//Serial.println(dataRec1);
|
||
|
}
|
||
|
else if(data[2] == 4) // dos datos de 2 bytes
|
||
|
{
|
||
|
|
||
|
dataRec1 = (data[3]<<8) + data[4];
|
||
|
dataRec2 = (data[5]<<8) + data[6];
|
||
|
//Serial.print("Dato: ");
|
||
|
//Serial.println(dataRec1);
|
||
|
//Serial.println(dataRec2);
|
||
|
//Serial.println();
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
void modbusProcess(){
|
||
|
|
||
|
int inByte = Serial.read();
|
||
|
|
||
|
if( inByte == 'r')
|
||
|
sendData(Mrpm);
|
||
|
else if( inByte == 'a')
|
||
|
sendData(Macel);
|
||
|
else if( inByte == 'i')
|
||
|
sendData(Mcurrent);
|
||
|
else if( inByte == 'p')
|
||
|
digitalWrite(RELE, HIGH);
|
||
|
else if( inByte == 'o')
|
||
|
digitalWrite(RELE, LOW);
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
void sendData(byte *array2send)
|
||
|
{
|
||
|
|
||
|
while( Serial.available() ) // Limpiamos buffer de entrada!
|
||
|
tempByte = Serial.read();
|
||
|
|
||
|
for( int i=0 ; i<10 ; i++ )
|
||
|
data[i] = 0;
|
||
|
|
||
|
digitalWrite( TX_ENABLE, HIGH);
|
||
|
delay(10);
|
||
|
|
||
|
for( int i=0 ; i<8 ; i++ )
|
||
|
{
|
||
|
Serial1.write(array2send[i]);
|
||
|
delayMicroseconds(1600); // distancia minima entre caracter
|
||
|
//delay(2); // distancia minima entre caracter
|
||
|
}
|
||
|
|
||
|
Serial1.flush(); // esperamos a que se envien todos los datos
|
||
|
|
||
|
digitalWrite( TX_ENABLE, LOW);
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
int receive_response(byte *received_string)
|
||
|
{
|
||
|
|
||
|
int bytes_received = 0;
|
||
|
int i = 0;
|
||
|
|
||
|
/* wait for a response; this will block! */
|
||
|
|
||
|
delay(50); // INTENTAR BAJAR ESTE DELAY AL MINIMO POSIBLE!!!!!
|
||
|
|
||
|
|
||
|
// EL WHILE NO ESPERA LO QUE TIENE QUE ESPERAR... POR ESO EL DELAY SUPERIOR...
|
||
|
/*
|
||
|
while( !Serial1.available() ) {
|
||
|
digitalWrite(2,LOW);
|
||
|
delay(1);
|
||
|
digitalWrite(2,HIGH);
|
||
|
if (i++ > TIMEOUT)
|
||
|
return bytes_received;
|
||
|
}
|
||
|
*/
|
||
|
|
||
|
// FIXME: does Serial.available wait 1.5T or 3.5T before exiting the loop?
|
||
|
while( Serial1.available() ) {
|
||
|
|
||
|
received_string[bytes_received] = Serial1.read();
|
||
|
//Serial.println(received_string[bytes_received], HEX);
|
||
|
|
||
|
bytes_received++;
|
||
|
|
||
|
if (bytes_received >= MAX_RESPONSE_LENGTH)
|
||
|
return PORT_ERROR;
|
||
|
}
|
||
|
|
||
|
return (bytes_received);
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
unsigned int crc(unsigned char *buf, int start, int cnt)
|
||
|
{
|
||
|
int i, j;
|
||
|
unsigned temp, temp2, flag;
|
||
|
|
||
|
temp = 0xFFFF;
|
||
|
|
||
|
for (i = start; i < cnt; i++) {
|
||
|
temp = temp ^ buf[i];
|
||
|
|
||
|
for (j = 1; j <= 8; j++) {
|
||
|
flag = temp & 0x0001;
|
||
|
temp = temp >> 1;
|
||
|
if (flag)
|
||
|
temp = temp ^ 0xA001;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/* Reverse byte order. */
|
||
|
|
||
|
temp2 = temp >> 8;
|
||
|
temp = (temp << 8) | temp2;
|
||
|
temp &= 0xFFFF;
|
||
|
|
||
|
return (temp);
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
void modbus_query(unsigned char *packet, size_t string_length)
|
||
|
{
|
||
|
|
||
|
int temp_crc;
|
||
|
|
||
|
temp_crc = crc(packet, 0, string_length);
|
||
|
|
||
|
Serial.print("CRC: ");
|
||
|
Serial.print( temp_crc >> 8 );
|
||
|
Serial.print(" ");
|
||
|
Serial.println( temp_crc & 0x00FF );
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|