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