Author Topic: OpcDaCom - InvalidCastException  (Read 1598 times)

andrew_pj

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
OpcDaCom - InvalidCastException
« on: February 12, 2015, 12:43:05 AM »
Dear Archie,

I am using AHMI version 3.97e.
Got an error "InvalidCastException" on OpcDaCom.vb (Refer to attachment for details)
This occurs when I close the program by calling Application.Exit()
Please advise, Thank you.


Best regards,
Andrew

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: OpcDaCom - InvalidCastException
« Reply #1 on: February 12, 2015, 09:30:05 PM »
You can try adding this Right after the "For" line:

If values(i).ClientHandle is Nothing then Exit For

Uncle3MTA3

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: OpcDaCom - InvalidCastException
« Reply #2 on: October 01, 2015, 10:30:46 AM »
I know this is an old thread, but I'm having this same error show up. My application originally only had the EthernetIPforCLXCom driver and everything was fine. I've since added a second driver for OpcDaCom to communicate with an OPC server on my local machine to talk to ABB robots. Now every time I exit the program, either with the 'x' in the top right hand corner or with the exit button (which has Application.Exit()) I get the same error on the same line. I've tried adding the line you suggest, but it doesn't seem to fix the error.
Any help would be fantastic.

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: OpcDaCom - InvalidCastException
« Reply #3 on: October 01, 2015, 10:41:08 AM »
If it is only occurring on application exit, I would just trap and ignore it like this:

            Dim ReturnedValues() As String = {Convert.ToString(values(i).Value)}
            Dim PolledAddress As PolledAddressInfo
            Try
                PolledAddress = DirectCast(values(i).ClientHandle, PolledAddressInfo)
            Catch ex As Exception
                Exit Sub
            End Try

Uncle3MTA3

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: OpcDaCom - InvalidCastException
« Reply #4 on: October 01, 2015, 10:50:39 AM »
Perfect, that worked!
I'm still pretty new with VB (a lot more experience with Java and Python), so I thought about catching the exception, but I didn't know how.