Menu

The ReadInputRegisters doesn't work

Anonymous
2016-06-16
2016-07-07
  • Anonymous

    Anonymous - 2016-06-16

    Hello everybody,

    I use a Modicon M221 TCP with EasyModbusTCP library on M.VisualStudio 2015. I have no problem with the connection which works properly.
    However when I try to recover some data from my TCP with ReadInputRegisters it send me back only one value which is weird... I actually connect with the TCP simulator with is IP adress (127.0.0.1) and when i switch the inputs in the simulator it doesn't seems to impact on the values...

    Here's part of my code, did I do something wrong? Thanks for your help.

    Private Sub Button1_Click() Handles Button1.Click
            'This subroutine will communicate using the EasyModbusTCP to the PLC
            Dim ComError = 0 'Set communication error flag to 0
            'Specify the IP Address and Port Number that we are connecting
            Dim ModbusClient As EasyModbus.ModbusClient = New EasyModbus.ModbusClient(TextBox1.Text, 502)   'The IP of the simulator is in the txtBox
            Try
                ModbusClient.Connect() 'Connect to the PLC
            Catch ex As Exception 'What to do when an error occurs
                Label10.ForeColor = Color.Red
                Label10.Text = "Communication Error!"
                ComError = 1 'Set communication error flag to 1
            End Try
            If ComError = 0 Then 'Do the following when communication is OK
                Label10.ForeColor = Color.Black
                Label10.Text = "Logging..."
                Dim Registers As Integer() 'Where to store the information
                Registers = ModbusClient.ReadInputRegisters(0, 3) 'Read three registers starting at the first one
                Label1.Text = Registers(0)     ''''''''''''''''''''''
                Label2.Text = Registers(1)     ''Display the values'' 
                Label3.Text = Registers(2)     ''''''''''''''''''''''
                ModbusClient.Disconnect() 'Disconnect from the PLC
    
     
  • Rossmann Engineering

    Hello,

    I just tried and it worked. I tried with a PLC, as well as Server Simulator. Try to get the new version, otherwise could you trace communication with Wireshark, or with the Client Demo in the Download area, and send it to me.

     
  • Jack

    Jack - 2016-06-17

    Many thanks for your answer ! =)

    Did you try with my code and with the Somachine BASIC software PLC's simulator ? Is there a specific kind of PLC needed for the library to work ?

    I tried with the ClientDemo to connect to the simulator of Somachine Basic, and an error occured (see attached screeshot). I got excatly the same error with the PLC (Modicon TM221CE16R).
    However I have not such an error with my code in M.VisualStudio, it just don't work because I can't retrieve the values of my simulator Inputs.

    Thanks again for your help.

     
  • Jack

    Jack - 2016-06-17

    Just to be clear why I say my values are weird.
    Here is my code :

    Private Sub Button1_Click() Handles Button1.Click
            'This subroutine will communicate using the EasyModbusTCP to the PLC
            'This will execute when the user clicks the button or at specific intervals from the Timer1
    
            Dim ComError = 0 'Set communication error flag to 0
            'Specify the IP Address and Port Number that we are connecting
            Dim ModbusClient As EasyModbus.ModbusClient = New EasyModbus.ModbusClient(TextBox1.Text, 502)
            Try
                ModbusClient.Connect() 'Connect to the PLC
            Catch ex As Exception 'What to do when an error occurs
                Label10.ForeColor = Color.Red
                Label10.Text = "Communication Error!"
                ComError = 1 'Set communication error flag to 1
            End Try
            If ComError = 0 Then 'Do the following when communication is OK
                Label10.ForeColor = Color.Black
                Label10.Text = "Logging..."
                Dim Registers As Integer() 'Where to store the information
                Dim Registers2 As Integer()
                Registers = ModbusClient.ReadHoldingRegisters(0, 3)  **'HERE I RECOVER THE HOLDING REGISTERS**
                Registers2 = ModbusClient.ReadInputRegisters(0, 3)   **'HERE I RECOVER THE INPUT REGISTERS**
                Label1.Text = Registers(0) 'Value of 1st Register
                Label2.Text = Registers(1) 'Value of 2nd Register
                Label3.Text = Registers(2) 'Value of 3rd Register
                Label12.Text = Registers2(0) 'Value of 1st Register
                Label13.Text = Registers2(1) 'Value of 2nd Register
                Label14.Text = Registers2(2) 'Value of 3rd Register
                ModbusClient.Disconnect() 'Disconnect from the PLC
    

    You can see in the attached sreenshot that in my form, no matter if it's the holding or input registers, the values are the same (if it's the same starting adress, here 0).
    However i have changed my analog input values in the simulator...

     
  • Jack

    Jack - 2016-06-17

    Sorry for the multiple posts but I give you the WiresShark report which contained several "value refreshs". I'm not really familiar with this software so I don't really know how to use this report.

    Thanks for all the help :)

     

    Last edit: Jack 2016-06-17
  • Rossmann Engineering

    Hello,

    I have a Schneider LMC201C running with SoMachine Motion.
    There are no requirements to use a specific PLC. If the PLC supports Modbus-TCP Server, it has to work.

    I don't know which version of SoMachine Basic you use, but if you send it to me (Then send an private Message) i'll try. Otherwise we could try to have a look via TeamViewer, if you want to.

    What you see for sure in your first post is, that the Client sends a valid request to the server, but the server answers with an error code, that say's that the starting address or the number of values are invalid.

    Try to read values from address "2", until the server answers with a valid response. Could be that you somehow limited the amount of Registers in the PLC.

     
  • Jack

    Jack - 2016-06-20

    Hello,

    Thanks for your answer.

    I can't say why but the Client Demo is working today (see ScreenShot) only with the IP of the Simulator with the PLC I got the same error of starting adress, but the values of input registers stay at 0 even if I change my analog inputs on the simulator.
    However I can't get the same values of 0 in VB, even with the same starting adress and number of values requested.

    EDIT : Now I have the same values in Client Demo and my VB application, but still don't respond to my changes in the simulator. Moreover when I change the number of values requested (without changing the address), it modifies the values and add somme null values. (see the attachement with Client Demo and my VB form with differents numbers of values requested).

    I use Somachine BASIC 1.4. I sent you my work in private message.

    We could have a look on teamViewer it could be usefull.

    Thanks again for your help.

     

    Last edit: Jack 2016-06-20
    • Rossmann Engineering

      Hello,

      thanks for your message. I would love to help find out what is wrong there.

      I would really ask you for the whole VB Project AND the SoMachine Project. So I don't have to spend so much time in creating projects out of the Code snippets.

      Thanks a lot

      Stefan

       
  • Jack

    Jack - 2016-06-21

    Hello, thank you for your amazing help.

    I gave you a link (Private Message) to Download my all VB program, there's a part of the code that redirect the datas to a database which is not important for you it works well on my pc but won't works on yours because of the direction path, I put a condition IF(false) on it in order it doesn't execute.

    I don't have any Somachine BASIC Project I just put an end on the program in order there's no error and run the simulator. In the future many different programs will run on it.

    Actually that is one of my questions, do I need, and if yes, how I write in the different registers the informations I want to recover ?

     

    Last edit: Jack 2016-06-21
  • Jack

    Jack - 2016-06-22

    Did you receive my private messages ? I can't see where MY private messages arrive... Where is it ?

    Thanks.

     
  • Rossmann Engineering

    Hi,

    received your message. THANKS. i did not download the file so far. I will take care at the weekend.

     
  • Jack

    Jack - 2016-06-23

    Hi,

    Thank you ! Another precision, my VB code works perfectly with the server simulator =)

     
  • Jack

    Jack - 2016-06-27

    Hi ! =)

    Did you have a look at my work ? Any idea why the recover datas don't change with the PLC simulator ?

    Thanks! =)

     
  • Rossmann Engineering

    Hi Jack,

    I tried a little bit with the SoMachine Basic Simulator, and I must say it was working perfectly.

    See some screenshots attached. Maybe I did not understand the whole problem.

     
  • Rossmann Engineering

    The Console Output

     
  • Rossmann Engineering

    Damn small C# Programm

     
  • Jack

    Jack - 2016-06-28

    Hi,
    Thank you again !

    I tried exactly the same thing as you but still have some unknown values coming out (see screenshot)
    My code is very similar to yours and i can't say why I don't get the values of the Memory Words back ...
    **EDIT : even with your code in C# I got the same values... Furthemore, is there any reason that you used the WriteSingleRegister function ? I guess this is to test if you recover the enter values then, but that is not my case, even if I read the registers just after writing in it, I can't recover the 123 and 1234 values... **

    Moreover, I think I need some explainations about registers. I thought that you didn't need to write anything in the PLC program (for the input registers) to get the values of inputs back ?

     

    Last edit: Jack 2016-06-28
    • Rossmann Engineering

      Hello,

      do you use the latest dll? Otherwise we try to have a look via teamviewer. Write me pn for the connection details and when it would be possible.

      Stefan

       
  • Jack

    Jack - 2016-07-04

    Hi !

    I have to thank you a lot for your patience and your work, I just update the library and it's working perfectly !!! =D Both with the simulator and the real PLC.

    Just one more question which i hope it'll be easy to answer to :

    Why the values are the same for the inputregisters and the holdingRegisters ? I thought that the inputregisters would directly give me the values of my inputs. However I need for now, to copy the values of the analog inputs to the %MWx to get those values. It's not a real problem but just a little question ^^

    Thank you again, your help was amazing .

     
  • Rossmann Engineering

    Hello !

    I also realized that, when I was working with the SoMachine Software.

    normally the MODBUS-Server has four data types. Discrete Inputs, Coils, Holding Registers and Input Registers.

    Coils(single bit) and holding registers (16 bit) can be transfered in both directions (Client <-> Server)

    Discrete Inputs (single bit) and input registers (16 bit) can only be transfered from Server to client (Server->Client)

    The mapping is different from device to device. In this case it seems that the PLC has just Bit and Word Datatypes. that means coils and discrete inputs are the same, as well as holding registers and input registers.

    STEFAN

     
  • Jack

    Jack - 2016-07-07

    Hello :)

    Thanks for this answer, happy to see that it's not only on my device.

    I would like to thank you once again and for the last time. My project is a success and it's thank to you.

    By !

     
  • Anonymous

    Anonymous - 2017-05-15
    Post awaiting moderation.
  • Anonymous

    Anonymous - 2017-11-22
    Post awaiting moderation.

Anonymous
Anonymous

Add attachments
Cancel





Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.