I have taken a look at the code to send data using Write Multiple Registers, but I want to send data using the values in textboxes instead of the listbox.
Your code:
try
{
if (!modbusClient.Connected)
{
modbusClient.IPAddress = txtIpAddressInput.Text;
modbusClient.Port = int.Parse(txtPortInput.Text);
modbusClient.Connect();
}
int[] registersToSend = new int[lsbWriteToServer.Items.Count];
for (int i = 0; i < lsbWriteToServer.Items.Count; i++)
{
registersToSend[i] = int.Parse(lsbWriteToServer.Items[i].ToString());
}
modbusClient.WriteMultipleRegisters(int.Parse(txtStartingAddressOutput.Text) - 1, registersToSend);
}
catch (Exception exc)
{
MessageBox.Show(exc.Message, "Exception writing values to Server", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
My code:
try
{
if (!modbusClient.Connected)
{
modbusClient.IPAddress = "192.168.24.128";
modbusClient.Port = 502;
modbusClient.Connect();
}
int[] registersToSend = new int[spParamvalues.Children.Count];
for (int i = 0; i < spParamvalues.Children.Count; i++)
{
registersToSend[i] = int.Parse(spParamvalues.Children[i].ToString());
}
modbusClient.WriteMultipleRegisters(0, registersToSend);
}
catch (Exception exc)
{
WindowMessage.Show(exc.Message);
}
However, I get the error: 'Input string was not in the correct format.'
Do you have any ideas to solve this?
Ganesh
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi,
I have taken a look at the code to send data using Write Multiple Registers, but I want to send data using the values in textboxes instead of the listbox.
Your code:
My code:
However, I get the error: 'Input string was not in the correct format.'
Do you have any ideas to solve this?
Ganesh
Hi Ganesh,
it seems to be a problem while parsing the String. Please make sure that spParamvalues.Children[i].ToString() are only numbers.
Stefan
Hi Stefan,
They are only numbers, but I still get the error. Even when I read the machine and then send the exact same values back, I get the error.
Hi Ganesh,
you should see were the Exception is thrown, and I think it is thrown in line
registersToSend[i] = int.Parse(spParamvalues.Children[i].ToString());
So I think it is definitely an parser Exception.
I don't kno what is spParamvalues.Children[i]? If these are textboxes is it possible that you have to write
registersToSend[i] = int.Parse(spParamvalues.Children[i].Text); ?? Try to set a breakpoint and findout what string you are trying to parse.
Stefan
Rewriting the registersToSend[i] helped, thanks!