Results 1 to 18 of 18

Thread: Init Problem? Serial Port Comm Program

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2020
    Posts
    10

    Init Problem? Serial Port Comm Program

    Hi all,

    I am currently working on a project that requires me to split off a function from an already existing multi-use program that was created in house. We currently are working with serial port communication using a CAN-to-USB method.

    I am a baby programmer and have been pulling hairs out trying to figure this program out, but to no avail. I am looking for any help, not necessarily hand-holding (I am willing to put in the work) but I would love to be able to get a good understanding of where my problems are and what I can do to prevent them in the future.

    I have included a snippet of my button click event for checking/opening and writing to port. The references the init process, which is where I am getting my first error. I cannot seem to recognize the device when it is plugged in via USB.
    Name:  ClickB.jpg
Views: 549
Size:  36.3 KBName:  Init Error.jpg
Views: 479
Size:  52.7 KBName:  Error 1.PNG
Views: 545
Size:  3.2 KBName:  Obj Ref Error.PNG
Views: 548
Size:  3.8 KB

    Thank you for any and all help in advance. I know I have a lot to learn to get this running as smoothly as possible.

    Also, please let me know if there is another part of the code needed to help out with this.

    Thanks!
    M

  2. #2
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    846

    Re: Init Problem? Serial Port Comm Program

    Hello,

    are you sure your device is on port com 2 ? you can find the list of port com available (as a collection of string) with My.Computer.Ports.SerialPortNames.
    mdevice is apparently nothing so what is the code and where is it that assign something to mdevice.

    for the second error, there is a "new" missing somewhere . which object is affected by this error ?
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2020
    Posts
    10

    Re: Init Problem? Serial Port Comm Program

    Hi Delaney,

    Once plugged into the CanBUS adapter, our programs communicate through COM2, yeah. I know it's a little unorthodox, but it has been that way since before I came onboard. I have attached a snippet of the sub that assigns mDevice. In regards to the second error, the error details point to the line " bal = mDevice.OpenBusAccessLayer()".

    Thanks for the response!
    M

  4. #4

    Thread Starter
    New Member
    Join Date
    Aug 2020
    Posts
    10

    Re: Init Problem? Serial Port Comm Program

    Hi Delaney,

    Yeah, strangely enough, we use Port COM2 here. mDevice gets assigned under a private sub (photo attached), and the 2nd error is referencing the line "bal = mDevice.OpenBusAccessLayer()"

    Thanks for the help!

    Attachment 178464

  5. #5
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    846

    Re: Init Problem? Serial Port Comm Program

    your attachment doesn't work and is not the good way to present the code (as it is poorly readable), please copy/paste the code between code tags ( click on # in the editor).

    for the second error correct the declaration of bal in InitSocket :
    Code:
    dim bal as new Ixxat.......
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  6. #6

    Thread Starter
    New Member
    Join Date
    Aug 2020
    Posts
    10

    Re: Init Problem? Serial Port Comm Program

    Ah, I see. That's fairly convenient to have this code paste feature.

    The code where the mDevice is assigned:

    Code:
    Private Sub SelectDevice(ByVal DeviceEnumNumber As Long)
            'temporary holder for device tag
            Dim mTempDevice As Ixxat.Vci3.IVciDevice = Nothing
            Dim deviceHardwareID As Object
            Try
                'populate the device list, this section should be automated and removed
                deviceEnum = deviceList.GetEnumerator()
                deviceEnum.Reset()
                'continue as long as more devices are detected.   for our purpose number of devices will always be one
                Do While deviceEnum.MoveNext() = True
                    mTempDevice = deviceEnum.Current
                    If mTempDevice.VciObjectId = DeviceEnumNumber Then
                        mDevice = mTempDevice
                        ' a sample how to read the unique hardware ID from the device
                        deviceHardwareID = mTempDevice.UniqueHardwareId
                    End If
                Loop
            Catch ex As Exception
                ' todo show the exception
            End Try
        End Sub
    When I add new in the declaration of bal, I get an error that states I can't "use New on an interface"

  7. #7
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    846

    Re: Init Problem? Serial Port Comm Program

    either deviceEnum or deviceList is empty or this mTempDevice.VciObjectId = DeviceEnumNumber is never achieved

    let try to debug
    put a msgbow("fail") in the catch part (to verify if you have an exception)

    after that line :
    mTempDevice = deviceEnum.Current
    put a msgbox(mTempDevice.VciObjectId.tostring) to check what you get
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  8. #8

    Thread Starter
    New Member
    Join Date
    Aug 2020
    Posts
    10

    Re: Init Problem? Serial Port Comm Program

    The errors remained the same. Attachment 178465 <-- This one is pointing to the bal = mDevice... line, and Attachment 178466 no message showed up for mTempDevice

  9. #9
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    846

    Re: Init Problem? Serial Port Comm Program

    ok for the second error, it may not be a bal problem but a mdevice problem which is not created.

    do you have somewhere in you code something like this (or close) :

    Code:
    dim mdevice as new Ixxat.Vci3.IVciDevice
    by the way do you have on VS the option explicit on ON? if not put it...
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  10. #10
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    846

    Re: Init Problem? Serial Port Comm Program

    Quote Originally Posted by LostintheCode View Post
    The errors remained the same. Attachment 178465 <-- This one is pointing to the bal = mDevice... line, and Attachment 178466 no message showed up for mTempDevice
    that's means you don't go into the loop or the list is empty.
    when do you call the sub select device ? before or after BtnTestStart ?
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  11. #11

    Thread Starter
    New Member
    Join Date
    Aug 2020
    Posts
    10

    Re: Init Problem? Serial Port Comm Program

    Quote Originally Posted by Delaney View Post
    ok for the second error, it may not be a bal problem but a mdevice problem which is not created.

    do you have somewhere in you code something like this (or close) :

    Code:
    dim mdevice as new Ixxat.Vci3.IVciDevice
    by the way do you have on VS the option explicit on ON? if not put it...
    With the exception of the declaration statements
    Code:
     Private mDevice As Ixxat.Vci3.IVciDevice = Nothing
        Private mCanChn As Ixxat.Vci3.Bal.Can.ICanChannel = Nothing
        Private mReader As Ixxat.Vci3.Bal.Can.ICanMessageReader = Nothing
        Private mWriter As Ixxat.Vci3.Bal.Can.ICanMessageWriter = Nothing
        Private mCanCtl As Ixxat.Vci3.Bal.Can.ICanControl = Nothing
    I dont see mDevice being declared/defined anywhere else

  12. #12

    Thread Starter
    New Member
    Join Date
    Aug 2020
    Posts
    10

    Re: Init Problem? Serial Port Comm Program

    Quote Originally Posted by Delaney View Post
    that's means you don't go into the loop or the list is empty.
    when do you call the sub select device ? before or after BtnTestStart ?
    Select device is being called before the btnTestStart. Would possibly moving it to the form load event help this?

  13. #13
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    846

    Re: Init Problem? Serial Port Comm Program

    try to replace
    Code:
    Private mDevice As Ixxat.Vci3.IVciDevice = Nothing
    by
    Code:
    Private mDevice As new Ixxat.Vci3.IVciDevice = Nothing
    where do you declare that ?

    Unfortunately, as it is late in France, I won't be able to help you more and much tonight (for me ).
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  14. #14

    Thread Starter
    New Member
    Join Date
    Aug 2020
    Posts
    10

    Re: Init Problem? Serial Port Comm Program

    Quote Originally Posted by Delaney View Post
    try to replace
    Code:
    Private mDevice As Ixxat.Vci3.IVciDevice = Nothing
    by
    Code:
    Private mDevice As new Ixxat.Vci3.IVciDevice = Nothing
    where do you declare that ?

    Unfortunately, as it is late in France, I won't be able to help you more and much tonight (for me ).
    I understand. Thank you for the help. The declaration of mDevice is in the form load event as shown

    Code:
    Public Class Form1
        'variables to run/initialize can connection
        Private mDevice As Ixxat.Vci3.IVciDevice = Nothing
        Private mCanChn As Ixxat.Vci3.Bal.Can.ICanChannel = Nothing
        Private mReader As Ixxat.Vci3.Bal.Can.ICanMessageReader = Nothing
        Private mWriter As Ixxat.Vci3.Bal.Can.ICanMessageWriter = Nothing
        Private mCanCtl As Ixxat.Vci3.Bal.Can.ICanControl = Nothing
        'device list variables, working to remove
        Private deviceManager As Ixxat.Vci3.IVciDeviceManager = Nothing
        Private deviceList As Ixxat.Vci3.IVciDeviceList = Nothing
        Private deviceEnum As IEnumerator = Nothing
    When I change the code to
    Code:
    ]Private mDevice As new Ixxat.Vci3.IVciDevice = Nothing
    I get the error stating I cannot use "new" on an interface.

  15. #15
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    846

    Re: Init Problem? Serial Port Comm Program

    I must confess that I am lost.

    I manage to find an IXXAT sample vb.net program which is close to yours. and some manual. CAN bus is not something I know so I am afraid I won't be able to help you more than that. I don't have CAN device, so I cannot test it

    I would advice you to start with that sample program and try to make it works and understand how it works and what are the difference with yours.

    https://cdn.hms-networks.com/docs/li...sn=b3eb48d7_22

    the link contains a large zip file with several sample programs in several language + manuals + api etc...

    I found the link here : https://www.ixxat.com/technical-supp...olete-products
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  16. #16

    Thread Starter
    New Member
    Join Date
    Aug 2020
    Posts
    10

    Re: Init Problem? Serial Port Comm Program

    Quote Originally Posted by Delaney View Post
    I must confess that I am lost.

    I manage to find an IXXAT sample vb.net program which is close to yours. and some manual. CAN bus is not something I know so I am afraid I won't be able to help you more than that. I don't have CAN device, so I cannot test it

    I would advice you to start with that sample program and try to make it works and understand how it works and what are the difference with yours.

    https://cdn.hms-networks.com/docs/li...sn=b3eb48d7_22

    the link contains a large zip file with several sample programs in several language + manuals + api etc...

    I found the link here : https://www.ixxat.com/technical-supp...olete-products
    Thank you for the help, I will take a look at the sample files

  17. #17
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Init Problem? Serial Port Comm Program

    Quote Originally Posted by LostintheCode View Post
    I understand. Thank you for the help. The declaration of mDevice is in the form load event as shown

    Code:
    Public Class Form1
        'variables to run/initialize can connection
        Private mDevice As Ixxat.Vci3.IVciDevice = Nothing
        Private mCanChn As Ixxat.Vci3.Bal.Can.ICanChannel = Nothing
        Private mReader As Ixxat.Vci3.Bal.Can.ICanMessageReader = Nothing
        Private mWriter As Ixxat.Vci3.Bal.Can.ICanMessageWriter = Nothing
        Private mCanCtl As Ixxat.Vci3.Bal.Can.ICanControl = Nothing
        'device list variables, working to remove
        Private deviceManager As Ixxat.Vci3.IVciDeviceManager = Nothing
        Private deviceList As Ixxat.Vci3.IVciDeviceList = Nothing
        Private deviceEnum As IEnumerator = Nothing
    When I change the code to
    Code:
    ]Private mDevice As new Ixxat.Vci3.IVciDevice = Nothing
    I get the error stating I cannot use "new" on an interface.
    Correct, you can't create an instancce of an interface... you can create an object that implements that interface, but an interface by itself cannot be instanciated.

    ergo, if the mDevice isn't being created on the line it is declared on (also, I don't think you can New an instance and set it to nothing at the same time, because, what's the point? What's the intent? Do you want the object, or nothing? Can't have both.) ... it should be set somewhere else then (looks like inside SelectDevice).... which isn't happening, which may be due to the first error... What it sounds like is happening is that SelectDevice is failing to find the device, and as a result mDevice doesn't get set, and there's other code further downstream that's called that IS using mDevice with out first checking to see if it is not nothing... but since it is... error.

    If it were me, I'd put a breakpoint on the first line of code in SelectDevice and then step through it, one line at a time, checking the value of everything, and seeing what it is really doing in there, and then working backwards to why it's not working as expected.
    ie: it's not entering the loop because deviceEnum is empty... ok, so why is deviceEnum empty? Well, it's because deviceList is empty.... ok, why is deviceList empty then? Well, it's because ........ and so on...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  18. #18

    Thread Starter
    New Member
    Join Date
    Aug 2020
    Posts
    10

    Re: Init Problem? Serial Port Comm Program

    Quote Originally Posted by techgnome View Post
    Correct, you can't create an instancce of an interface... you can create an object that implements that interface, but an interface by itself cannot be instanciated.

    ergo, if the mDevice isn't being created on the line it is declared on (also, I don't think you can New an instance and set it to nothing at the same time, because, what's the point? What's the intent? Do you want the object, or nothing? Can't have both.) ... it should be set somewhere else then (looks like inside SelectDevice).... which isn't happening, which may be due to the first error... What it sounds like is happening is that SelectDevice is failing to find the device, and as a result mDevice doesn't get set, and there's other code further downstream that's called that IS using mDevice with out first checking to see if it is not nothing... but since it is... error.

    If it were me, I'd put a breakpoint on the first line of code in SelectDevice and then step through it, one line at a time, checking the value of everything, and seeing what it is really doing in there, and then working backwards to why it's not working as expected.
    ie: it's not entering the loop because deviceEnum is empty... ok, so why is deviceEnum empty? Well, it's because deviceList is empty.... ok, why is deviceList empty then? Well, it's because ........ and so on...

    -tg
    Tg,

    That's a pretty solid idea. I know that deconstruction is a viable option, it's just so hard sometimes when you're stuck in your head on a problem. So I'm starting at the beginning of the updated button click event here
    Code:
     Dim CurListViewItem As ListViewItem
    
            CurListViewItem = ListViewAvailInterfaces.SelectedItems(0)
            'disable get status timer
            TimerGetStatus.Enabled = False
            'if controller is already open, close existing before opening new one
            CloseCurrentExistingController()
            'get tag for currently selected item
            If CurListViewItem IsNot Nothing Then
                'MessageBox.Show(CurListViewItem.Tag.ToString())
                SelectDevice(CurListViewItem.Tag)
                InitSocket(0)
                'disable initialize button after it has been clicked
                ButtonInit.Enabled = False
                'enable getstatus timer again
                TimerGetStatus.Enabled = True
            End If
    
            'open a com port for use in sending serial commands to test card
            Try
                ' Get the selected COM port's name from the combo box.
                If Not myComPort.IsOpen Then
                    myComPort.PortName = "COM2"
                    ' Get the selected bit rate from the combo box.
                    myComPort.BaudRate = 19200
                    ' Set other port parameters.
                    myComPort.Parity = Parity.None
                    myComPort.DataBits = 8
                    myComPort.StopBits = StopBits.One
                    myComPort.Handshake = Handshake.None
                    myComPort.ReadTimeout = 3000
                    myComPort.WriteTimeout = 5000
                    myComPort.ReadTimeout = 1000
                    myComPort.WriteTimeout = 1000
                    ' Open the port.
                    myComPort.Open()
                End If
            Catch ex As InvalidOperationException
                MessageBox.Show(ex.Message)
            Catch ex As UnauthorizedAccessException
                MessageBox.Show(ex.Message)
            Catch ex As System.IO.IOException
                MessageBox.Show(ex.Message)
            End Try
    
    
            canMessage.Identifier = &H10A
            canMessage.DataLength = 8
            canMessage.Data(0) = "&H24"
            canMessage.Data(1) = "&H21"
            canMessage.Data(2) = "&H1E"
            canMessage.Data(3) = "&H1C"
            canMessage.Data(4) = "&H1A"
            canMessage.Data(5) = "&H18"
            canMessage.Data(6) = "&H16"
            canMessage.Data(7) = "&H14"
    
            If mWriter.Capacity > 0 Then
                mWriter.SendMessage(canMessage)
            End If
    
            System.Threading.Thread.Sleep(500)
    
            MessageBox.Show("OK")
    
            'While comTemp.Length < 1
            '    Try
            '        comTemp = myComPort.ReadLine()
            '    Catch ex As Exception
    
            '    End Try
            'End While
    
            'comTemp = ""
    
    
    
    
            canMessage.Identifier = &H10A
            canMessage.DataLength = 8
            canMessage.Data(0) = "&H00"
            canMessage.Data(1) = "&H11"
            canMessage.Data(2) = "&HF"
            canMessage.Data(3) = "&HD"
            canMessage.Data(4) = "&H7"
            canMessage.Data(5) = "&H5"
            canMessage.Data(6) = "&H26"
            canMessage.Data(7) = "&H25"
    
            If mWriter.Capacity > 0 Then
                mWriter.SendMessage(canMessage)
            End If
    
            System.Threading.Thread.Sleep(50)
    
            myComPort.Write("OK")
    
    
    
            canMessage.Identifier = &H10A
            canMessage.DataLength = 8
            canMessage.Data(0) = "&H00"
            canMessage.Data(1) = "&H2"
            canMessage.Data(2) = "&H23"
            canMessage.Data(3) = "&H22"
            canMessage.Data(4) = "&H20"
            canMessage.Data(5) = "&H1D"
            canMessage.Data(6) = "&H1B"
            canMessage.Data(7) = "&H19"
    
            If mWriter.Capacity > 0 Then
                mWriter.SendMessage(canMessage)
            End If
    
            System.Threading.Thread.Sleep(50)
    
            myComPort.Write("OK")
    
    
    
            canMessage.Identifier = &H10A
            canMessage.DataLength = 8
            canMessage.Data(0) = "&H00"
            canMessage.Data(1) = "&H15"
            canMessage.Data(2) = "&H13"
            canMessage.Data(3) = "&H12"
            canMessage.Data(4) = "&H10"
            canMessage.Data(5) = "&HE"
            canMessage.Data(6) = "&HC"
            canMessage.Data(7) = "&H8"
    
            If mWriter.Capacity > 0 Then
                mWriter.SendMessage(canMessage)
            End If
    
            System.Threading.Thread.Sleep(50)
    
            myComPort.Write("OK")
    
    
    
            canMessage.Identifier = &H10A
            canMessage.DataLength = 8
            canMessage.Data(0) = "&H00"
            canMessage.Data(1) = "&H4"
            canMessage.Data(2) = "&H27"
            canMessage.Data(3) = "&H1F"
            canMessage.Data(4) = "&H17"
    
    
            If mWriter.Capacity > 0 Then
                mWriter.SendMessage(canMessage)
            End If
    
            System.Threading.Thread.Sleep(50)
    
            myComPort.Write("OK")
    
          
        End Sub
    and I'm looking at the lines
    Code:
    Dim CurListViewItem As ListViewItem
    
            CurListViewItem = ListViewAvailInterfaces.SelectedItems(0)
    for
    Code:
    CurListViewItem = ListViewAvailInterfaces.SelectedItems(0)
    The sub is
    Code:
        Private Sub ListViewAvailInterfaces_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListViewAvailInterfaces.SelectedIndexChanged
            ButtonInit.Enabled = True
            ShowDeviceHardwareID()
        End Sub
    which leads me to

    Code:
     Private Sub ShowDeviceHardwareID()
            Dim CurListViewItem As ListViewItem
            Dim selIndex As Windows.Forms.ListView.SelectedIndexCollection
            selIndex = ListViewAvailInterfaces.SelectedIndices()
            If selIndex.Count > 0 Then
                CurListViewItem = ListViewAvailInterfaces.SelectedItems(0)
                If CurListViewItem IsNot Nothing Then
                    SelectDevice(CurListViewItem.Tag)
                End If
            End If
        End Sub
    My question right off the bat is, looking at the code map that I sent above, isn't the Dim of CurLustViewItem in both places unneccessary, and also, I feel like there is an issue within the nested If statements. Like I posted originally, I am inheriting the code in this situation, and can't really speak on why it was constructed the way it was.

    Thanks for any help with this.
    M

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width