0

I just started creating a project in Visual Studio. I am completely new to this. I have already made a form, where I read data via modbus. That all works perfectly.

But sometimes I need to be able to change the settings in the form to communicate with modbus. If the settings are numbers, there is no problem. But if it is a word, then i get a error.

This is my code:

Private Sub ComboBox3_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox3.SelectedIndexChanged
        ModbusRTUCom1.Parity = ComboBox3.SelectedItem
End Sub

The error message is:

--> System.InvalidCastException: 'The conversion from string None to type Integer is invalid.'

I have a combobox with 3 items; None, Odd and Even. I already tried a lot of things i found on Google, but it does not work :'(.

The Error: error

This is the project: project

This is the itemlist for the combo: itemlist

EDIT: Nothing worked for me. Then i suddenly did something simple!

Private Sub ComboBox3_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox3.SelectedIndexChanged
    ModbusRTUCom1.Parity = ComboBox3.SelectedIndex

End Sub

  • The error message actually tells you exactly what is wrong. You are trying to assign a String value to an integer variable. Start with Set Option Strict On https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/option-strict-statement which will catch this for you at design time, then read up on type casting https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/data-types/how-to-convert-an-object-to-another-type – Hursey Jul 23 '21 at 00:52
  • "Then i suddenly did something simple" relies on the the order in the checkbox matching the ENUM order; which is not the case based on the screenshots in your question. In the enum `Odd`=1, `Even`=2 whereas in your list `Even` (1) is before `Odd` (2) so while the statement may appear to work it will not give you the correct result. – Brits Jul 24 '21 at 21:41
  • I adjusted the order after the combobox was working.The order was not correct because i was testing it with the list and also with ADD function. – Yoni De Volder Jul 26 '21 at 16:57

1 Answers1

0

I'm assuming you are using the EasyModbus library. That being the case lets look at the sample code:

ModbusClient modbusClient = new ModbusClient("COM1");
//modbusClient.UnitIdentifier = 1; Not necessary since default slaveID = 1;
//modbusClient.Baudrate = 9600; // Not necessary since default baudrate = 9600
//modbusClient.Parity = System.IO.Ports.Parity.None;
//modbusClient.StopBits = System.IO.Ports.StopBits.Two;
//modbusClient.ConnectionTimeout = 500;         
modbusClient.Connect();

modbusClient is a ModbusClient and modbusClient.Parity is of type Parity (an enum which has the underlying type integer).

So the error is telling you that you cannot assign a string to a variable of type Integer. As per the sample you can set the parity with a statement like modbusClient.Parity = System.IO.Ports.Parity.None (note that System.IO.Ports.Parity.None = 0); modbusClient.Parity = "none" will result in the error you are seeing.

The example in the SerialPort Class docs shows how to convert this to/from a string and this answer may also be useful.

If you use Option Strict On then the compiler should pick up issues like this (its generally better to deal with errors at compile time).

Based upon the answer you posted it seems you have not understood the above; here is a full demonstration showing converting string <--> Parity.

Option Strict On 
Imports System.IO.Ports

Module VBModule
 
    Sub Main()
            
    REM The below gets an array of strings containing the available options for parity
    REM Use this to populate your dropdown
    dim parityOptions as String()
    parityOptions = [Enum].GetNames(GetType(Parity))
    
    REM print out available options for parity (as text)
    Console.WriteLine("Possible Parities")
    For Each s As String In parityOptions
       Console.WriteLine(s)
    Next
    
    REM Now lets take a parity in string form and convert to enum
    REM This is what you need to do when you get the value from the dropdown
    Dim parityString as string
    parityString = "Even"
    Dim parity as System.IO.Ports.Parity
    parity = DirectCast([Enum].Parse(GetType(Parity), parityString), System.IO.Ports.Parity)
    Console.WriteLine("Value of parity")
    Console.WriteLine(parity)
    REM convert a second option...
    parityString = "Odd"
    parity = DirectCast([Enum].Parse(GetType(Parity), parityString), System.IO.Ports.Parity)
    Console.WriteLine(parity)
    
    End Sub
End Module
Brits
  • 14,829
  • 2
  • 18
  • 31