Menu

Sending using textboxes instead of listbox

2017-08-21
2017-08-21
  • Ganesh Gebhard

    Ganesh Gebhard - 2017-08-21

    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:

    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

     
  • Rossmann Engineering

    Hi Ganesh,

    it seems to be a problem while parsing the String. Please make sure that spParamvalues.Children[i].ToString() are only numbers.

    Stefan

     
  • Ganesh Gebhard

    Ganesh Gebhard - 2017-08-21

    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.

     
  • Rossmann Engineering

    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

     
  • Ganesh Gebhard

    Ganesh Gebhard - 2017-08-24

    Rewriting the registersToSend[i] helped, thanks!

     

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.