Perhaps you meant that I am using MAX485, but here I am using UART TTL to RS485.
try
#define TxEnablePin 2
see if it works
If not then you need anRS485 board with a TX enable pin or a different library
use 3911 on the modscan ( on pc ), if you take a look at the log it does not use address 3911 but 3910 ( in the log 01 03 0f 46 where 0xf46 = 3910 )
Use 3910 on the arduino if simplemodbusmaster is 'true-to-address' as is modbusmaster
on the lower right it says it can't connect to port com3
You also changed something else.
Try resetting the mega
I have reset it, but the issue remains the same.
Give the modbusmaster library a try ( the basic example is ok to start )
The simplemodbusmaster is not that simple if it requires a modbus_construct and a modbus_configure
P.S.
Also as you have an usb/485 adapter on pc try sniffing the communication ( with the pc ) while transmitting on arduino, you should have something similar to: 01 03 0f 46 00 02... if you are reading two words
Connect Mega GND to PM1200 GND
Do not connect Mega pin 2 to anything.
It still may not work since your RS485 board does not have a Tx Enable pin.
My PM1200 not available GND only L-N
I am using the code below
/*
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 <marius at kintel dot net>
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 <ModbusMaster.h>
/*!
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 MAX485_DE 3
#define MAX485_RE_NEG 2
// instantiate ModbusMaster object
ModbusMaster node;
void preTransmission()
{
digitalWrite(MAX485_RE_NEG, 1);
digitalWrite(MAX485_DE, 1);
}
void postTransmission()
{
digitalWrite(MAX485_RE_NEG, 0);
digitalWrite(MAX485_DE, 0);
}
void setup()
{
pinMode(MAX485_RE_NEG, OUTPUT);
pinMode(MAX485_DE, OUTPUT);
// Init in receive mode
digitalWrite(MAX485_RE_NEG, 0);
digitalWrite(MAX485_DE, 0);
// Modbus communication runs at 115200 baud
Serial.begin(115200);
// Modbus slave ID 1
node.begin(1, Serial);
// Callbacks allow us to configure the RS485 transceiver correctly
node.preTransmission(preTransmission);
node.postTransmission(postTransmission);
}
bool state = true;
void loop()
{
uint8_t result;
uint16_t data[6];
// Toggle the coil at address 0x0002 (Manual Load Control)
result = node.writeSingleCoil(0x0002, state);
state = !state;
// Read 16 registers starting at 0x3100)
result = node.readInputRegisters(0x3100, 16);
if (result == node.ku8MBSuccess)
{
Serial.print("Vbatt: ");
Serial.println(node.getResponseBuffer(0x04)/100.0f);
Serial.print("Vload: ");
Serial.println(node.getResponseBuffer(0xC0)/100.0f);
Serial.print("Pload: ");
Serial.println((node.getResponseBuffer(0x0D) +
node.getResponseBuffer(0x0E) << 16)/100.0f);
}
delay(1000);
}
This is the basic example using serial1 ( pins 18 and 19 ) at 9600 baud for modbus
Serial is still used for 'serial debug'
On the scope the data is decoded correctly ( 01 03 0c 1c 00 06 ).
Don't know what data is read, and even how to decode floating point data ( which may require swapping high and low word or don't know what else )
/*
Basic.pde - example using ModbusMaster library
Library:: ModbusMaster
Author:: Doc Walker <4-20ma@wvfans.net>
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 <ModbusMaster.h>
// instantiate ModbusMaster object
ModbusMaster node;
void setup()
{
Serial.begin(115200); // use Serial (port 0) for serial debug
Serial.println("Starting modbusmaster");
Serial1.begin(9600, SERIAL_8E1); // use Serial1 (port 1) for Modbus communication
node.begin(1, Serial1); // communicate with Modbus slave ID 1 over Serial (port 1)
}
void loop()
{
static uint32_t i;
uint8_t j, result;
uint16_t data[6];
i++;
// slave: read (6) 16-bit registers starting at register 3100 to RX buffer
result = node.readHoldingRegisters(3100, 6);
// do something with data if read is successful
if (result == node.ku8MBSuccess)
{
for (j = 0; j < 6; j++)
{
data[j] = node.getResponseBuffer(j);
}
}
delay(1000);
}
Like many RS485 devices it'd designed wrong.
No ground and no Tx enable could be all your problems.
/*
Basic.pde - example using ModbusMaster library
Library:: ModbusMaster
Author:: Doc Walker <4-20ma@wvfans.net>
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 <ModbusMaster.h>
// instantiate ModbusMaster object
ModbusMaster node;
void setup()
{
// use Serial (port 0); initialize Modbus communication baud rate
Serial.begin(115200); // use Serial (port 0) for serial debug
Serial.println("Starting Modbusmaster");
// communicate with Modbus slave ID 2 over Serial (port 0)
Serial1.begin(9600, SERIAL_8E1); // Use Serial1 (port 1) for Modbus Communication
node.begin(1, Serial1); // communication with Modbus Slave ID 1 over Serial (port 1)
}
void loop()
{
static uint32_t i;
uint8_t j, result;
uint16_t data[6];
i++;
// set word 0 of TX buffer to least-significant word of counter (bits 15..0)
// node.setTransmitBuffer(0, lowWord(i));
// set word 1 of TX buffer to most-significant word of counter (bits 31..16)
// node.setTransmitBuffer(1, highWord(i));
// slave: write TX buffer to (2) 16-bit registers starting at register 0
// result = node.writeMultipleRegisters(0, 2);
// slave: read (6) 16-bit registers starting at register 3911 to RX buffer
result = node.readHoldingRegisters(3910, 6);
// do something with data if read is successful
if (result == node.ku8MBSuccess)
{
for (j = 0; j < 6; j++)
{
data[j] = node.getResponseBuffer(j);
}
}
}
/*
Basic.pde - example using ModbusMaster library
Library:: ModbusMaster
Author:: Doc Walker <4-20ma@wvfans.net>
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 <ModbusMaster.h>
// instantiate ModbusMaster object
ModbusMaster node;
void setup()
{
// use Serial (port 0); initialize Modbus communication baud rate
Serial.begin(9600); // use Serial (port 0) for serial debug
Serial.println("Starting Modbusmaster");
// communicate with Modbus slave ID 2 over Serial (port 0)
Serial1.begin(9600, SERIAL_8E1); // Use Serial1 (port 1) for Modbus Communication
node.begin(1, Serial1); // communication with Modbus Slave ID 1 over Serial (port 1)
}
void loop()
{
uint8_t result;
uint16_t data[2];
// set word 0 of TX buffer to least-significant word of counter (bits 15..0)
// node.setTransmitBuffer(0, lowWord(i));
// set word 1 of TX buffer to most-significant word of counter (bits 31..16)
// node.setTransmitBuffer(1, highWord(i));
// slave: write TX buffer to (2) 16-bit registers starting at register 0
// result = node.writeMultipleRegisters(0, 2);
// slave: read (6) 16-bit registers starting at register 3911 to RX buffer
result = node.readHoldingRegisters(3911, 1);
// do something with data if read is successful
if (result == node.ku8MBSuccess)
{
data[0] = node.getResponseBuffer(0); // Register 3910
data[1] = node.getResponseBuffer(1); // Register 3911
Serial.print(" Register 3910: ");
Serial.println(data[0]);
Serial.print(" Register 3911: ");
Serial.println(data[1]);
} else {
Serial.println("failed to read register");
}
// Delay untuk interval pembacaan
delay(1000);
}