Skip to content

Instantly share code, notes, and snippets.

@mcorrigan
Last active November 28, 2023 22:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcorrigan/8d1016a028e831d788095c0bebc5d89b to your computer and use it in GitHub Desktop.
Save mcorrigan/8d1016a028e831d788095c0bebc5d89b to your computer and use it in GitHub Desktop.
Test Optidrive E3 Modbus RTU
#include <Arduino.h>
#include <ModbusMaster.h>
#define MAX485_DE_RE 4
ModbusMaster node;
void preTransmission()
{
digitalWrite(MAX485_DE_RE, HIGH);
}
void postTransmission()
{
digitalWrite(MAX485_DE_RE, LOW);
}
void setup()
{
Serial.begin(115200);
while(!Serial);
pinMode(MAX485_DE_RE, OUTPUT);
digitalWrite(MAX485_DE_RE, LOW);
Serial1.begin(115200); // modbus serial
node.begin(1, Serial1); // slave ID 1, use Serial1 uart
node.preTransmission(preTransmission);
node.postTransmission(postTransmission);
}
void loop()
{
Serial.println("==============");
// NOTE: The modbus library registers are 0 based, so you have to use register 0 for register 1 (and always subtract 1 from the documented regiser number)
node.writeSingleRegister(0x0000, 0x0001); //----> enable runMode (call every time?)
node.writeSingleRegister(0x0001, 0x0010); //----> set Freq
int16_t readResult = node.readHoldingRegisters(0x0005, 1);// ----> get drive status
if (readResult == node.ku8MBSuccess) {
uint16_t result = static_cast<int16_t>(node.getResponseBuffer(0));
bool isDriveRunning = result & (1 << 0);
bool isTripped = result & (1 << 1);
bool isInStandbyMode = result & (1 << 2);
bool isReady = result & (1 << 6);
Serial.print("isDriveRunning: ");
Serial.println(isDriveRunning);
Serial.print("status: ");
Serial.println(isTripped ? "Tripped" : "OK");
Serial.print("isInStandbyMode: ");
Serial.println(isInStandbyMode);
Serial.print("isReady: ");
Serial.println(isReady);
}else{
Serial.println("** Failed getVFDStatus()");
if (readResult == node.ku8MBInvalidFunction){
Serial.println(" - Invalid Function");
}else if (readResult == node.ku8MBInvalidSlaveID){
Serial.println(" - Invalid Slave ID");
}else if (readResult == node.ku8MBInvalidCRC){
Serial.println(" - Bad CRC");
}else if (readResult == node.ku8MBResponseTimedOut){
Serial.println(" - Time out");
}else{
Serial.println(" - Something else...");
}
}
delay(1000);
}
@mcorrigan
Copy link
Author

NOTE: The ModbusMaster library registers are 0 based, so you have to use register 0 for register 1 (and always subtract 1 from the documented register number)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment