Announcement

Collapse
No announcement yet.

TCP Modbus C#

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • TCP Modbus C#

    I would like to use one of the many C# libraries available to listen for TCP Modbus messages from a Click PLC C0-10DD1E-D. Specifically, when input X001 is closed I would like to send a message to my C# program, that will be listening, using the Send Command in the Click PLC software suite. Has someone here successfully done this and can you help me? I am unsure of what to put in the Sending Data dialog box as far as starting slave address and master address, etc.

  • #2
    Follow Up

    I am trying to send a text value to my C# program. I am sending using the send function in the Click program (see attached image). My C# program detects a holding register changed, but I don't know what register to look at...there are ~65000 registers. I am sending up the value of TXT1. Which register would that be in the array?




    Code:
     private void Server_holdingRegistersChanged()
            {
                System.Console.Beep();
    
                int x = server.holdingRegisters.Count();
    
    
    
                foreach (var x in server.holdingRegisters)
                {
                    System.Console.WriteLine(x.ToString());
                }
                System.Console.WriteLine("Changed");
            }
    Attached Files

    Comment


    • #3
      It would probably be best to use a Modbus library and integrate it into your C# app.
      Circumstances don't determine who we are, they only reveal it.
      Jason Wolthuis

      Comment


      • #4
        TCP Modbus C#

        If you are looking for one specific Modbus address that relates to a click memory location, the Address Picker will display or export the Modbus address for each Click variable or bit.
        Will need to select the display Modbus address option.

        Comment


        • #5
          Your text is going to show up in address 40001 since that is where you sent it to. The "Slave Address" in your WX statement will tell you where it will go in your PC.
          If you have an urgent issue, please contact AutomationDirect's Technical Support team.

          AutomationDirect.com Technical Support: 1(800) 633-0405 or (770) 844-4200 Email Tech Support

          Comment


          • #6
            Wow, I just went back and read the original post as well as my response...
            I did not even see that the OP mentioned Modbus in his first post. I should never post when I have as much going as I did yesterday...

            Which Modbus library are you using? (A link would be great).
            Circumstances don't determine who we are, they only reveal it.
            Jason Wolthuis

            Comment


            • #7
              ModBusTCP C# Follow-Up

              I am playing around with both the NModBus library found here:

              https://www.nuget.org/packages/NModbus/

              and the EasyModbus library version 1.0.5908.34700 found here:

              https://sourceforge.net/projects/eas...e=typ_redirect

              I have had some success receiving values from the PLC. I am now hoping to be able to receive a value in the Click PLC from the C# code. If I want to change a bit in the program via C# using the receive function, what would I enter for the starting slave and starting master addresses? I am guessing the starting slave address is what is coming from the C# code and the starting master address is the value I want to change in the PLC....is this correct?
              Attached Files

              Comment


              • #8
                Confused With Results

                I am sending the following commands via C#:

                Code:
                  System.Net.Sockets.TcpClient client = new System.Net.Sockets.TcpClient("192.168.1.3", 502);
                            Modbus.Device.ModbusIpMaster master = Modbus.Device.ModbusIpMaster.CreateIp(client);
                
                            master.WriteSingleRegister(Convert.ToUInt16(0), Convert.ToUInt16(210));
                                   
                            master.Dispose();
                            client.Close();
                My receiving flag gets enabled (C1). The success(C2) and error(C3) flags never enable and the ErrCode (DS1 ) changes to the value of 210 that I am sending from C#. Why would the 210 show up in the ErrCode and why does the success and error flags not enable briefly?
                Attached Files

                Comment


                • #9
                  Which unit - the Click or you C program - is acting as the Master, that is the unit which initiates each transfer?

                  If the C program is the initiator then all you need to do on the Click is to make sure the port you are using is set for Modbus.

                  For example, if the C program is sending a value to a Modbus location in the Click you DO NOT NEED a 'receive' instruction in the Click. The transfer of information is done by the Click's operating system in the background.
                  thePLCguy

                  Bernie

                  Comment


                  • #10
                    That seems scary. I can change a value in the PLC without a receive instruction in the ladder logic? I'm going to test that. Thanks bcarlton.

                    Comment


                    • #11
                      It really is not scary at all. The security is up to you as the programmer. The advisable method would be for you to move the MODBUS bits to other internal bits that do the actual control. You can use whatever security method you desire to ensure that it is actually your software that is issuing the command.
                      Circumstances don't determine who we are, they only reveal it.
                      Jason Wolthuis

                      Comment


                      • #12
                        The security concern is why the makers of the Do-more placed the DL and Modbus 'type' registers as seperate memory areas intending that external accesss to variables be only to those areas. That would leave bounds checking and security up to the core program. But there was insistence that direct access to the Do-more data tables be allowed.

                        As a side note, unless you are directly connected to the system's circuit board - directly controlling the address, data and R/W lines to the memory, you are using some system software to receive your communication and translate your request into read or write commands to the memory. So don't be concerned that there is not yet another layer of software (a read command) in the ladder.
                        Last edited by bcarlton; 04-29-2016, 02:50 PM. Reason: Adding on thoughts.
                        thePLCguy

                        Bernie

                        Comment

                        Working...
                        X