how to write data like string from industruino to pc through modbus network(rs485=usb)
i want to send data to PLC from arduino.So I am considring industruino as master and and PLC as slave ..BUT currently m not having PLC so I have to test it on modscan32 whether data is going or not..on holding register ...on modscan32 holding register series is like 0x4000001,0X4000002. how to write data on these series of register
Hi, modbus registers are 16-bit so each register can hold two 8-bit characters.
If you want the Master to send registers to the Slave, you can use function 16 PRESET_MULTIPLE_REGISTERS; that will fill the holding registers at the addresses you mention. You can start from the Modbus RTU Master example at https://industruino.com/blog/our-news-1/post/modbus-rtu-master-and-slave-14 and check that library's documentation.
modbus_construct(&packets[PACKET1], SlaveID, PRESET_MULTIPLE_REGISTERS, 0, 4, 0);
This constructor allows you to write 4 registers (e.g. 8 8-bit characters) to slave addresses 0x40001-0c40004 from the Master's register index 0-3.
Modbus can pass registers only, via a register array. You can use serial.write() to communicate via the RS485 port to another device that has an RS485, such as this example with an arduino UNO https://industruino.com/blog/our-news-1/post/indio-communication-with-arduino-over-rs485-31
an we use serial.write() to write on PLC??how can we check Serial.write() is actually writing data or not.Because we dont have PLC, we witing code for write on rs485port on PLC.modbus slave simulator tool is useful or not??
#include <Wire.h>
#include <Indio.h>
#include <SimpleModbusMaster.h>
#include <UC1701.h>
static UC1701 lcd;
/*
this example uses holding registers:
[0-3] sends 4 registers (int) to the slave: status of 4 LEDs = digital outputs of the slave
[6-7] reads 2 registers (int) from the slave: status of 2 PUSH BUTTONS = digital inputs of the slave
actually we read 4 registers but only 2 are connected to buttons
*/
//////////////////// Port information ///////////////////
#define baud 9600 // use 9600 on D21G
#define timeout 2000
#define polling 20 // the scan rate, standard was 200
#define retry_count 10
// used to toggle the receive/transmit pin on the driver
#define TxEnablePin 9 // INDUSTRUINO RS485
#define SlaveID 1
// The total amount of available memory on the master to store data
#define TOTAL_NO_OF_REGISTERS 4 // e.g. INDIO digital I/O
// This is the easiest way to create new packets
// Add as many as you want. TOTAL_NO_OF_PACKETS
// is automatically updated.
enum
{
PACKET1, // set 4 registers
TOTAL_NO_OF_PACKETS // leave this last entry
};
// Create an array of Packets to be configured
Packet packets[TOTAL_NO_OF_PACKETS];
// Masters register array
unsigned int regs[TOTAL_NO_OF_REGISTERS];
void setup()
{
lcd.begin();
lcd.clear();
lcd.setCursor(1, 1);
lcd.print("hello Industruino!");
lcd.setCursor(1, 3);
lcd.print("Modbus RTU Master");
Serial.begin(9600);
// Initialize each packet: packet, slave-id, function, start slave indexof, number of regs, start of master index
// set 4 registers
modbus_construct(&packets[PACKET1], SlaveID, PRESET_MULTIPLE_REGISTERS, 0, 4, 0);
// Initialize the Modbus Finite State Machine
modbus_configure(&Serial, baud, SERIAL_8N2, timeout, polling, retry_count, TxEnablePin, packets, TOTAL_NO_OF_PACKETS, regs);
// Serial = INDUSTRUINO RS485 -- Serial1 on 32u4/1286
}
void loop()
{
modbus_update(); // send Master request to Slave, as defined above
for (int u = 0; u < 3; u++) {
regs[u] = 4;
}
}
I have written this code to write on modscan tool.I am using usb to rs485 converter between arduino and pc.But in modscan no data getting reflected.Its showing "**MODBUS message TIMED-OUT**" and "Recieved invalid Response to MODBUS Query".
hi, it looks like your modscan tool is a MASTER https://www.win-tech.com/html/modscan32.htm, not a slave. the master sends out queries, the slave only responds to queries. you can use the slave example of the Industruino to make it work with a master device.
I tried with modbus slave tool, slave tool does not reflect any data. is the above code is correct? is below writes the data to holding register? for (int u = 0; u
you do not need to include the 'Serial.begin(9600);'. the code should work; you need to check this at the slave side: slave_id = 1, baud=9600, format SERIAL_8N2. if that is all fine, then try swapping the A/B wires on the RS485 just to try if that makes a difference. also note that there is a hardware switch on the baseboard to disable the RS485, it should be enabled by default but just double check https://github.com/Industruino/documentation/blob/master/indio.md#rs485
Your answer
Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!
Keep Informed
About This Forum
This community is for professionals and enthusiasts of our products and services.
Read GuidelinesQuestion tools
Stats
Asked: 2/13/20, 10:41 AM |
Seen: 3235 times |
Last updated: 2/19/20, 12:07 PM |
can we wtire on slave using serial.write() .