home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / serial1a / serialpo.bas < prev   
Encoding:
BASIC Source File  |  1999-09-03  |  6.0 KB  |  181 lines

  1. Attribute VB_Name = "SerialPort"
  2. Option Explicit
  3.  
  4. Global ComNum As Long
  5. Global bRead(255) As Byte
  6.  
  7. Type COMSTAT
  8.         fCtsHold As Long
  9.         fDsrHold As Long
  10.         fRlsdHold As Long
  11.         fXoffHold As Long
  12.         fXoffSent As Long
  13.         fEof As Long
  14.         fTxim As Long
  15.         fReserved As Long
  16.         cbInQue As Long
  17.         cbOutQue As Long
  18. End Type
  19.  
  20. Type COMMTIMEOUTS
  21.         ReadIntervalTimeout As Long
  22.         ReadTotalTimeoutMultiplier As Long
  23.         ReadTotalTimeoutConstant As Long
  24.         WriteTotalTimeoutMultiplier As Long
  25.         WriteTotalTimeoutConstant As Long
  26. End Type
  27.  
  28. Type DCB
  29.         DCBlength As Long
  30.         BaudRate As Long
  31.         fBinary As Long
  32.         fParity As Long
  33.         fOutxCtsFlow As Long
  34.         fOutxDsrFlow As Long
  35.         fDtrControl As Long
  36.         fDsrSensitivity As Long
  37.         fTXContinueOnXoff As Long
  38.         fOutX As Long
  39.         fInX As Long
  40.         fErrorChar As Long
  41.         fNull As Long
  42.         fRtsControl As Long
  43.         fAbortOnError As Long
  44.         fDummy2 As Long
  45.         wReserved As Integer
  46.         XonLim As Integer
  47.         XoffLim As Integer
  48.         ByteSize As Byte
  49.         Parity As Byte
  50.         StopBits As Byte
  51.         XonChar As Byte
  52.         XoffChar As Byte
  53.         ErrorChar As Byte
  54.         EofChar As Byte
  55.         EvtChar As Byte
  56. End Type
  57.  
  58. Type OVERLAPPED
  59.         Internal As Long
  60.         InternalHigh As Long
  61.         offset As Long
  62.         OffsetHigh As Long
  63.         hEvent As Long
  64. End Type
  65. Type SECURITY_ATTRIBUTES
  66.         nLength As Long
  67.         lpSecurityDescriptor As Long
  68.         bInheritHandle As Long
  69. End Type
  70.  
  71. Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
  72. Declare Function GetLastError Lib "kernel32" () As Long
  73. Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, lpOverlapped As Long) As Long
  74. Declare Function WriteFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, lpOverlapped As Long) As Long
  75. Declare Function SetCommTimeouts Lib "kernel32" (ByVal hFile As Long, lpCommTimeouts As COMMTIMEOUTS) As Long
  76. Declare Function GetCommTimeouts Lib "kernel32" (ByVal hFile As Long, lpCommTimeouts As COMMTIMEOUTS) As Long
  77. Declare Function BuildCommDCB Lib "kernel32" Alias "BuildCommDCBA" (ByVal lpDef As String, lpDCB As DCB) As Long
  78. Declare Function SetCommState Lib "kernel32" (ByVal hCommDev As Long, lpDCB As DCB) As Long
  79. Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
  80. Declare Function FlushFileBuffers Lib "kernel32" (ByVal hFile As Long) As Long
  81.  
  82.  
  83. Function fin_com()
  84.     fin_com = CloseHandle(ComNum)
  85. End Function
  86.  
  87. Function FlushComm()
  88.     FlushFileBuffers (ComNum)
  89. End Function
  90.  
  91. Function Init_Com(ComNumber As String, Comsettings As String) As Boolean
  92. On Error GoTo handelinitcom
  93.     Dim ComSetup As DCB, Answer, Stat As COMSTAT, RetBytes As Long
  94.     Dim retval As Long
  95.     Dim CtimeOut As COMMTIMEOUTS, BarDCB As DCB
  96.     ' Open the communications port for read/write (&HC0000000).
  97.     ' Must specify existing file (3).
  98.     ComNum = CreateFile(ComNumber, &HC0000000, 0, 0&, &H3, 0, 0)
  99.     If ComNum = -1 Then
  100.         MsgBox "Com Port " & ComNumber & " not available. Use Serial settings (on the main menu) to setup your ports.", 48
  101.         Init_Com = False
  102.         Exit Function
  103.     End If
  104.     'Setup Time Outs for com port
  105.     CtimeOut.ReadIntervalTimeout = 20
  106.     CtimeOut.ReadTotalTimeoutConstant = 1
  107.     CtimeOut.ReadTotalTimeoutMultiplier = 1
  108.     CtimeOut.WriteTotalTimeoutConstant = 10
  109.     CtimeOut.WriteTotalTimeoutMultiplier = 1
  110.     retval = SetCommTimeouts(ComNum, CtimeOut)
  111.     If retval = -1 Then
  112.         retval = GetLastError()
  113.         MsgBox "Unable to set timeouts for port " & ComNumber & " Error: " & retval
  114.         retval = CloseHandle(ComNum)
  115.         Init_Com = False
  116.         Exit Function
  117.     End If
  118.     retval = BuildCommDCB(Comsettings, BarDCB)
  119.     If retval = -1 Then
  120.         retval = GetLastError()
  121.         MsgBox "Unable to build Comm DCB " & Comsettings & " Error: " & retval
  122.         retval = CloseHandle(ComNum)
  123.         Init_Com = False
  124.         Exit Function
  125.     End If
  126.     retval = SetCommState(ComNum, BarDCB)
  127.     If retval = -1 Then
  128.         retval = GetLastError()
  129.         MsgBox "Unable to set Comm DCB " & Comsettings & " Error: " & retval
  130.         retval = CloseHandle(ComNum)
  131.         Init_Com = False
  132.         Exit Function
  133.     End If
  134.     
  135.     Init_Com = True
  136. handelinitcom:
  137.     Exit Function
  138. End Function
  139.  
  140. Function ReadCommPure() As String
  141. On Error GoTo handelpurecom
  142.     Dim RetBytes As Long, i As Integer, ReadStr As String, retval As Long
  143.     Dim CheckTotal As Integer, CheckDigitLC As Integer
  144.     retval = ReadFile(ComNum, bRead(0), 255, RetBytes, 0)
  145.     ReadStr = ""
  146.     If (RetBytes > 0) Then
  147.         For i = 0 To RetBytes - 1
  148.             ReadStr = ReadStr & Chr(bRead(i))
  149.         Next i
  150.        Else
  151.         FlushComm
  152.     End If
  153.     'Return the string read from serial port
  154.     ReadCommPure = ReadStr
  155. handelpurecom:
  156.     Exit Function
  157. End Function
  158.  
  159. Function WriteCOM32(COMString As String) As Integer
  160. On Error GoTo handelwritelpt
  161.     Dim RetBytes As Long, LenVal As Long
  162.     Dim retval As Long
  163.     
  164.     If Len(COMString) > 255 Then
  165.         WriteCOM32 Left$(COMString, 255)
  166.         WriteCOM32 Right$(COMString, Len(COMString) - 255)
  167.         Exit Function
  168.     End If
  169.     
  170.     For LenVal = 0 To Len(COMString) - 1
  171.         bRead(LenVal) = Asc(Mid$(COMString, LenVal + 1, 1))
  172.     Next LenVal
  173. '    bRead(LenVal) = 0
  174.     retval = WriteFile(ComNum, bRead(0), Len(COMString), RetBytes, 0)
  175. '    FlushComm
  176.     WriteCOM32 = RetBytes
  177.     
  178. handelwritelpt:
  179.     Exit Function
  180. End Function
  181.