Dim D As DCB, cto As COMMTIMEOUTS
Dim DCBString As String
Dim lRet&
Dim Status As Long
SetupPort = False
Erm = ""
D.DCBlength = Len(D)
If GetCommState(cmn.hFile, D) = 0 Then
Erm = "Error Getting " + cmn.Port + " parameters"
Exit Function
End If
' ---
'Parámetros del puerto
'd.DCBlength = Len(d)
D.BaudRate = CBR_9600 'Velocidad
D.fBinary = 1 'Binary Transfers ALWAYS TRUE
D.fParity = 1 'Verificar paridad
'D.fOutxCtsFlow = 1 'Hardware Control, monitor CTS
D.fOutxDsrFlow = 0 'Hardware Control, controlar DSR
D.fDtrControl = DTR_CONTROL_DISABLE 'Hardware Control, DTR flow control
D.fDsrSensitivity = 0 'Hardware Control, monitor DSR
'D.fTXContinueOnXoff = 0 'XOn/XOff Control, manejo del buffer
'D.fOutX = 0 'XOn/XOff Control, transmisión
'D.fInX = 0 'XOn/XOff Control, recepción
'D.fErrorChar = 0 'Replace Error Chars
'D.fNull = 0 'Discard Null bytes
D.fRtsControl = RTS_CONTROL_DISABLE 'Hardware Control, control RTS
'D.fAbortOnError = 0 'Control de Errores
'D.fDummy2 = 0 'RESERVADO, no usar
'D.wReserved = 0 'RESERVADO, debe ser o
'D.XonLim = 0 'Bytes minimo para activar control de flujo
'D.XoffLim = 0 'Bytes máxim para activar control de flujo
D.ByteSize = 8 'Cantidad de bits
D.Parity = NOPARITY 'Paridad (solo si fParity)
D.StopBits = ONESTOPBIT 'Bits de parada
'D.XonChar = 255 'Caracter XOff
'D.XoffChar = 254 'Caracter XOn
'D.ErrorChar = 0 'Caracter de reemplazo en error
'D.EofChar = 0 'Caracter de fin
'D.EvtChar = 0 'Caracter de evento
'Crear la estructura
'DCBString = "COM1: baud=9600 parity=N data=8 stop=1" ' to=off xon=off odsr=off octs=off dtr=off rts=off idsr=off"
'lRet = BuildCommDCB(DCBString, D)
'If lRet = 0 Then
' 'No se pudo crear la estructura
' Erm = "Error Setting Com State"
' Exit Function
'End If
If SetCommState(cmn.hFile, D) = 0 Then
'Error al cambiar el estado del puerto
Erm = "Error setting " + cmn.Port + " parameters"
Debug.Print Err.Description
Exit Function
End If
' --- In Out Buffer sizes
If SetupComm(cmn.hFile, 256, 256) = 0 Then
'No se pudieron cnfigurar los buffers
Erm = "Error on API SetupComm()"
Exit Function
End If
' Setup Timeouts in Milliseconds
cto.ReadIntervalTimeout = 100
cto.ReadTotalTimeoutMultiplier = 100
cto.ReadTotalTimeoutConstant = 80
cto.WriteTotalTimeoutMultiplier = 100
cto.WriteTotalTimeoutConstant = 10
If SetCommTimeouts(cmn.hFile, cto) = 0 Then
Erm = "Error setting " + cmn.Port + " timeouts"
Exit Function
End If
Call PurgeComm(cmn.hFile, PURGE_RXCLEAR)
Call PurgeComm(cmn.hFile, PURGE_TXCLEAR)
Call PurgeComm(cmn.hFile, PURGE_RXABORT)
Call PurgeComm(cmn.hFile, PURGE_TXABORT)
If GetCommModemStatus(cmn.hFile, Status) = 0 Then
Erm = "Failed GetCommModemStatus()"
Exit Function
End If
' --- Good Exit
SetupPort = True