TYPE COMMTIMEOUTS
ReadIntervalTimeout AS DWORD
ReadTotalTimeoutMultiplier AS DWORD
ReadTotalTimeoutConstant AS DWORD
WriteTotalTimeoutMultiplier AS DWORD
WriteTotalTimeoutConstant AS DWORD
END TYPE
TYPE DCB
DCBlength AS DWORD
BaudRate AS DWORD
DCBflags AS DWORD
wReserved AS WORD
XonLim AS WORD
XoffLim AS WORD
ByteSize AS BYTE
Parity AS BYTE
StopBits AS BYTE
XonChar AS BYTE
XoffChar AS BYTE
ErrorChar AS BYTE
EofChar AS BYTE
EvtChar AS BYTE
wReserved1 AS WORD
END TYPE
TYPE COMSTAT
COMSTATflags AS DWORD
cbInQue AS DWORD
cbOutQue AS DWORD
END TYPE
TYPE SECURITYATTRIBUTES
nLength AS DWORD
lpSecurityDescriptor AS LONG
bInheritHandle AS LONG
END TYPE
TYPE OVERLAPPED
Internal AS LONG
InternalHigh AS LONG
offset AS DWORD
OffsetHigh AS DWORD
hEvent AS LONG
END TYPE
CONST NOPARITY = 0
CONST ODDPARITY = 1
CONST EVENPARITY = 2
CONST MARKPARITY = 3
CONST SPACEPARITY = 4
CONST ONESTOPBIT = 0
CONST ONE5STOPBITS = 1
CONST TWOSTOPBITS = 2
CONST PURGE_RXABORT = &H2
CONST PURGE_RXCLEAR = &H8
CONST PURGE_TXABORT = &H1
CONST PURGE_TXCLEAR = &H4
CONST GENERIC_READ = &H80000000
CONST GENERIC_WRITE = &H40000000
CONST FILE_SHARE_READ = &H1
CONST FILE_SHARE_WRITE = &H2
CONST CREATE_NEW = 1
CONST CREATE_ALWAYS = 2
CONST OPEN_EXISTING = 3
CONST OPEN_ALWAYS = 4
CONST TRUNCATE_EXISTING = 5
CONST FILE_ATTRIBUTE_NORMAL = &H80
CONST FORMAT_MESSAGE_ALLOCATE_BUFFER = &H100
CONST FORMAT_MESSAGE_FROM_SYSTEM = &H1000
CONST LANG_NEUTRAL = &H0
CONST SUBLANG_DEFAULT = &H1
CONST ERROR_BAD_USERNAME = 2202&
CONST ERROR_OPEN_PORT = "There was an error opening the Comm Port. "
CONST ERROR_PORT_BUFFERS = "There was an error creating the Comm Port Buffers. "
CONST ERROR_PURGE_BUFFERS = "There was an error purging old data from the Comm Port Buffers. "
CONST ERROR_TIMEOUTS = "There was an error setting the Comm Port timeouts. "
CONST ERROR_RETRIEVE_PORTSETTINGS = "There was an error retrieving the existing Comm Port settings. "
CONST ERROR_CHANGING_PORTSETTINGS = "There was an error changing the Comm Port settings. "
CONST ERROR_CLOSING_PORT = "There was an error closing the Comm Port. "
CONST ERROR_WRITING_PORT = "There was an error writing data to the Comm Port. "
CONST ERROR_SENDING_DATA = "Could not send all the data in time specified. Try increasing your wait time."
CONST ERROR_GETTING_PORT_STATUS = "There was an error getting the Comm Ports current status. "
CONST ERROR_READING_PORT = "There was an error reading data from the Comm Port. "
DECLARE SUB SLEEP.ms LIB "kernel32" ALIAS "Sleep" (dwMilliseconds AS DWORD)
DECLARE SUB SetLastError LIB "kernel32" ALIAS "SetLastError" (BYVAL dwErrCode AS LONG)
DECLARE FUNCTION WSAGetLastError LIB "WSOCK32" ALIAS "WSAGetLastError" () AS LONG
DECLARE FUNCTION GetLastError LIB "kernel32" ALIAS "GetLastError" () AS DWORD
DECLARE FUNCTION FormatMessage LIB "kernel32" ALIAS "FormatMessageA" _
(dwFlags AS DWORD, _
lpSource AS LONG, _
dwMessageId AS DWORD, _
dwLanguageId AS DWORD, _
BYREF lpBuffer AS STRING, _
nSize AS DWORD, _
lpArguments AS LONG) _
AS LONG
DECLARE FUNCTION SetupComm LIB "kernel32" ALIAS "SetupComm"_
(hFile AS LONG,_
dwInQueue AS DWORD,_
dwOutQueue AS DWORD) _
AS LONG
DECLARE FUNCTION PurgeComm LIB "kernel32" ALIAS "PurgeComm"_
(hFile AS LONG, _
dwFlags AS DWORD) _
AS LONG
DECLARE FUNCTION SetCommTimeouts LIB "kernel32" ALIAS "SetCommTimeouts"_
(hFile AS LONG,_
lpCommTimeouts AS COMMTIMEOUTS) _
AS LONG
DECLARE FUNCTION GetCommState LIB "kernel32" ALIAS "GetCommState" _
(hFile AS LONG, _
lpDCB AS DCB) _
AS LONG
DECLARE FUNCTION SetCommState LIB "kernel32" ALIAS "SetCommState" _
(hFile AS LONG, _
lpDCB AS DCB) _
AS LONG
DECLARE FUNCTION ClearCommError LIB "kernel32" ALIAS "ClearCommError" _
(hFile AS LONG,_
BYREF lpErrors AS DWORD,_
lpStat AS COMSTAT) _
AS LONG
DECLARE FUNCTION CreateFile LIB "kernel32" ALIAS "CreateFileA"_
(lpFileName AS STRING, _
dwDesiredAccess AS DWORD, _
dwShareMode AS DWORD, _
lpSecurityAttributes AS SECURITYATTRIBUTES, _
dwCreationDisposition AS DWORD, _
dwFlagsAndAttributes AS DWORD,_
hTemplateFile AS LONG) _
AS LONG
DECLARE FUNCTION WriteFile LIB "kernel32" ALIAS "WriteFile"_
(hFile AS LONG, _
lpBuffer AS STRING, _
nNumberOfBytesToWrite AS DWORD, _
BYREF lpNumberOfBytesWritten AS DWORD, _
lpOverlapped AS OVERLAPPED) _
AS LONG
DECLARE FUNCTION ReadFile LIB "kernel32" ALIAS "ReadFile"_
(hFile AS LONG, _
BYREF lpBuffer AS STRING, _
nNumberOfBytesToRead AS DWORD, _
BYREF lpNumberOfBytesRead AS DWORD, _
lpOverlapped AS OVERLAPPED) _
AS LONG
DECLARE FUNCTION CloseHandle LIB "kernel32" ALIAS "CloseHandle" _
(hObject AS LONG) _
AS LONG
DECLARE FUNCTION ReadFileEx LIB "kernel32" ALIAS "ReadFileEx"_
(BYVAL hFile AS LONG, _
BYREF lpBuffer AS STRING, _
BYVAL nNumberOfBytesToRead AS DWORD, _
lpOverlapped AS OVERLAPPED,_
BYVAL lpCompletionRoutine AS LONG)_
AS LONG
DECLARE SUB OnComError_EventTemplate (strErrorMessage AS STRING)
DECLARE SUB OnOpen_EventTemplate
DECLARE SUB OnClose_EventTemplate
DECLARE SUB OnWriteString_EventTemplate
DECLARE SUB OnReadString_EventTemplate
TYPE COMPORT EXTENDS QOBJECT
OnComError AS EVENT (OnComError_EventTemplate)
OnOpen AS EVENT (OnOpen_EventTemplate)
OnClose AS EVENT (OnClose_EventTemplate)
OnWriteString AS EVENT (OnWriteString_EventTemplate)
OnReadString AS EVENT (OnReadString_EventTemplate)
Handle AS LONG
Port AS STRING
BaudRate AS DWORD
DataBits AS BYTE
Parity AS BYTE
StopBits AS BYTE
ReadBufSize AS DWORD
WriteBufSize AS DWORD
BytesNotRead AS DWORD
BytesNotWritten AS DWORD
InQue AS DWORD PROPERTY SET GetInQue
OutQue AS DWORD PROPERTY SET GetOutQue
Connected AS BYTE
CONSTRUCTOR
Port = "COM1"
BaudRate = 9600
DataBits = 8
Parity = NOPARITY
StopBits = 1
ReadBufSize = 1024
WriteBufSize = 1024
BytesNotRead = 0
BytesNotWritten = 0
Connected = 0
END CONSTRUCTOR
PRIVATE:
FUNCTION FormatError AS STRING
DIM Buffer AS STRING
DIM lngRet AS LONG
Buffer=SPACE$(1024)
lngRet=FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM, _
0, _
GetLastError(), _
LANG_NEUTRAL, _
Buffer, _
LEN(Buffer), _
0)
Result=LEFT$(Buffer,lngRet)
END FUNCTION
PRIVATE:
SUB GetCommStatus
DIM lngStatus AS LONG
DIM dwErrorFlags AS DWORD
DIM udtCommStat AS COMSTAT
lngStatus = ClearCommError(ComPort.Handle, _
dwErrorFlags, _
udtCommStat)
IF lngStatus = 0 THEN
IF ComPort.OnComError > 0 THEN
CALLFUNC (ComPort.OnComError, ERROR_CHANGING_PORTSETTINGS & ComPort.FormatError)
END IF
ELSE
ComPort.BytesNotWritten = udtCommStat.cbOutQue
ComPort.BytesNotRead = udtCommStat.cbInQue
ComPort.InQue = udtCommStat.cbInQue
ComPort.OutQue = udtCommStat.cbOutQue
END IF
END SUB
PROPERTY SET GetInQue(myVoid AS DWORD)
Comport.GetCommStatus
END PROPERTY
PROPERTY SET GetOutQue(myVoid AS DWORD)
Comport.GetCommStatus
END PROPERTY
PUBLIC:
SUB OPEN
DIM udtCommTimeOuts AS COMMTIMEOUTS
DIM udtDCB AS DCB
DIM lngStatus AS LONG
ComPort.Handle = CreateFile(COMPORT.Port, _
GENERIC_READ OR GENERIC_WRITE, _
FILE_SHARE_READ OR FILE_SHARE_WRITE, _
0&, _
OPEN_EXISTING, _
FILE_ATTRIBUTE_NORMAL, _
0&)
IF ComPort.Handle = -1 THEN
IF ComPort.OnComError > 0 THEN
CALLFUNC (ComPort.OnComError,ERROR_OPEN_PORT & ComPort.FormatError)
END IF
EXIT SUB
END IF
lngStatus = SetupComm(ComPort.Handle,_
ComPort.ReadBufSize,_
ComPort.WriteBufSize)
IF lngStatus = 0 THEN
IF ComPort.OnComError > 0 THEN
CALLFUNC (ComPort.OnComError, ERROR_PORT_BUFFERS & ComPort.FormatError)
END IF
EXIT SUB
END IF
lngStatus = PurgeComm(ComPort.Handle,_
PURGE_TXABORT OR PURGE_RXABORT OR PURGE_TXCLEAR OR PURGE_RXCLEAR)
IF lngStatus = 0 THEN
IF ComPort.OnComError > 0 THEN
CALLFUNC (ComPort.OnComError, ERROR_PURGE_BUFFERS & ComPort.FormatError)
END IF
EXIT SUB
END IF
udtCommTimeOuts.ReadIntervalTimeout = -1
udtCommTimeOuts.ReadTotalTimeoutMultiplier = 0
udtCommTimeOuts.ReadTotalTimeoutConstant = 1000
udtCommTimeOuts.WriteTotalTimeoutMultiplier = 0
udtCommTimeOuts.WriteTotalTimeoutMultiplier = 1000
lngStatus = SetCommTimeouts(ComPort.Handle,_
udtCommTimeOuts)
IF lngStatus = 0 THEN
IF ComPort.OnComError > 0 THEN
CALLFUNC (ComPort.OnComError, ERROR_TIMEOUTS & ComPort.FormatError)
END IF
EXIT SUB
END IF
lngStatus = GetCommState (ComPort.Handle, _
udtDCB)
IF lngStatus = 0 THEN
IF ComPort.OnComError > 0 THEN
CALLFUNC (ComPort.OnComError, ERROR_RETRIEVE_PORTSETTINGS & ComPort.FormatError)
END IF
EXIT SUB
END IF
udtDCB.BaudRate = ComPort.BaudRate
udtDCB.ByteSize = ComPort.DataBits
udtDCB.Parity = ComPort.Parity
udtDCB.StopBits = ComPort.StopBits
lngStatus = SetCommState (ComPort.Handle, _
udtDCB)
IF lngStatus = 0 THEN
IF ComPort.OnComError > 0 THEN
CALLFUNC (ComPort.OnComError, ERROR_CHANGING_PORTSETTINGS & ComPort.FormatError)
END IF
ELSE
ComPort.Connected = 1
IF ComPort.OnOpen > 0 THEN
CALLFUNC ComPort.OnOpen
END IF
END IF
END SUB
PUBLIC:
SUB PurgeIn ()
PurgeComm(ComPort.Handle, PURGE_RXABORT OR PURGE_RXCLEAR)
END SUB
SUB PurgeOut
PurgeComm(ComPort.Handle, PURGE_TXABORT OR PURGE_TXCLEAR)
END SUB
PUBLIC:
SUB CLOSE
DIM lngStatus AS LONG
lngStatus = CloseHandle (ComPort.Handle)
IF lngStatus = 0 THEN
IF ComPort.OnComError > 0 THEN
CALLFUNC (ComPort.OnComError, ERROR_CLOSING_PORT & ComPort.FormatError)
END IF
ELSE
ComPort.Connected = 0
IF ComPort.OnClose > 0 THEN
CALLFUNC ComPort.OnClose
END IF
END IF
END SUB
PUBLIC:
SUB WriteString (strData AS STRING, intWait AS INTEGER)
DIM lngStatus AS LONG
DIM dwBytesToWrite AS DWORD
DIM dwByesWritten AS DWORD
dwBytesToWrite=LEN(strData)
lngStatus = WriteFile(ComPort.Handle, _
strData, _
dwBytesToWrite, _
dwByesWritten, _
0&)
IF lngStatus = 0 THEN
IF ComPort.OnComError > 0 THEN
CALLFUNC (ComPort.OnComError, ERROR_WRITING_PORT & ComPort.FormatError)
END IF
ELSE
IF intWait THEN SLEEP.ms(intWait)
ComPort.GetCommStatus
IF Comport.BytesNotWritten = 0 THEN
IF ComPort.OnWriteString > 0 THEN
CALLFUNC ComPort.OnWriteString
END IF
ELSE
IF ComPort.OnComError > 0 THEN
CALLFUNC (ComPort.OnComError, ERROR_SENDING_DATA)
END IF
END IF
END IF
END SUB
PUBLIC:
FUNCTION ReadString (dwRdSize AS DWORD, intWait AS INTEGER) AS STRING
DIM lngStatus AS LONG
DIM dwBytesRead AS DWORD
DIM strReadBuffer AS STRING
strReadBuffer=SPACE$(ComPort.ReadBufSize)
lngStatus = ReadFile(Comport.Handle, _
strReadBuffer, _
dwRdSize, _
dwBytesRead, _
0&)
IF lngStatus = 0 THEN
IF ComPort.OnComError > 0 THEN
CALLFUNC (ComPort.OnComError, ERROR_READING_PORT & ComPort.FormatError)
END IF
ELSE
Result = LEFT$(strReadBuffer, dwBytesRead)
IF intWait THEN SLEEP.ms(intWait)
ComPort.GetCommStatus
IF ComPort.OnReadString > 0 THEN
CALLFUNC ComPort.OnReadString
END IF
END IF
END FUNCTION
END TYPE
$DEFINE QCOMPORT COMPORT
|
|