home *** CD-ROM | disk | FTP | other *** search
/ Dan Appleman's Visual Bas…s Guide to the Win32 API / Dan.Applmans.Visual.Basic.5.0.Programmers.Guide.To.The.Win32.API.1997.Ziff-Davis.Press.CD / VB5PG32.mdf / vbpg32 / samples4 / ch21 / commutil.bas < prev    next >
Encoding:
BASIC Source File  |  1996-01-25  |  1.9 KB  |  69 lines

  1. Attribute VB_Name = "COMMUTIL"
  2. '
  3. '   This function checks the input port and retrieves the
  4. '   contents of the input buffer. Returns the empty string
  5. '   if no characters were found.
  6. '
  7. Function GetChars$()
  8.     Dim csbuf As COMSTAT
  9.     Dim instring$
  10.     Dim charsread%
  11.  
  12.     If CommID% < 0 Then Exit Function ' Make sure port is open
  13.  
  14.     ' Is there space for the data?
  15.  
  16.     di% = GetCommError(CommID%, csbuf)
  17.  
  18.     If csbuf.cbInQue = 0 Then ' No characters found
  19.         GetChars$ = ""
  20.         Exit Function
  21.     End If
  22.  
  23.     instring$ = String$(csbuf.cbInQue, 0)
  24.  
  25.     ' A real application would check for an error here
  26.     di% = ReadComm(CommID%, instring$, csbuf.cbInQue)
  27.  
  28.     charsread% = Abs(di%)
  29.     If charsread% = csbuf.cbInQue Then instring$ = instring$ + GetChars$()
  30.  
  31.     GetChars$ = instring$
  32.  
  33. End Function
  34.  
  35.  
  36. '
  37. ' Sends the characters specified.  Changes tosend$ to
  38. ' the characters that do not fit into the output buffer
  39. '
  40. Sub SendChars(tosend$)
  41.     Dim csbuf As COMSTAT
  42.     Dim outstring$
  43.  
  44.     If CommID% < 0 Then Exit Sub ' Make sure port is open
  45.  
  46.     ' Is there space for the data?
  47.  
  48.     di% = GetCommError(CommID%, csbuf)
  49.  
  50.     If BufferSize% - csbuf.cbOutQue < Len(tosend$) Then
  51.         ' This is an error - a real application would somehow
  52.         ' need to notify the calling function that it could
  53.         ' only write part of the data so that the calling
  54.         ' function would know what to do next.
  55.         ' What we do is only write the portion of the string
  56.         ' that fits, then change tosend$ to exclude the data
  57.         ' written.
  58.         outstring$ = Left$(tosend$, BufferSize% - csbuf.cbOutQue)
  59.         tosend$ = Mid$(tosend$, BufferSize% - csbuf.cbOutQue + 1)
  60.     Else
  61.         outstring$ = tosend$
  62.         tosend$ = ""
  63.     End If
  64.  
  65.     ' A real application would check for an error here
  66.     di% = WriteComm(CommID%, outstring$, Len(outstring$))
  67. End Sub
  68.  
  69.