Home › Forums › Miscellaneous › SensorModbusMaster › Reply To: SensorModbusMaster
2022-12-06 at 12:09 PM
#17492
1 2 3 |
// --------------------------------------------------------------------------- // Include the base required libraries // ---------------------------------------- |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
// --------------------------------------------------------------------------- // Include the base required libraries // --------------------------------------------------------------------------- #include <Arduino.h> #include <SensorModbusMaster.h> // --------------------------------------------------------------------------- // Set up the sensor specific information // ie, pin locations, addresses, calibrations and related settings // --------------------------------------------------------------------------- // Define the sensor's modbus address byte modbusAddress = 0x01; // The sensor's modbus address, or SlaveID long modbusBaudRate = 9600; // The baud rate the sensor uses // Define pin number variables const int sensorPwrPin = -1; // The pin sending power to the sensor const int adapterPwrPin = -1; // The pin sending power to the RS485 adapter const int DEREPin = -1; // The pin controlling Recieve Enable and Driver Enable // on the RS485 adapter, if applicable (else, -1) // Setting HIGH enables the driver (arduino) to send text // Setting LOW enables the receiver (sensor) to send text // Construct software serial object for Modbus // This is just a assigning another name to the same port, for convienence // Unless it is unavailable, always prefer hardware serial. HardwareSerial* modbusSerial = &Serial1; // Construct the modbus instance modbusMaster modbus; // --------------------------------------------------------------------------- // Main setup function // --------------------------------------------------------------------------- void setup() { // Set various pins as needed if (DEREPin >= -1) { pinMode(DEREPin, OUTPUT); } if (sensorPwrPin >= 0) { pinMode(sensorPwrPin, OUTPUT); digitalWrite(sensorPwrPin, HIGH); } if (adapterPwrPin >= 0) { pinMode(adapterPwrPin, OUTPUT); digitalWrite(adapterPwrPin, HIGH); } // Turn on the "main" serial port for debugging via USB Serial Monitor Serial.begin(9600); // Turn on your modbus serial port Serial1.begin(modbusBaudRate); // ^^ use this for 8 data bits - no parity - 1 stop bits // Despite being technically "non-compliant" with the modbus specifications // 8N1 parity is very common. // Turn on debugging, if desired modbus.setDebugStream(&Serial); // Start the modbus instance modbus.begin(modbusAddress, modbusSerial, DEREPin); } // --------------------------------------------------------------------------- // Main setup function // --------------------------------------------------------------------------- void loop() { // Get data values from read-only input registers (0x04) // Just for show, we will do the exact same thing 2 ways // All values will be read as bigEndian // Some variables to hold results //int16_t reg1 = 0; //float_t reg2 = 0.0; //int16_t reg3 = 0; // Method 1: // Get three values one at a time from 3 different registers. // This code is easier to follow, but it requires more back-and-forth between // the Arduino and the sensor so it is a little "slower". //reg1 = modbus.int16FromRegister(0x04, 0x01, bigEndian); float reg2 = modbus.float32FromRegister(0x04, 0x02, littleEndian); //reg3 = modbus.int16FromRegister(0x04, 0x03, bigEndian); // Print results //Serial.print("reg1:"); //Serial.println(reg1); Serial.print("reg2:"); Serial.println(reg2); //Serial.print("reg3:"); //Serial.println(reg3); //Serial.println(); } |