Hello,
I'm trying to acces the Canport using VB.net and I allready looked at your examples for C and the DIO for VB.NET, so I was pretty sure that I could pull it of... I was wrong. Here's my code so far:
Code
- Imports System.Runtime.InteropServices
- Imports System.Threading.Thread
- '////////////////////////////////////////////////////////////////////////////
- Module Module1
- Sub Main()
- Dim CAN As New CANPort
- Dim Cycle As UInteger = 0
- CAN.CANport("CID1:")
- Dim SendData As String = "Hallo"
- CAN.Init()
- CAN.WriteValue(SendData)
- System.Threading.Thread.Sleep(1000)
- CAN.Close()
- End Sub
- End Module
- Class CANPort
- ' File access
- Const INVALID_HANDLE_VALUE As Integer = -1
- Const OPEN_EXISTING As Integer = 3
- Const FILE_BEGIN As Integer = 0
- Protected hPort As IntPtr = CType(INVALID_HANDLE_VALUE, IntPtr) 'Create File Handle
- ' Function to create/open port and get handle
- Public Sub CANport(ByVal Filename As String)
- Dim Access As UInt32 = 3221225472 '&HC0000000 (read/write)
- hPort = CECreateFileW(Filename, Access, 0, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero)
- If hPort = INVALID_HANDLE_VALUE Then
- Console.Write("WinMain() CreateFile Failed")
- Else
- Console.Write("Canport open\n")
- End If
- End Sub
- ' Function to get data from port
- Public Sub ReadValue(ByVal Data As String)
- Dim NumberOfBytesToRead As UInteger = 2
- Dim NumberOfBytesRead As UInteger = 0
- Dim value As Integer
- value = CEReadFile(hPort, Data, NumberOfBytesToRead, NumberOfBytesRead, IntPtr.Zero)
- If value = 0 Or NumberOfBytesRead = 0 Then Console.Write("ReadFile Failed")
- End Sub
- ' Function to write data to port
- Public Sub WriteValue(ByVal Data As String)
- Dim NumberOfBytesWritten As UInteger = 0
- Dim value As Integer
- value = CEWriteFile(hPort, Data, Len(Data), NumberOfBytesWritten, IntPtr.Zero)
- If value = 0 Or NumberOfBytesWritten = 0 Then Console.Write("Write File Failed")
- End Sub
- Public Sub Init()
- 'zet de baudrate op 250000
- Dim baudrate As String = "250000"
- Dim ReturnedBytes As UInteger
- '8207C000 because : ctl_code(file_device_can, 0x803, method_buffered, file_any_acces)
- '= Ctl_code(0x00008007, 0x803, 0, 0)
- '= (0x00008007) << 16) | ((0x803) << 14) | ((0) << 2) | (0)
- ' = 8207C000
- If CEDeviceIoControl(hPort, &H8207C000, IntPtr.Zero, 0, baudrate, Len(baudrate), ReturnedBytes, IntPtr.Zero) Then
- Console.WriteLine("Baudrate changed")
- Else
- Console.WriteLine()
- Console.WriteLine(ReturnedBytes)
- Console.WriteLine("Baudrate change failed")
- End If
- End Sub
- Sub Close()
- Dim value As Integer
- value = CECloseHandle(hPort)
- If value = 0 Then Console.Write("Close Failed")
- End Sub
- <DllImport("coredll.dll", EntryPoint:="CreateFileW", SetLastError:=True)> _
- Public Shared Function CECreateFileW( _
- ByVal lpFileName As String, _
- ByVal dwDesiredAccess As UInt32, _
- ByVal dwShareMode As UInteger, _
- ByVal pSecurityAttributes As IntPtr, _
- ByVal dwCreationDisposition As UInteger, _
- ByVal dwFlagsAndAttributes As UInteger, _
- ByVal hTemplatefile As IntPtr) As IntPtr
- ' Leave the body of the function empty.
- End Function
- ' CloseHandle()
- <DllImport("coredll.dll", EntryPoint:="CloseHandle", SetLastError:=True)> _
- Public Shared Function CECloseHandle( _
- ByVal hObject As IntPtr) As Integer
- ' Leave the body of the function empty.
- End Function
- 'Read File()
- <DllImport("coredll.dll", EntryPoint:="ReadFile", SetLastError:=True)> _
- Public Shared Function CEReadFile( _
- ByVal hObject As IntPtr, _
- ByVal lpBuffer As String, _
- ByVal NumberOfBytesToRead As UInteger, _
- ByRef lpNumberOfBytesRead As UInteger, _
- ByVal lpOverLapped As IntPtr) As Integer
- 'Leave the body of the function empty.
- End Function
- 'write File()
- <DllImport("coredll.dll", EntryPoint:="WriteFile", SetLastError:=True)> _
- Public Shared Function CEWriteFile( _
- ByVal hObject As IntPtr, _
- ByVal lpBuffer As String, _
- ByVal NumberOfBytesToWrite As UInteger, _
- ByRef lpNumberOfBytesWritten As UInteger, _
- ByVal lpOverLapped As IntPtr) As Integer
- 'Leave the body of the function empty.
- End Function
- 'deviceio
- <DllImport("coredll.dll", EntryPoint:="DeviceIoControl", SetLastError:=True)> _
- Public Shared Function CEDeviceIoControl( _
- ByVal hObject As IntPtr, _
- ByVal dwIoControlCode As Integer, _
- ByVal lpInBuffer As String, _
- ByVal nInBufferSize As UInteger, _
- ByVal lpOutBuffer As String, _
- ByVal nOutBufferSize As UInteger, _
- ByVal lpBytesReturned As UInteger, _
- ByVal lpOverLapped As IntPtr) As Integer
- 'Leave the body of the function empty.
- End Function
- End Class
The program runs, and doesn't give an error when opening the port or trying to write to it. But I can't change any settings and when I check the CAN-port with a scope (while writing), I can't see any data.
Any ideas?
Regards,
Niek