Search the Community
Showing results for tags 'WriteOperands'.
-
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"); } }