Rapid-Q Documentation by William Yu (c)1999,  appended by John Kelly Appendix A: QCOMPORT

QCOMPORT Component
 (included in RAPIDQ2.INC)

QComPort provides an easy to use interface to communicate with the com ports. It was dropped from RapidQ, but now lives! The code extends the custom "Comport" by Pete Kleinschmidt. Most of the original QComport properties and Methods are incorporated. This component only for Windows 32. You cannot make arrays of this object and must be declared at the GLOBAL level. Does not need additional DLLs. The QCOMPORT Component has not been extensively tested. Works on Win98SE, and XP SP2). Use at your own risk.

Limitations:

Doesn't perform checks if hardware exists
Does not have the ability change flow control settings (Hardware, Xon/Xoff, etc).
When a port is opened it should default to whatever flow control settings last used on open or the default settings.
This requires programming some of the Device Control Block (DCB) settings, like that for Baud, Parity, etc.
The following docs may help you out.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/hardware/commun_2pf7.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwbgen/html/msdn_serial.asp

QComPort Properties
Field Type R/W Default




BytesNotRead DWORD R
BytesNotWritten DWORD R
BaudRate DWORD RW br110
Connected BYTE R
DataBits BYTE RW 8
Handle LONG R
InQue (same as BytesNotRead, for compatibility) DWORD R
OutQue (same as BytesNotWritten, for compatibility) DWORD R
Parity BYTE RW prNone
PendingIO (***)INTEGER R
Port      (now a string) STRING RW COM1
ReadBufSize DWORD RW 1024
StopBits BYTE RW sbOneStopBit
WriteBufSize DWORD RW 1024

QComPort Methods
Method Type Description Params




AbortAllIO SUB (***) Aborts all asynchronous read/write operations 0
Close SUB Closes communication port 0
Open SUB Opens communication port 0
PurgeIn SUB Clears input buffer and stops all input functions 0
PurgeOut SUB Clears output buffer and stops all output functions 0
Read SUB Read(QFile/QMemoryStream, Count%, Wait%)
(***)
Reads stream data from com port, Count% < 32000 3
ReadString FUNCTION ReadString$(Count%, Wait%) Returns a string representation bytes read from comport 2
WaitForLastIO SUB (***) Blocks until last IO is completed 0
Write SUB Write(QMemoryStream, Count%, Wait%) Writes binary stream to com port
May need Count% < 32000
3
WriteString SUB WriteString(Str$, Wait%) Writes string to communication port 2

QComPort Events
Event Type Occurs when... Params




OnBreak VOID (***) A line break is detected, input and output is suspended until break is cleared 0
OnClose VOID Com Port is successfully closed 0
OnComError SUB(ErrorMessage AS String)  An error occurs 0
OnOpen VOID Com port is successfully opened 0
OnReadString  VOID Data successfully read from Com Port 0
OnWriteString  VOID Data successfully written to Com Port 0
OnRing VOID (***) A ring signal is detected, used only with modems. 0
OnRxChar SUB (InQue%) (***) A character(s) arrives in the input buffer. 1
OnTxEmpty VOID (***) Output buffer is flushed 0
(*** = no longer supported)

QComPort Examples
$Apptype GUI
$TYPECHECK ON
$OPTIMIZE ON
$INCLUDE "RAPIDQ2.INC"

DECLARE SUB OpenClick
DECLARE SUB SendClick
DECLARE SUB ReadClick
DECLARE SUB ComError(strErrorMessage AS STRING)
DECLARE SUB PortOpen
DECLARE SUB PortClose
DECLARE SUB PortClosed
DECLARE SUB StringWritten
DECLARE SUB StringRead

DIM strPort   AS STRING
DIM dwBaud    AS DWORD
DIM bParity   AS BYTE
DIM bDataBits AS BYTE
DIM bStopBits AS BYTE

