Click here to Skip to main content
15,889,867 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,
Maybe somebody could help me out. I have an application with a USB connection with a lawicel CANUSB converter. Everything works OK, but slowly my apllication is running slow or even freeze.
I know that maybe using a second thread would help. I see a lot of examples for a serial port, but unfortunate nothing for a USB device with a direct library.

My application is running a refresh of the CAN data every 100 ms. about 40 different CANid's are loaded and will be processed within the application. At the moment the refresh and load of the updated CANids is done through a timer.tick.

Is there a better solution to get it working without slowing down my computer after an hour?

What I have tried:

Private Sub CANframes_Tick(sender As System.Object, e As System.EventArgs) Handles CANframes.Tick
        Dim retval As Short
        Dim msg As Lawicel.CANUSB.CANMsg
        Dim buffer As String = ""
        If (bOpen) Then
            retval = Lawicel.CANUSB.canusb_Read(h, msg)
            While (retval = Lawicel.CANUSB.ERROR_CANUSB_OK)
                If msg.id = 522 Then
                    byteres = BitConverter.GetBytes(msg.data)
                End If
                If msg.id = 420 Then

                    Readbyte1 = BitConverter.GetBytes(msg.data) 
                End If
                If msg.id = 421 Then
                    Readbyte2 = BitConverter.GetBytes(msg.data) 
                End If
                If msg.id = 422 Then
                    Readbyte3 = BitConverter.GetBytes(msg.data) 
                End If
                If msg.id = 423 Then
                    Readbyte4 = BitConverter.GetBytes(msg.data)
                End If
                If msg.id = 450 Then
                    Readbyte11 = BitConverter.GetBytes(msg.data)
                End If
                If msg.id = 460 Then
                    Readbyte12 = BitConverter.GetBytes(msg.data)
                End If
                If msg.id = 461 Then
                    Readbyte13 = BitConverter.GetBytes(msg.data)
                End If
                If msg.id = 462 Then
                    Readbyte14 = BitConverter.GetBytes(msg.data)
                End If
                If msg.id = 463 Then
                    Readbyte15 = BitConverter.GetBytes(msg.data)
                End If
                If msg.id = 150 Then
                    readvar7 = BitConverter.GetBytes(msg.data)
                End If
                If msg.id = 151 Then
                    readvar8 = BitConverter.GetBytes(msg.data)
                End If
                If msg.id = 152 Then
                    readvar9 = BitConverter.GetBytes(msg.data)
                End If
                If msg.id = 153 Then
                    readvar10 = BitConverter.GetBytes(msg.data)
                End If
                If msg.id = 154 Then
                    readvar11 = BitConverter.GetBytes(msg.data)
                End If
                If msg.id = 162 Then
                    readvar1 = BitConverter.GetBytes(msg.data)
                End If
                If msg.id = 164 Then
                    readvar2 = BitConverter.GetBytes(msg.data)
                End If
                If msg.id = 165 Then
                    readvar3 = BitConverter.GetBytes(msg.data)
                End If
                If msg.id = 166 Then
                    readvar4 = BitConverter.GetBytes(msg.data)
                End If
                If msg.id = 167 Then
                    readvar5 = BitConverter.GetBytes(msg.data)
                End If
                If msg.id = 168 Then
                    readvar6 = BitConverter.GetBytes(msg.data)
                End If
                If msg.id = 177 Then
                    readvar16 = BitConverter.GetBytes(msg.data)
                End If
                If msg.id = 202 Then
                    readvar12 = BitConverter.GetBytes(msg.data)
                End If
                If msg.id = 216 Then
                    readvar13 = BitConverter.GetBytes(msg.data)
                End If
                If msg.id = 217 Then
                    readvar14 = BitConverter.GetBytes(msg.data)
                End If
                If msg.id = 218 Then
                    readvar15 = BitConverter.GetBytes(msg.data)
                End If
                If msg.id = 223 Then
                    readvar17 = BitConverter.GetBytes(msg.data)
                End If
                If msg.id = 224 Then
                    readvar18 = BitConverter.GetBytes(msg.data)
                End If
                If msg.id = 773 Then
                    readwatchdog = BitConverter.GetBytes(msg.data)
                End If
                retval = Lawicel.CANUSB.canusb_Read(h, msg)
            End While
         
        End If
        If UpdateTimer.Enabled = False Then
            UpdateTimer.Enabled = True
        End If
   
    End Sub

Every CANID is set into its own byte array of 8 bytes. (like readvar1)
The updateTimer which is enabled at the bottom is for updating the dashboard of the form.
Posted
Updated 28-Sep-18 4:44am
Comments
MadMyche 28-Sep-18 11:05am    
Have you ran this in debug while watching the memory? What happens if lower the refresh rate?
Jaroslav Mitrovic 28-Sep-18 20:45pm    
Hi.

I could not see any harmfull Stuff...

But maybe The two calls of "retval = Lawicel.CANUSB.canusb_Read(h, msg)" inside the loop are too much. I would try to get rid of the first Call (if Time critical then the last Call). Ahh I missed it... got it now. You are using the First Call to store the "retval" for the While condition, but you later on recall it inside the WhileLoop (at the End), try to get rid of the Last Call of "retval = Lawicel.CANUSB.canusb_Read(h, msg)".

c.u. Zui from Hamburg
Henkjan 2 8-Oct-18 8:56am    
Hi,

i will give that a try, but this was the way the supplier built his own CAN read/write demo. Thanks for the suggestion.
Greetz Henkjan
Jaroslav Mitrovic 11-Oct-18 3:28am    
Hi,

maybe then there is a reason for the Supplier to place it in his Demo...
It is possible that the Value inside the Variable (I assume "msg") can be changed meanwhile.
But anyway the Messages of type "OK" are the only ones that are Processed, so why then Read it in again? If You get wrong Results, or out of order maybe You are forced to read the Value inside the Loop also.
If that is the Case You need a Caching strategy for the Values (?) if this is a necassity Try a FiFo LiFo or some of these Fancy Named Stack´s or Queue´s ;) .
But be aware FiFi sound cute but she is really Big (doberman´s usally are...).

O.K. Funny Bone asside...

I would Use a Select-Case for the If-Statements.

And also Refactor it inside a "outsourced" Function.

Maybe You can "Group" the Outputs with something like
Case msg.id >= 420 AndAlso msg.id <= 463
'Do something about the ByteValues

etc.

Good Luck and Happy Coding

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900