// Installare, dal library Menager di Arduino, "ModBusMaster by Doc Walker" /* RS485_HalfDuplex.pde - example using ModbusMaster library to communicate with EPSolar LS2024B controller using a half-duplex RS485 transceiver. This example is tested against an EPSolar LS2024B solar charge controller. See here for protocol specs: http://www.solar-elektro.cz/data/dokumenty/1733_modbus_protocol.pdf Library:: ModbusMaster Author:: Marius Kintel Copyright:: 2009-2016 Doc Walker Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #include /*! We're using a MAX485-compatible RS485 Transceiver. Rx/Tx is hooked up to the hardware serial port at 'Serial'. The Data Enable and Receiver Enable pins are hooked up as follows: */ #define TxEnablePin 7 // GEVINO TFT //#define TxEnablePin A0 // GEVINO Opto //#define TxEnablePin 0 // RS485 Module #define SerialUsed Serial1 // instantiate ModbusMaster object ModbusMaster node; void preTransmission(){ digitalWrite(TxEnablePin, HIGH); } void postTransmission(){ digitalWrite(TxEnablePin, LOW); } void setup() { pinMode(TxEnablePin, OUTPUT); digitalWrite(TxEnablePin, LOW); // Modbus communication runs at 115200 baud SerialUsed.begin(9600); // Modbus slave ID 1 node.begin(1, SerialUsed); // Callbacks allow us to configure the RS485 transceiver correctly node.preTransmission(preTransmission); node.postTransmission(postTransmission); } bool state = true; unsigned long Timer = millis(), Timer2 = millis(); byte Ciclo = 0; void loop() { uint8_t result; if( millis() - Timer2 > 1000 ){ Timer2 = millis(); node.readHoldingRegisters(0, 7); if( node.getResponseBuffer(0) == 5 ){ delay(100); node.setTransmitBuffer(0, 0); // Azzero contatore node.setTransmitBuffer(6, 2); // led Verde node.writeMultipleRegisters(0, 7); delay(1200); } SerialUSB.print(node.getResponseBuffer(0)); SerialUSB.print("\t"); SerialUSB.print((char)node.getResponseBuffer(1)); SerialUSB.print("\t"); SerialUSB.print((char)(node.getResponseBuffer(1)/0xFF)); SerialUSB.print("\t"); SerialUSB.print((char)node.getResponseBuffer(2)); SerialUSB.print("\t"); SerialUSB.print((char)(node.getResponseBuffer(2)/0xFF)); SerialUSB.print("\t"); SerialUSB.print((char)node.getResponseBuffer(3)); SerialUSB.print("\t"); SerialUSB.print((char)(node.getResponseBuffer(3)/0xFF)); SerialUSB.print("\t"); SerialUSB.print((char)node.getResponseBuffer(4)); SerialUSB.print("\t"); SerialUSB.print((char)(node.getResponseBuffer(4)/0xFF)); SerialUSB.print("\t"); SerialUSB.print((char)node.getResponseBuffer(5)); SerialUSB.print("\t"); SerialUSB.println((char)(node.getResponseBuffer(5)/0xFF)); } // if( millis() - Timer > 3000 ){ // Timer = millis(); // switch (++Ciclo){ // case 1: // node.setTransmitBuffer(0, 1); // 3 beep e led rosso // node.writeMultipleRegisters(6, 1); // break; // case 2: // node.setTransmitBuffer(0, 2); // led Verde // node.writeMultipleRegisters(6, 1); // Ciclo = 0; // break; // } // } }