Forum Replies Created
Viewing 2 posts - 1 through 2 (of 2 total)
-
AuthorPosts
-
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102// ---------------------------------------------------------------------------// 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 addressbyte modbusAddress = 0x01; // The sensor's modbus address, or SlaveIDlong modbusBaudRate = 9600; // The baud rate the sensor uses// Define pin number variablesconst int sensorPwrPin = -1; // The pin sending power to the sensorconst int adapterPwrPin = -1; // The pin sending power to the RS485 adapterconst 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 instancemodbusMaster modbus;// ---------------------------------------------------------------------------// Main setup function// ---------------------------------------------------------------------------void setup(){// Set various pins as neededif (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 MonitorSerial.begin(9600);// Turn on your modbus serial portSerial1.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 desiredmodbus.setDebugStream(&Serial);// Start the modbus instancemodbus.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();}
BTW I am using a teensy 4.1 module
-
AuthorPosts
Viewing 2 posts - 1 through 2 (of 2 total)