andresarmento / modbus-arduino

A library that allows your Arduino to communicate via Modbus protocol, acting as a slave (master in development). Supports serial (RS-232, RS-485) and IP via Ethernet (Modbus IP).
BSD 3-Clause "New" or "Revised" License
455 stars 267 forks source link

Arduino Due #8

Open josb86 opened 8 years ago

josb86 commented 8 years ago

Hi, Is this library compatible with the Arduino Due? I am traying run a Basic lamp example and I get a error with the hardware serial format, can you help me?

NaKroTeK commented 8 years ago

Hi josb86, If you did not resolve your problem, I had the same problem with this card : Arrow SmartEverything IoT SoM.

I manage to get it work, by tweaking ModbusSerial.cpp By default it uses this function : bool ModbusSerial::config(HardwareSerial* port, long baud, u_int format, int txPin) I tried with : remplace _VARIANT_AMEL_SMARTEVERYTHING_ with your card

#ifdef _VARIANT_AMEL_SMARTEVERYTHING_
bool ModbusSerial::config(Serial_* port, long baud, u_int format, int txPin) {
....
}

To be sure, force the lib to use this function : bool ModbusSerial::config(Serial_* port, long baud, u_int format, int txPin) instead of : bool ModbusSerial::config(HardwareSerial* port, long baud, u_int format, int txPin)

josb86 commented 8 years ago

I can´t get that this library run on the DUE please, I tried to do that say NaKroTeK but nothing succeed.

NaKroTeK commented 8 years ago

I apologise if it was not clear. Look for something like _VARIANT_AMELSMARTEVERYTHING But for your arduino due.

josb86 commented 8 years ago

Hi NaKroTeK, I understand your reply and I triyed put in the file "modbusserial.cpp":

#ifdef _SAM3XA_
bool ModbusSerial::config(Serial_* port, long baud, u_int format, int txPin) {
    this->_port = port;
    this->_txPin = txPin;
    (*port).begin(baud, format);
    while (!(*port));

    if (txPin >= 0) {
        pinMode(txPin, OUTPUT);
        digitalWrite(txPin, LOW);
    }

    if (baud > 19200) {
        _t15 = 750;
        _t35 = 1750;
    } else {
        _t15 = 15000000/baud; // 1T * 1.5 = T1.5
        _t35 = 35000000/baud; // 1T * 3.5 = T3.5
    }

    return true;
}
#endif

and

#ifndef __ARM__
bool ModbusSerial::config(Serial_* port, long baud, u_int format, int txPin) {
    this->_port = port;
    this->_txPin = txPin;
    (*port).begin(baud, format);
    while (!(*port));

    if (txPin >= 0) {
        pinMode(txPin, OUTPUT);
        digitalWrite(txPin, LOW);
    }

    if (baud > 19200) {
        _t15 = 750;
        _t35 = 1750;
    } else {
        _t15 = 15000000/baud; // 1T * 1.5 = T1.5
        _t35 = 35000000/baud; // 1T * 3.5 = T3.5
    }

    return true;
}

#endif

At the same in the file "modbusserial.h":

#ifdef _SAM3XA_
        bool config(Serial_* port, long baud, u_int format, int txPin=-1);
        #endif

and

#ifdef __ARM__
        bool config(Serial_* port, long baud, u_int format, int txPin=-1);
        #endif

And nothing happen. I got tehe same error:

C:\Users\jose\Documents\Arduino\libraries\ModbusSerial\ModbusSerial.cpp: In member function 'bool ModbusSerial::config(HardwareSerial*, long int, u_int, int)':

C:\Users\jose\Documents\Arduino\libraries\ModbusSerial\ModbusSerial.cpp:23:32: error: invalid conversion from 'u_int {aka unsigned int}' to 'UARTClass::UARTModes' [-fpermissive]

     (Serial).begin(baud, format);
C:\Users\jose\AppData\Local\Arduino15\packages\arduino\hardware\sam\1.6.9\cores\arduino/UARTClass.h:48:10: error:   initializing argument 2 of 'void UARTClass::begin(uint32_t, UARTClass::UARTModes)' [-fpermissive]

     void begin(const uint32_t dwBaudRate, const UARTModes config);

I am using the example

#include <Modbus.h>
#include <ModbusSerial.h>

// Modbus Registers Offsets (0-9999)
const int LAMP1_COIL = 100; 
// Used Pins
const int ledPin = 13;

// ModbusSerial object
ModbusSerial mb;

void setup() {
    // Config Modbus Serial (port, speed, byte format) 
    mb.config(&Serial, 38400, SERIAL_8N1);
    // Set the Slave ID (1-247)
    mb.setSlaveId(10);  

    // Set ledPin mode
    pinMode(ledPin, OUTPUT);
    // Add LAMP1_COIL register - Use addCoil() for digital outputs
    mb.addCoil(LAMP1_COIL);
}

void loop() {
   // Call once inside loop() - all magic here
   mb.task();

   // Attach ledPin to LAMP1_COIL register     
   digitalWrite(ledPin, mb.Coil(LAMP1_COIL));
}
idexon commented 6 years ago

bool ModbusSerial::config(HardwareSerial port, long baud, u_int format, int txPin) { this->_port = port; this->_txPin = txPin; //(port).begin(baud, format); (*port).begin(baud);

asdelx1 commented 5 years ago

exactly, just delete "format" and it works