Results 1 to 4 of 4

Thread: [RESOLVED] Comm port and API

Threaded View

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2006
    Posts
    5

    Resolved [RESOLVED] Comm port and API

    I am developing an application to use the serial port using API calls. I have been able to transmit corectly and control de RTS signal at my desire (need it to implement a rs-323 to rs-485 convertion), but I have not ben able to receive any bytes. I have chekd the signal at the RX pin in the PC and it is OK. The hardware wotks fine with a previous app that used the VB6 comm object. Can anyone help me out? I beleive is something to do with the configuraion, here is what I am doing:

    VB Code:
    1. Dim D As DCB, cto As COMMTIMEOUTS
    2.     Dim DCBString As String
    3.    
    4.     Dim lRet&
    5.     Dim Status As Long
    6.  
    7.     SetupPort = False
    8.     Erm = ""
    9.  
    10.     D.DCBlength = Len(D)
    11.  
    12.     If GetCommState(cmn.hFile, D) = 0 Then
    13.         Erm = "Error Getting " + cmn.Port + " parameters"
    14.         Exit Function
    15.     End If
    16.  
    17. ' ---
    18.     'Parámetros del puerto
    19.     'd.DCBlength = Len(d)
    20.     D.BaudRate = CBR_9600                   'Velocidad
    21.     D.fBinary = 1                           'Binary Transfers ALWAYS TRUE
    22.     D.fParity = 1                           'Verificar paridad
    23.     'D.fOutxCtsFlow = 1                      'Hardware Control, monitor CTS
    24.     D.fOutxDsrFlow = 0                      'Hardware Control, controlar DSR
    25.     D.fDtrControl = DTR_CONTROL_DISABLE     'Hardware Control, DTR flow control
    26.     D.fDsrSensitivity = 0                   'Hardware Control, monitor DSR
    27.     'D.fTXContinueOnXoff = 0                 'XOn/XOff Control, manejo del buffer
    28.     'D.fOutX = 0                             'XOn/XOff Control, transmisión
    29.     'D.fInX = 0                              'XOn/XOff Control, recepción
    30.     'D.fErrorChar = 0                        'Replace Error Chars
    31.     'D.fNull = 0                             'Discard Null bytes
    32.     D.fRtsControl = RTS_CONTROL_DISABLE     'Hardware Control, control RTS
    33.     'D.fAbortOnError = 0                     'Control de Errores
    34.     'D.fDummy2 = 0                           'RESERVADO, no usar
    35.     'D.wReserved = 0                         'RESERVADO, debe ser o
    36.     'D.XonLim = 0                            'Bytes minimo para activar control de flujo
    37.     'D.XoffLim = 0                           'Bytes máxim para activar control de flujo
    38.     D.ByteSize = 8                          'Cantidad de bits
    39.     D.Parity = NOPARITY                     'Paridad (solo si fParity)
    40.     D.StopBits = ONESTOPBIT                 'Bits de parada
    41.     'D.XonChar = 255                         'Caracter XOff
    42.     'D.XoffChar = 254                        'Caracter XOn
    43.     'D.ErrorChar = 0                        'Caracter de reemplazo en error
    44.     'D.EofChar = 0                          'Caracter de fin
    45.     'D.EvtChar = 0                           'Caracter de evento
    46.    
    47.     'Crear la estructura
    48.     'DCBString = "COM1: baud=9600 parity=N data=8 stop=1" ' to=off xon=off odsr=off octs=off dtr=off rts=off idsr=off"
    49.     'lRet = BuildCommDCB(DCBString, D)
    50.     'If lRet = 0 Then
    51.     '    'No se pudo crear la estructura
    52.     '    Erm = "Error Setting Com State"
    53.     '    Exit Function
    54.     'End If
    55.  
    56.  
    57.  
    58.     If SetCommState(cmn.hFile, D) = 0 Then
    59.         'Error al cambiar el estado del puerto
    60.         Erm = "Error setting " + cmn.Port + " parameters"
    61.         Debug.Print Err.Description
    62.         Exit Function
    63.     End If
    64.  
    65.  
    66.     ' --- In Out Buffer sizes
    67.     If SetupComm(cmn.hFile, 256, 256) = 0 Then
    68.         'No se pudieron cnfigurar los buffers
    69.         Erm = "Error on API SetupComm()"
    70.         Exit Function
    71.     End If
    72.  
    73.     ' Setup Timeouts in Milliseconds
    74.     cto.ReadIntervalTimeout = 100
    75.     cto.ReadTotalTimeoutMultiplier = 100    
    76.     cto.ReadTotalTimeoutConstant = 80      
    77.    
    78.     cto.WriteTotalTimeoutMultiplier = 100
    79.     cto.WriteTotalTimeoutConstant = 10
    80.  
    81.     If SetCommTimeouts(cmn.hFile, cto) = 0 Then
    82.         Erm = "Error setting " + cmn.Port + " timeouts"
    83.         Exit Function
    84.     End If
    85.    
    86.     Call PurgeComm(cmn.hFile, PURGE_RXCLEAR)
    87.     Call PurgeComm(cmn.hFile, PURGE_TXCLEAR)
    88.     Call PurgeComm(cmn.hFile, PURGE_RXABORT)
    89.     Call PurgeComm(cmn.hFile, PURGE_TXABORT)
    90.  
    91.     If GetCommModemStatus(cmn.hFile, Status) = 0 Then
    92.         Erm = "Failed GetCommModemStatus()"
    93.         Exit Function
    94.     End If
    95.  
    96.     ' --- Good Exit
    97.     SetupPort = True

    The following code is used to read data form the port
    VB Code:
    1. ContBytes = 0
    2.     'Recibir solo por un tiempo
    3.     PortTmr.StartTimer
    4.     Do While PortTmr.ElapsedTime < TimeOut
    5.         'Esperar a que lleguen datos al buffer
    6.         res = SetCommMask(cmn.hFile, EV_TXEMPTY Or EV_RXCHAR)
    7.         CommEvent = 0
    8.         res = WaitCommEvent(cmn.hFile, CommEvent, Null)
    9.         Do
    10.             res = ReadFile(cmn.hFile, DataTemp, 1, BytesRead, OverRes)
    11.             If res = 0 Then
    12.                 Debug.Print Err.Number
    13.                 Debug.Print Error(Err.Number)
    14.                 Debug.Print Err.LastDllError
    15.                 Debug.Print Error(Err.LastDllError)
    16.             End If
    17.         Loop While BytesRead > 0
    18.         ContBytes = ContBytes + BytesRead
    19.         Data = Data & DataTemp
    20.         If ContBytes = NumBytes Then
    21.             RxData = True
    22.             Exit Do
    23.         End If
    24.     Loop

    The waitcommevent function returns and the event tells me there was a byte received. But when I read de port I get an error. The erros's message is "Error defined by the application or object". So I can not tell what is failing.

    Any ideas will be appreciated
    thanks!
    Last edited by Keita; Aug 29th, 2006 at 02:27 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width