DIM MyPort AS COMPORT
MyPort.OnComError     = ComError
MyPort.OnOpen         = PortOpen
MyPort.OnClose        = PortClosed
MyPort.OnWriteString  = StringWritten
MyPort.OnReadString   = StringRead

DIM Font AS QFont
Font.Name="Courier New"


CREATE Form AS QFORM
    Caption = "Form1"
    Width   = 820
    Height  = 355
    Center
    CREATE lblPort AS QLABEL
        Caption = "COM Port"
        Left = 10: Width = 100: Top = 5
    END CREATE
    CREATE lblBaud AS QLABEL
        Caption = "Bits per second"
        Left = 10: Width = 100: Top = 30
    END CREATE
    CREATE lblParity AS QLABEL
        Caption = "Parity"
        Left = 10: Width = 100: Top = 55
    END CREATE
    CREATE lblDataBits AS QLABEL
        Caption = "Data bits"
        Left = 10: Width = 100: Top = 80
    END CREATE
    CREATE lblStopBits AS QLABEL
        Caption = "Stop Bits"
        Left = 10: Width = 100: Top = 105
    END CREATE
    CREATE lblSend AS QLABEL
        Caption = "String to Send"
        Left = 10: Width = 100: Top = 130
    END CREATE
    CREATE cbPort AS QCOMBOBOX
        AddItems "COM1","COM2","COM3","COM4"
        Left      = 120: Width = 70: Top = 5
        ItemIndex = 0
    END CREATE
    CREATE cbBaud AS QCOMBOBOX
        AddItems "9600", "14400", "19200","38400", "56000", "57600", "115200"
        Left = 120: Width = 70: Top = 30
        ItemIndex = 6
    END CREATE
    CREATE cbParity AS QCOMBOBOX
        AddItems  "None", "Odd", "Even", "Mark", "Space"
        Left = 120: Width = 70: Top = 55
        ItemIndex = 0
    END CREATE
    CREATE cbDataBits AS QCOMBOBOX
        AddItems  "4", "5", "6", "7", "8"
        Left = 120: Width = 70: Top = 80
        ItemIndex = 4
    END CREATE
    CREATE cbStopBits AS QCOMBOBOX
        AddItems "1", "1.5", "2"
        Left = 120: Width = 70: Top = 105
        ItemIndex = 0
    END CREATE
    CREATE Edit1 AS QEDIT
        Text  = ""
        Left = 120: Top = 130: Width = 200
    END CREATE
    CREATE RichEdit1 AS QRICHEDIT
        Left = 3: Top = 160: Width = 795: Height = 130
        PlainText   = 1
        ReadOnly    = 1
        ScrollBars  = 3
        WordWrap    = 0
        Font        = Font
    END CREATE
    CREATE StatusBar AS QStatusBar
        Left = 10: Top = 300: Width = 795: Height = 20
        SizeGrip  = FALSE
        AddPanels "Panel 1", "Panel 2"
        Panel(0).Caption = "Not Connected"
        Panel(0).Width = 645
        Panel(1).Caption = ""
    END CREATE
    CREATE OpenButton AS QBUTTON
        Caption = "&Open Port"
        Left = 330: Top = 5: Height = 20
        OnClick = OpenClick
    END CREATE
    CREATE SendButton AS QBUTTON
        Caption = "&Send"
        Left = 330: Top = 130: Height = 20
        Enabled = 0
        OnClick = SendClick
    END CREATE
    CREATE ReadButton AS QBUTTON
        Caption = "&Read"
        Left = 440: Top = 130: Height = 20
        Enabled = 0
        OnClick = ReadClick
    END CREATE
END CREATE



