Announcement

Collapse
No announcement yet.

Serial Communication

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Serial Communication

    I was messing around with serial communication today, I am
    having a problem. I used the code below to open COM1, then I
    connected on the other end with Hyper-Terminal. The program
    below will not read anything I am typing on the other end. To
    find out if it was the program, I tried using Hyper-Terminal on
    both ends of the connection and it worked just fine... That
    means it has to be the program, right!? What am I doing wrong:

    $INCLUDE "C:\VTECH\INCS\WIN32API.INC"
    GLOBAL Text AS ASCIIZ * 10000
    GLOBAL BufferLength AS LONG
    GLOBAL BytesRead AS LONG
    GLOBAL ComEvent AS LONG
    GLOBAL ComHandle AS LONG
    FUNCTION OpenCom AS LONG
    LOCAL TimeOuts AS CommTimeOuts
    ComHandle = CreateFile("COM1", _
    %GENERIC_READ OR %GENERIC_WRITE, _
    BYVAL %NULL, _
    BYVAL %NULL, _
    %OPEN_EXISTING, _
    BYVAL %NULL, _
    BYVAL %NULL)
    IF ComHandle = %INVALID_HANDLE_VALUE THEN STDOUT "Could not open COM1."
    TimeOuts.ReadIntervalTimeout = 50
    TimeOuts.ReadTotalTimeoutConstant = 300
    i% = SetCommTimeouts(ComHandle, TimeOuts)
    i% = PurgeComm(ComHandle, %PURGE_TXCLEAR)
    i% = PurgeComm(ComHandle, %PURGE_RXCLEAR)
    i% = SetCommMask(ComHandle, %EV_RXCHAR)
    END FUNCTION

    FUNCTION PBMAIN AS LONG
    OpenCom
    BufferLength = 8000
    DO WHILE INKEY$ = ""
    IF ReadFile(ComHandle, Text, BufferLength, BytesRead, BYVAL %NULL) THEN
    IF LEN(Text) > 0 THEN
    Text = LEFT$(Text, BytesRead)
    STDOUT Text;
    END IF
    END IF
    LOOP
    CloseHandle ComHandle
    END FUNCTION

    ------------------

  • #2
    I've found a piece of working code. Will receive serial data and print it on screen on receiving CR
    It uses waitcommevent to (almost) eliminate overhead.

    Code:
    #Include "win32api.inc"
    
    Function RxComm (ByVal pComm As Long) As Long
      Local lRet As Long, lComm() As Long, lDCB As DCB, lCTO As COMMTIMEOUTS
      Local lOVL As OVERLAPPED, lEvtMsk As Long, lBuff As String, lByteBuff As String * 1
      Local lBytes As Long, lCO As Long
      Dim lComm(2)
      On Error Resume Next                                                                          'Error skip
      lComm(pComm) = CreateFile ("COM" + Format$(pComm,"0"), _
                                 %GENERIC_READ Or %GENERIC_WRITE, _
                                 0, _
                                 ByVal %NULL, _
                                 %OPEN_EXISTING, _
                                 0, _
                                 ByVal %NULL)
      lRet     = GetCommState (lComm(pComm), lDCB)            'Get current settings
      lDCB.BaudRate = 9600                                    'Set Baudrate
      lRet     = SetCommState (lComm(pComm), lDCB)            'And write them back
      lRet     = GetCommTimeOuts (lComm(pComm), lCTO)         'Get current time-outs
      lCTO.ReadIntervalTimeOut = %MAXDWORD
      lCTO.ReadTotalTimeOutMultiplier = 0
      lCTO.ReadTotalTimeOutConstant   = 0
      lRet     = SetCommTimeOuts (lComm(pComm), lCTO)         'And write them back
      lRet     = SetCommMask (lComm(pComm), %EV_RXCHAR)
      lOVL.hEvent = CreateEvent (ByVal %NULL, 0, 0, ByVal %NULL)
      If lOVL.hEvent <> 0 Then
        While %True
        If WaitCommEvent (lComm(pComm), lEvtMsk, ByVal %NULL) Then
          lRet     = SetCommMask (lComm(pComm), %EV_RXCHAR)
          Do
            ReadFile lComm(pComm), lByteBuff, 1, lBytes, ByVal %NULL
            If (lEvtMsk = %EV_RXCHAR) And (lBytes = 1) Then lBuff = lBuff + lByteBuff
          Loop Until (lBytes <> 1)  Or (lByteBuff = $CR)
          If lByteBuff = $CR Then StdOut lBuff : lBuff = ""
        End If
        Wend
        CloseHandle lOVL.hEvent
        CloseHandle lComm(pComm)
      End If
    End Function
    '-------------------------------------------------------------------------------
    Function PbMain ()
      Color 15,0 :Cls
      Print "Waiting for data.... (press ctrl-break to end)
      rxcomm 1         ' 1 is the commport
    End Function

    ------------------
    Peter.
    mailto[email protected][email protected]</A>
    Regards,
    Peter

    Comment

    Working...
    X