// Inizializzare la tastiera al reset con 0014 (Indirizzo 1 e 9600 baud) #include "SimpleModbusMaster.h" #define connection_error_led LED_BUILTIN #define baud 9600 #define timeout 100 #define polling 200 // the scan rate #define retry_count 1 #define TxEnablePin 7 // GEVINO TFT //#define TxEnablePin A0 // GEVINO Opto //#define TxEnablePin 0 // RS485 Module enum { PACKET1, // Letture tasti // PACKET2, // leave this last entry TOTAL_NO_OF_PACKETS_A }; enum { // PACKET1, PACKET2, // Bit comando led // leave this last entry TOTAL_NO_OF_PACKETS_B }; enum { PACKET3, // Sovrascrittura tasti e contatore // leave this last entry TOTAL_NO_OF_PACKETS_C }; Packet packets_A[TOTAL_NO_OF_PACKETS_A], packets_B[TOTAL_NO_OF_PACKETS_B], packets_C[TOTAL_NO_OF_PACKETS_C]; packetPointer packet1 = &packets_A[PACKET1]; packetPointer packet2 = &packets_B[PACKET2]; packetPointer packet3 = &packets_C[PACKET3]; uint16_t input[10]; uint16_t output_led[10]; uint16_t output_res[10] = {0,0,0,0,0,0,0}; void setup() { SerialUSB.begin(115200); // read 7 registers starting at address 0 packet1->id = 1; packet1->function = READ_HOLDING_REGISTERS; packet1->address = 0; packet1->no_of_registers = 6; packet1->register_array = input; packet1->connection = 1; // write the 1 registers to the PLC starting at address 6 packet2->id = 1; packet2->function = PRESET_MULTIPLE_REGISTERS; packet2->address = 6; packet2->no_of_registers = 1; packet2->register_array = output_led; packet2->connection = 1; // write the 7 registers to the PLC starting at address 0 packet3->id = 1; packet3->function = PRESET_MULTIPLE_REGISTERS; packet3->address = 0; packet3->no_of_registers = 7; packet3->register_array = output_res; packet3->connection = true; modbus_configure(baud, timeout, polling, retry_count, TxEnablePin); pinMode(connection_error_led, OUTPUT); } unsigned long Timer = millis(), Timer2 = millis(); byte Ciclo = 0; void loop() { // uint16_t connection_status = modbus_update(packets_A, 1); // Legge // // if (connection_status != TOTAL_NO_OF_PACKETS_A) // { // digitalWrite(connection_error_led, HIGH); // // re-enable the connection // // Dopo retry_count volte errori lui si ferma e accende il led, dopo di che se dai il comando sotto riparte con i tentativi // packets_A[connection_status].connection = true; // } // else digitalWrite(connection_error_led, LOW); // update the array with the counter data // xx = packet1->requests; // xx = packet1->successful_requests; // xx = packet1->total_errors; // xx = packet2->requests; // xx = packet2->successful_requests; // xx = packet2->total_errors; // if( millis() - Timer > 1000){ // Timer = millis(); // SerialUSB.print((int)input[0]); // SerialUSB.print("\t"); // SerialUSB.print(((char*)input)[2]); // SerialUSB.print("\t"); // SerialUSB.print(((char*)input)[3]); // SerialUSB.print("\t"); // SerialUSB.print(((char*)input)[4]); // SerialUSB.print("\t"); // SerialUSB.print(((char*)input)[5]); // SerialUSB.print("\t"); // SerialUSB.print(((char*)input)[6]); // SerialUSB.print("\t"); // SerialUSB.print(((char*)input)[7]); // SerialUSB.print("\t"); // SerialUSB.print(((char*)input)[8]); // SerialUSB.print("\t"); // SerialUSB.print(((char*)input)[9]); // SerialUSB.print("\t"); // SerialUSB.print(((char*)input)[10]); // SerialUSB.print("\t"); // SerialUSB.print(((char*)input)[11]); // SerialUSB.print("\t"); // SerialUSB.println(((char*)input)[12]); // // } if( millis() - Timer2 > 3000 ){ Timer2 = millis(); switch (++Ciclo){ case 1: if(packet2->connection == true ) SerialUSB.println("Green ok"); else SerialUSB.println("Green NO"); output_led[0] = 2; // Led Verde packet2->connection = true; // modbus_update(packets_B, 1); break; case 2: if(packet2->connection == true ) SerialUSB.println("Red ok"); else SerialUSB.println("Red NO"); output_led[0] = 1; // 3 beep e led rosso packet2->connection = true; // modbus_update(packets_B, 1); Ciclo = 0; break; } } modbus_update(packets_B, 1); // modbus_update(packets_C); // Azzera il contatore e tasti. }