Surya1993

Write single coil to Modicon TM218 Plc Using Easymodbus

5 posts in this topic

Hello, I am using Easymodbus library to Communicate with Modicon TM218 PLC. Reading and Writing of Holding Registers working fine. But coming to writing of single bit in data register i am facing Problem. Need to know how we find coil address of a data register. e.g I have to write bit 1 to MW70.1 address. How we do that. Right now i am reading complete MW70 Register and Manipulating it using bit manipulation techniques and again Writing using Write single Register Function. It's working but some times because of reading problem it writes some garbage values. At that time complete system effecting. 

 

Please provide me any solution.

Thanks in Advance. 

Share this post


Link to post
Share on other sites

well solution is to use all resources at your disposal that Easymodbus offers. and it has plenty...

on the right side is EasyModbusTCP Server Simulator. it is a great substitute for PLC when doing testing.

on the left side is corresponding client which has ability to read/write anything including single coils...

so, all you need to do is try it and look inside the code how things work. 

EasyModbus.png

Share this post


Link to post
Share on other sites

Dear @panic mode Thanks for reply. 

I did that. It's working good for me also. But my problem is writing single bit in a Data register. 

Private Sub btnSealing_Click(sender As Object, e As EventArgs) Handles btnSealing.Click
        If btnManual.BackColor = Color.Green Then
            If btnSealing.BackColor = Color.Red Then
                Dim card As Integer() = ModbusC.ReadHoldingRegisters("20""1")
                ModbusC.WriteSingleRegister("20", getintArray(card(0), 10, 1))
            Else
                Dim card As Integer() = ModbusC.ReadHoldingRegisters("20""1")
                ModbusC.WriteSingleRegister("20", getintArray(card(0), 10, 0))
            End If
        End If
    End Sub

Here the code snippet of  Button click event. There every time I am reading Data register and Adding either Bit 0 or 1 and Again Writing. But some time read fails means it writing Some Garbage value. 

So seeking help to write direct Coil instead of Data Register.

 

Thank You..

 

Share this post


Link to post
Share on other sites

if you do want to write words (MW...) and set/reset single bit you need to manipulate the bit in the word and then write entire word.

if you want to ensure there is no corruption, you should first read the word, manipulate the bit of interest, then write the word back.

but first of all let's make things clear - you should not use same register for R/W access as this is too long and complicated (read>manipulate>write back) and there is always a chance of getting things out of sync. this is because value that you read could be out of date by the time you write new value back, some of bits could already be modified (in the PLC) so your write back attempt would overwrite them and thus create corruption.

it is a MUCH better approach to simply choose a range of memory locations (such as register) that you only read and choose another range that you only write. once the value is transferred, you may evaluate it at received end - no need to write back. 

that is of course if you want to reduce your communication to single read and single write... 

but if you do not wish to parse received data, then instead of reading just one range of memory locations (such as registers), read two or three or more... one could be bits/coils, another could be bytes, another could be words...

same goes for writing values back to PLC. instead of writing one range of memory locations, just use two or more writes. again, one could be some range or bits/coils, another could be bytes, another could be words...

 

Share this post


Link to post
Share on other sites

Many modbus devices support the masked write opcode, which perform an AND then OR operation on a register atomically. While this was created long ago by Gould Modicon, their corporate successor, Schneider, doesn't implement this in some of their cheaper products. /:

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now