[VB.net 1.1] Accesso a porte USB

martedì 23 giugno 2009 - 10.50

qeiciccio Profilo | Newbie

Salve, mi chiamo Francesco ed è la prima volta che scrivo su questo forum.
Sto gestendo un applicativo che interagisce con HW esterno e che in prima battuta utilizzava un interfaccia RS232 e successivamente tramite una connessione USB.
Il passaggio da un interfaccia hw all'altra non è stata del tutto indolore ma gli statment che utilizzo per aprire la porta sono praticamente gli stessi.
Attualmente l'unica difficoltà che incontro è legata alla impossibilità di indirizzare porte con nome superiore al com9 e non ne capisco il motivo.
L'ambito tecnologico è quello del framework 1.1 con Visual studio 2003 ed il linguaggio di riferimento è VB.net
Per completezza qui di seguito riporto il metodo che utilizzo per aprire la connessione verso la porta i-esima.
Ci tengo a sottolineare che l'istruzione che va in errore è la chiamata relativa alla CreateFile.

C'è Qualcuno tra voi che si è già scontrato con problemi simili?

Saluti
Francesco

codice:
Public Overloads Sub Open() ' Get Dcb block,Update with current data Dim uDcb As DCB, iRc As Integer ' Set working mode Dim iMode As Integer = Convert.ToInt32(IIf(meMode = Mode.Overlapped, _ FILE_FLAG_OVERLAPPED, 0)) ' Initializes Com Port If miPort > 0 Then Try ' Creates a COM Port stream handle mhRS = CreateFile("COM" & miPort.ToString, _ GENERIC_READ Or GENERIC_WRITE, 0, 0, _ OPEN_EXISTING, iMode, 0) If mhRS <> -1 Then ' Clear all comunication errors Dim lpErrCode As Integer iRc = ClearCommError(mhRS, lpErrCode, 0&) ' Clears I/O buffers iRc = PurgeComm(mhRS, PurgeBuffers.RXClear Or _ PurgeBuffers.TxClear) ' Gets COM Settings iRc = GetCommState(mhRS, uDcb) ' Updates COM Settings Dim sParity As String = "NOEM" sParity = sParity.Substring(meParity, 1) ' Set DCB State Dim sDCBState As String = String.Format( _ "baud={0} parity={1} data={2} stop={3}", _ miBaudRate, sParity, miDataBit, CInt(meStopBit)) iRc = BuildCommDCB(sDCBState, uDcb) 'CODAN 17/05/05 If (uDcb.Bits1 <> 12305) Then uDcb.Bits1 = 12305 End If 'FINE iRc = SetCommState(mhRS, uDcb) If iRc = 0 Then Dim sErrTxt As String = pErr2Text(GetLastError()) Throw New CIOChannelException( _ "Unable to set COM state0" & sErrTxt) End If ' Setup Buffers (Rx,Tx) iRc = SetupComm(mhRS, miBufferSize, miBufferSize) ' Set Timeouts pSetTimeout() Else ' Raise Initialization problems Throw New CIOChannelException( _ "Unable to open COM" & miPort.ToString) End If Catch Ex As Exception ' Generica error Throw New CIOChannelException(Ex.Message, Ex) End Try Else ' Port not defined, cannot open Throw New ApplicationException("COM Port not defined, " + _ "use Port property to set it before invoking InitPort") End If End Sub

Jeremy Profilo | Guru

Ciao Francesco.
Dai un occhio qui:
http://support.microsoft.com/kb/115831

Facci sapere...
Ciao

Jeremy Profilo | Guru

E qui:
http://msdn.microsoft.com/en-us/library/aa365247(VS.85).aspx

ho trovato questo:
For example, if you want to open the system's serial communications port 1, you can use either "\\.\COM1" or "COM1" in the call to the CreateFile function. This works because COM1-COM9 are part of the reserved names in the NT file namespace as previously mentioned. But if you have a 100 port serial expansion board and want to open COM56, you need to open it using "\\.\COM56". This works because "\\.\" goes to the device namespace, and there is no predefined NT namespace for COM56. Another example of this is using the CreateFile function on "\\.\PhysicalDiskX" or "\\.\CdRom1" allow you to access those devices, bypassing the file system. It just happens that the device driver that implements the name "C:\" has its own namespace that is the file system. APIs that go through the CreateFile function should work because CreateFile is the same API to open files and devices. If you're working with Win32 functions, you should use only "\\.\" to access devices and not files.

Facci sapere...
Ciao

qeiciccio Profilo | Newbie

Ok problema risolto utilizzando semplicemente le indicazioni fornite negli articoli indicati da Jeremy.
Grazie a tutti
Francesco
Partecipa anche tu! Registrati!
Hai bisogno di aiuto ?
Perchè non ti registri subito?

Dopo esserti registrato potrai chiedere
aiuto sul nostro Forum oppure aiutare gli altri

Consulta le Stanze disponibili.

Registrati ora !
Copyright © dotNetHell.it 2002-2024
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5