SUB OpenClick
    SELECT CASE cbPort.ItemIndex
     CASE 0
      strPort = "COM1"
     CASE 1
      strPort = "COM2"
     CASE 2
      strPort = "COM3"
     CASE 3
      strPort = "COM4"
    END SELECT

    SELECT CASE cbBaud.ItemIndex
      CASE 0
       dwBaud = 110
      CASE 1
       dwBaud = 300
      CASE 2
       dwBaud = 600
      CASE 3
       dwBaud = 1200
      CASE 4
       dwBaud = 2400
      CASE 5
       dwBaud = 4800
      CASE 6
       dwBaud = 9600
      CASE 7
       dwBaud = 14400
      CASE 8
       dwBaud = 19200
      CASE 9
       dwBaud = 38400
      CASE 10
       dwBaud = 56000
      CASE 11
       dwBaud = 57600
      CASE 12
       dwBaud = 115200
    END SELECT

    SELECT CASE cbParity.ItemIndex
      CASE 0
        bParity = NOPARITY
      CASE 1
        bParity = ODDPARITY
      CASE 2
        bParity = EVENPARITY
      CASE 3
        bParity = MARKPARITY
      CASE 4
        bParity = SPACEPARITY
    END SELECT

    SELECT CASE cbDataBits.ItemIndex
      CASE 0
        bDataBits = 4
      CASE 1
        bDataBits = 5
      CASE 2
        bDataBits = 6
      CASE 3
        bDataBits = 7
      CASE 4
        bDataBits = 8
    END SELECT

    SELECT CASE cbStopBits.ItemIndex
      CASE 0
        bStopBits = ONESTOPBIT
      CASE 1
        bStopBits = ONE5STOPBITS
      CASE 2
        bStopBits = TWOSTOPBITS
    END SELECT

   'Settings
   MyPort.Port     = strPort
   MyPort.BaudRate = dwBaud
   MyPort.Parity   = bParity
   MyPort.DataBits = bDataBits
   MyPort.StopBits = bStopBits

   'Open COM Port
   MyPort.Open
   MyPort.PurgeIn

   IF MyPort.Connected=TRUE THEN
     OpenButton.Enabled = 0
     SendButton.Enabled = 1
     ReadButton.Enabled = 1
   END IF
END SUB




SUB SendClick
  DIM BytesRead AS DWORD

  IF Edit1.Text="" THEN
    SHOWMESSAGE "Please enter String to Send"
  ELSE
    'Write to COM Port
    MyPort.WriteString(Edit1.Text + CHR$(13) +CHR$(10), 1000)
    Edit1.Text=""

    WHILE MyPort.BytesNotRead > 0
      RichEdit1.Text=RichEdit1.Text & MyPort.ReadString(MyPort.BytesNotRead, 500)	'& is addition operator..
      BytesRead=MyPort.BytesNotRead + BytesRead
      StatusBar.Panel(1).Caption = "Number of Bytes Read  " & STR$(BytesRead)
    WEND

    PortClose
  END IF
END SUB





SUB ReadClick
  DIM BytesRead AS DWORD

	MyPort.PurgeIn
    WHILE MyPort.BytesNotRead > 0
      RichEdit1.Text=RichEdit1.Text & MyPort.ReadString(MyPort.BytesNotRead, 5)	'& is addition operator..
      BytesRead=MyPort.BytesNotRead + BytesRead
      StatusBar.Panel(1).Caption = "Number of Bytes Read  " & STR$(BytesRead)
    WEND
END SUB



SUB ComError (strErrorMessage AS STRING)
  SHOWMESSAGE strErrorMessage
END SUB


SUB PortOpen
  StatusBar.Panel(0).Caption = "Connected to " & MyPort.Port
END SUB


SUB PortClose
    MyPort.Close
END SUB


SUB PortClosed
  StatusBar.Panel(0).Caption = "Not Connected"
  OpenButton.Enabled = 1
  SendButton.Enabled = 0
END SUB

SUB StringWritten
  SHOWMESSAGE "String was written"
END SUB

SUB StringRead
  SHOWMESSAGE "String was read"
END SUB
Form.ShowModal

Prev Component Contents Next Component