modbus lcd display free sample

Great article, are you familiar enough with NModbus (or the modbus protocol in general) to know if it has the ability to be event-driven. That is, to not have to user a timer or manually poll for data from a client PLC?

Finally, I know that the NModbus site was hacked and the documentation was lost. However, the author said the documentation could be recovered if rebuilt from the Nant task in the source code. (https://groups.google.com/d/topic/NModbus-discuss/BfQz-po8OiM/discussion) Perhaps you could re-host the documentation, or that could be another article! I know a LOT of developers are looking for it.

Hi Derek, there are no problem in hosting the documentation of nmodbus, but it just consists in 8 examples that can be found inside the source code (“MySample” folder).

To develop an asyncronous communication you need to have a thread dedicated to polling the modbus device. This thread listen to external calls from the application and executes reads and writes to the plc. When data are ready it fires a “DataReady_Event(EventArg data)” to all subscribers.

Hi, I’ve problem using nmodbus library with plcsimulator. When i’m trying to read data in intervals ~10sec, all slots in simulator becoming full in 10 secconds.

I belive this library uses modbus (but not sure). However it also lacks the event driven communication. I understand that you can create a thread that polls the PLC and generates the event when it detects that data is changed. However the optimal solution would be that the PLC (or modbus device) sends the data itself upon change. Does such mechanism exist for the modbus protocol?

If your PLC (or another modbus device ) is a master, so your PC application will be a slave. In this case the PLC(master) can share is information when ready to do it, and the PC(slave) only have to receive it.

I think you must read something about the Modbus Protocol first. The Modbus protocol is very simple to understand, so you don’t need so much time to do it.

So from what I understand you have a master and a slave configuration. The master sends out requests which the slave node(s) reply to. Typically a polling scenario. In order to get data upon events I have to set up the PLC as a master which writes to the PC as a slave. This would require some kind of extra logic (PLC program) set up in the PLC which I want to supervise. With TCP-modbus it is possible to setup the PC and PLC as both master and slave (on different connections). Not the general simple solution I was looking for so I think if I want an event driven communication it’s better to go with some OPC-solution which has already solved what I’m looking for.

Hi Eric, probably you are confusing the two libraries, but S7.Net use S7 protocol, nModbus use Modbus protocol (that depending on the physical cable can be Modbus TCP (ethernet) or Modbus RTU (RS 422 – RS 485).

Which to me indicated that the library used modbus TCP for communication. I kind of doubt that it is derived from LibNoDave after reading this discussion:

And by looking at the source it seems like it’s really implemented in C# all the way down to the byte communication. But it may very well use another protocol than modbus and I’m quite eager to find out which protocol. We did not set up anything (if I remeber correctly) on the PLC side to be able to access it using S7.net

For one project, I want to control a servo motor/drive (Delta ASDA B2) though c# program. I want the motor to be controlled directly through the software code using Modbus/RS 232. The drive supports both protocols. I do not want to use a PLC. Is it possible? Please advise.

I am working one one project which needs to realise the Modbus RTU master function in C#, in order to read the data value from the slaves. I found that your library is the best, but ..my problem is that: Can you explain to me the structure of your library? In fact I do not know which parts are the library excatly. It has so 6 projects when I open it in Visual C# 2010… some other I can not even open…

I still did not figure out how to add your library to my C# project. Is that I add all the projects to my solution, then I can use the library? When I done like this, it still does not work. Is that MySample is a sample folder only to show how to programming with Modbus? MySample is not a library? Sorry for my questions, I really could not make it work!

Sorry for bothering you again. I would like to ask that, I want to make one software application which will read and display the Modbus RTU communication register values. How can I do it? should I create a IModbusSerialMaster first, then send message to slave? I want to display all the regoister values when I connect my software and PLC. Which is the function to read from RTU slave? Thanks a lot. Please even give me a general hint is also very appreciated. As I am new to this field, but need to get the work done still.Thanks!

thanks for answering . but they are too much for me I do not understand what do you mean. Can you explain little bit in detail? In fact what I want is: user enter slave ID, comport number, and baudrate then I have one button which will connect to the corresponding port automatically. Then the value of the registers should be displayed in datagrdiview…I get got stuck in this connect button….You gave is a very good library, but I do not know where should I start to do. Hope you understand, Also you know any Modbus RTU slave simulator?

If you have problems with the connection of your device, you should ask help to the device’s manifacturer. Using NModbus implies that you know what is modbus and that you can debug the connection, and also you should have all the hardware needed for the communication.

hello everybody , i have just download the NModBus library to read some Holding Registers of a device , in my c# 2010 express solution explorer added the reference to the modbus.dll from binary/net of the extracted folder , added all namespaces from modbus ie using modbus.data , using modbus.io and so on . then also added ftdAdapter.dll and wrote a small code to read holding registers using modbus RTU as explained by mesta at this page . But my project is not able to build .

it says namespace name ‘Modbus’ could not be found (are you missing a using directive or an assembly reference? u guys are way ahead this issue , please drop me a line of advice .

Got out of obstacle but stuck at the next . well , I have a Modbus tester from namely “Modbus tester ” http://modbus.pl , this is something I have used for years . I have made some usual settings inside this software (protocol = Modbus RTU , pairity = even as per my device and so on) . starting address address = 3901 , no of registers = 2 , type of reg = holding and so on . This Modbus tester software reads my device correctly and also sets the display correctly in binary / hex / float etc . This is perfect . but when I run the same settings using same converter same device with Modbus RTU holding register read (start address = ushort 3901 and no of reg = ushort 2 , device address = 1) , my c# code throws an exception saying “Function code 131 , exception code 2 , the data address received in query is not an allowable address ” .

For the time being this is beyond me , how come NModbus driver is interpreting the same working address as wrong ? anybody has any clue ? Please drop me a line .

Depending on the Modbus, you can sniff the packets with Wireshark if is Modbus TCP, or with SerialPortMonitor if is Modbus RTU (check the download links in the article).

Mesta u r very correct . memory I was reading were meant to be read as a block but I was trying to read only 4 locations from top out of 20 , which was giving me a CRC error . Next , a location named 3901 in the manual = address 3900 in the c# code . I was able to fix the problem by looking at the response in the serial port monitor software . Then I hooked my Oscilloscope in Rtx of com port , that too was helpful . now NModBus C# API is ROCKING in my shop floor , thanks a lot for ur expert advice .

But the result is the whole application exits. What I want is I can connect and disconnect manually, not exits. Is there anything related to the Modbus 485 cable I amusing now? Please help, so long time suffering from this problem. Thanks so much!

Hi Mesta i have had started long time ago from here now i have written my own modbus implementation for S7 300 / 400 / 1200 and also have written a small library using tcp ip fetch and write service to talk to above mentioned PLCs and they all work as expected . There is one thing i am still strugling with and that is drawing line circles and rectangles on the winform . While developing a SCADA one has to show pipelines and stuff , is there any windows OS paint like program using which i could draw those static graphics on winform ?

However, my PC answers to all masters requests independent the value of UnitID present in the TCP frame sent by the master. I have been looking to some management of the requests addressed to a particular slave like this one for the ModbusSerialSlave:

I know that probably it’s not the answer that you expect, but you should ask this question in the nMdobus google group: https://groups.google.com/forum/#!forum/NModbus-discuss

Dear Eloy, writing your own modbud tcp query is not complected at all . you may very easily study the modbus tcp protocol and send a message frame using c# raw tcp connection , and the device would respond to it. Me too started out with this library but started to write my own message frames and it works quite well. Also learn little bit about Wireshark which may be of great help finding out problem in your code initially .

It depends if the PLC supports 32bit transfer with Modbus. For sure you need to read 2 consecutive registers, then you need to convert the 2 numbers to Hexadecimal, then depending if it’s floating point, integer or what else, you need to convert from hex to your unit.

I just downloaded Nmodbus source and binaries. When I am running example code ‘Driver.cs’ given in Nmodbus source , i am getting IOException Checksum Failed to match .

I have been working with this library for almost a year and have a problem that I can’t solve. In searching through the NModbus discussion groups, it looks like other people have this problem as well and it hasn’t been resolved, (see https://groups.google.com/forum/#!searchin/NModbus-discuss/close$20client$20port/nmodbus-discuss/QMSyOfRcIrU/rxpCdroOxNMJ and https://groups.google.com/forum/#!searchin/NModbus-discuss/close$20client$20port/nmodbus-discuss/OhvR3hLOD8w/ncBdaQ1PUKAJ).. The problem is related to shutting down the TCP ports when the ModbusTcpSlave is disposed. If I dispose of the slave, the ports are still active. If I iterate through the Masters collection and manually close the clients, then an exception is thrown (either IO Exception or Object Disposed Exception) that I haven’t been able to catch, so the software simply crashes. This seems like a bug within the class library since the software is still trying to communicate on the port after the clients have been closed and the ModbusTcpSlave has been disposed. The only way I’ve found to close these ports is to kill the software altogether and start over. Is there a recommended way to close the client ports?

Thanks for your quick reply; however, properly closing the ports is not the problem. The problem is that as soon as they are closed, an ObjectDisposedException is thrown from NModbus.

In reviewing the NModbus source code, this is what I think the problem is: in class ModbusTcpSlave, in the method AcceptCompleted, a new master is created when a client connects and a corresponding event handler is added for masterConnection.ModbusMasterTcpConnectionClosed. The method linked to this event handler is supposed to remove the master and close the socket, but it is not being called. So, if I call the Dispose() method on ModbusTcpSlave, the slave is shutdown, but the master ports remain open. I verified this using NETSTAT from a command line. If I manually close the masters, they are closed, but the event handler created when the connection was first made is still active and polling, so the software immediately throws the exception and kills my software.

This is a NModbus problem because only NModbus can unsuscribe from the event handler. I have no way to control this event handler from my code. I have tried to catch this exception, but haven’t been successful.

This is a problem in my software because if the ModbusTcpSlave needs to be shut down and restarted (e.g., in the case of a communication failure), then when it is restarted and the master reconnects, then the original ports remain open. Eventually, those open (but now dead) ports are causing the software to crash.

I think a simple solution would be to unsubscribe from the masterConnection.ModbusMasterTcpConnectionClosed event in the ModbusTcpSlave.Dispose() method. However, I would really like your opinion on this matter, especially if there is a way to properly dispose of the slave and master connections.

I was looking at the revisions of ModbusTcpSlave.cs, that you can find here: http://code.google.com/p/nmodbus/source/list?path=/tags/NModbus_1.11.0.0/src/Modbus/Device/ModbusTcpSlave.cs&start=317

As you can see, the Dispose method of the last revision removed exactly the code that you want to add now to iterate through masters and close them (http://code.google.com/p/nmodbus/source/diff?spec=svn303&r=303&format=side&path=/trunk/NModbus/src/Modbus/Device/ModbusTcpSlave.cs).

Usually for Modbus communication I use NModbus. It’s possible to debug the error and check what’s wrong with your library, but if I would be in your place, I would switch to NModbus because of better documentation, bigger user base and the certainty that it just works. If you go on with your library, you’re on your own with bug fixing.

You need to use a serial port sniffer to debug modbus problems. There is a link in the article, download it then sniff the packets that you are sending with the new software, you also sniff the packets that you sent with the old software that worked, check the frames and understand what’s different, then adjust the new software.

Hi, How I could use this code for a .Net program?. I have to communicate a PLC with a PC using Modbus RTU and I have connected PLC with a PC using a RS485-RS232-USB converter. How I could read and write memory registers from the PLC with this address:

When i go to “Modbus.cs” and change “public const int MinimumFrameSize = more than 6;” i get new message “Message frame must contain at least (6) bytes of data”.

Maybe you can check if there is a bug in the NModbus4 library. Try to download the old sources to make a test. Here is the link: https://code.google.com/p/nmodbus/downloads/list

I receive a “valid modbus response” with slave-adress, functional-code, correct number of following registers, the registers contain the values I expect and the crc-checksum is correct. (Even calculated it on my own with the Utlity provided by NModbus4)

But *somewhere inside the library* (i assume “ModbusMessageImpl”-class), a few bytes of the response get dropped / get lost. And therefore, such exceptions arise. After some hours I was not able to locate the cause for this. In my opinion, the response is valid, but NModbus4-lib is doing something wrong!

Great article. I try to connect my Laptop to a Schneider Stepper Motor(LMDCE853) with ModBusTCP, connection is OK. Reading the address with single byte also fine. However, the error popup when I try to read the address with 2 or 4 bytes number( start address and read the length of data as 2 or 4) .

I look through those examples, and i somehow cant (understand/get to work) that slave read. I’m trying to write winforms app, that works on data change. If i’m correct, then for that answers ModbusTcpSlave right?

You can use the modbus tcp client that I posted in the section “Other Modbus TCP libraries”, to check if you configured correctly the simulator. Then if you can connect with that application, you can probably connect also with NModbus.

Sorry I didn’t recognized the code. This is the bug that we were talking in this comment: https://www.mesta-automation.com/modbus-with-c-sharp-libraries-examples/#comment-392

I pushed a stable version on github here: https://github.com/mesta1/nmodbus/commits/master, that is just all the original commits except the part where the original author removed IDisposable on ModbusTcpSlave.

With the terminal programm of the sample it works well. (I think ReadRequestResponse in ModbusAsciiTransport is writing it to a byte[] called frame). I want to use the responded message from the slave in a Label. So I can see in Form1 if I the slave accepted the changes or not. How can I implement this?

Ok but it is responding. Maybe it is not really ASCII. Because when I use “mysample” I can see the TX 58, 48 … (So it is ASCII) and then RX 1, 16, 0 … (So it is RTU?). But the Problem is that I have my own class “communication” and my own form1 and I can not Access the data from Modbus.IO.ModbusAsciiTransport ReadRequestResponse because it says it is inaccessible due to its protection Level.

When master makes a write, the slave respond with the same query that the master wrote, so the master can verify that the instruction has been sent and processed correctly (http://www.simplymodbus.ca/FC06.htm). So there is no need for you to check ReadRequestResponse, becuase it’s used only to validate the communication.

I realized that the Problem is in the writing command already. The Problem is not closing the port. So a delay or reading the response does not help. But I still don´t know what to do against the modbus.SlaveException in line 161 of ModbusTransport.cs.

I know that exceptioncode 4 means An unrecoverable error occurred while the slave was attempting to perform the requested action.from http://www.simplymodbus.ca/exceptions.htm

As i can see all the example above uses Ushort as address type, that means maximum register address is 65535, but if i want to access register address beyond this like say 400001 (which is beyond ushort range) how can i achive it. I am new to NModbus, Please guide me through.

Hello Aurelien, there is a bug with the disconnection and reconnection from a server. You can read through the comments to check if it’s your case. There is a new library, NModbus4, or you can get the old code from here:https://github.com/mesta1/nmodbus

I am into automation project right now. Need to control a despatche oven machine. Its controller based system that applies a MOdbus protocol. Last week I downloaded the Nmodbus project. I had it working (perfectly) but only with PLC modbus simulator as my slave. My boss was glad to hear that i have control now with the machine. ..But, it wasnt the case when i connected with the real machine. I set and followed all the configuration settings if the serial port. But when the code run into these line

Additional info: There is a third party software running in the master pc that seems to have no problem using the modbus. It can actuaLLY read out data from the machine. And I think to rule out some probelem in the machine itself..

If you are using serial ports it means that there can be only 1 master / client, and as many slave / server as you want. Your program is a master, same as the third party software, so they can’t live both in the same Modbus network.

Man you’re fast! I was expecting ur reply tomorrow. I was only given until tomorrow to figure out the Modbus communication..so i must be able to connect it.

The controller is a Protocol3 . It supports Modbus RTU. Currently there is software running at configuration 9600,None,8 bits, 1 of the serial port. There is no problem at all, only the software is limited in functions. So we have to design a system that can automate the machine based on the company’s data architecture. Meaning it has to be connected with our manufacturing system where we can automate and control. Because right now we have to depend on the operator (human) which has been proven to be a source of lots of problems in the production floor…And i just couldn’t even connect.