Jump to content
Sign in to follow this  
alexmoskos

Can't write value to PLC greater than 127

Recommended Posts

Hi,

I am looking at the C# example provided by Unitronics for reading and writing values to a PLC via Ethernet. I am able to connect and do a number of things but I can't seem to write a value greater than 127 to the PLC. I can write values less than 127, and I am able to read a number greater than 127 but having trouble when I try to write a value greater than 127. My assumption is that it has something to do with sending a "byte" versus "short" but am still new to Visual Studio and C#. Here is the code, if the line in BIG BOLD LETTERS below is changed to anything below 127, it works but 128 or above errors out and says "Could not communicate with the PLC". Can anyone help?:

 

 private void Read_Click(object sender, EventArgs e)
        {
            ReadWriteRequest[] rw = new ReadWriteRequest[1];
 
            rw[0] = new ReadOperands()
            {
                OperandType = OperandTypes.MI,
                NumberOfOperands = 3,
                StartAddress = 0
            };
 
            try
            {
                plc.ReadWrite(ref rw);
                
                object[] values = (object[])(rw[0].ResponseValues);
                
                for (int i=0; i<values.Length; i++)
                {
                    if (values != null)
                    {
                        TextBox txtbox = this.groupBox1.Controls["txtMI" + i.ToString()] as TextBox;
                        txtbox.Text = ((short)values).ToString();
                    }
                }
            }
            catch
            {
                System.Windows.Forms.MessageBox.Show("Could not communicate with the PLC");
            }
        }
 
        private void Write_Click(object sender, EventArgs e)
        {
            object[] values = new object[3];
            try
            {
                values[0] = (object)Convert.ToInt16(txtMI0.Text);
                values[1] = (object)Convert.ToInt16(txtMI1.Text);
                values[2] = (object)Convert.ToInt16(txtMI2.Text);
            }
            catch
            {
                System.Windows.Forms.MessageBox.Show("One of more of the values are not valid");
                return;
            }
 
            ReadWriteRequest[] rw = new ReadWriteRequest[1];
 
            rw[0] = new WriteOperands()
            {
                OperandType = OperandTypes.MI,
                NumberOfOperands = 3,
                StartAddress = 0,
                Values = values
            };
 
 
            try
            {
                plc.ReadWrite(ref rw);
            }
            catch
            {
                System.Windows.Forms.MessageBox.Show("Could not communicate with the PLC");
            }
        }
 

Share this post


Link to post
Share on other sites

Hi, we have done a lof of tests to the code and it worked in the past as it works right now,

I have tried your scenario and I can write any value that I want.

 

You did not supply the PLC Model, its OS, Boot and BinLib versions.

You did not supply the Communication Driver version.

(so I can't 100% simulate the case your are describing, although I can hardly believe that you are using a version that doesn't work properly).

 

Since I don't know what project the PLC is loaded with, then I can't also tell if something is using the value of the MI (for communication for example), which might cause the problem

 

Furthermore, the messagebox says "Could not communicate with the PLC", but what is the actual exception?

(Have you tried to see what the message says?)

Does the messagebox appears immediatly after cliking the button, or does it take like 9 seconds?

 

 

Assuming it is a V130/V350/V570/V1210/V1040... have you tried looking at the Serial Port monitor inside the PLC (or the ethernet monitor) to see what there is in the Tx and Rx (in order to know if the PLC got the request from the PC and if it has replied)?

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...