Você está na página 1de 5

'This program is based on the following Arduino's source code:

'C:\Arduino\sketch_nov05a\sketch_nov05a.pde. It is intended to
'monitor the state od an LED
'Types
Private Type DCB
DCBlength As Long
BaudRate As Long
fBitFields As Long 'See Comments in Win32API.Txt
wReserved As Integer
XonLim As Integer
XoffLim As Integer
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 Integer 'Reserved; Do Not Use
End Type
Private Type COMMTIMEOUTS
ReadIntervalTimeout As Long
ReadTotalTimeoutMultiplier As Long
ReadTotalTimeoutConstant As Long
WriteTotalTimeoutMultiplier As Long
WriteTotalTimeoutConstant As Long
End Type
Private Type OVERLAPPED
Internal As Long
InternalHigh As Long
offset As Long
OffsetHigh As Long
hEvent As Long
End Type
'Variables
Dim hPort As Long 'Handle to the port
Dim hPortReady As Boolean 'Is port ready for Rx & Tx?
'Constants
Private Const
Private Const
Private Const
Private Const
Private Const
Private Const
Private Const
Private Const
Private Const
Private Const

GENERIC_READ As Long = &H80000000


GENERIC_WRITE As Long = &H40000000
OPEN_EXISTING As Long = 3
ERROR_FILE_NOT_FOUND As Long = 2&
INVALID_HANDLE_VALUE As Long = -1
ONESTOPBIT As Byte = 0
NOPARITY As Byte = 0
MAXDWORD As Long = &HFFFF
CARRIAGE_RETURN As Long = &HD
FORMAT_MESSAGE_FROM_SYSTEM As Long = &H1000

'Event constants
Private Const EV_BREAK As Long = &H40
Private Const EV_CTS As Long = &H8
Private Const EV_DSR As Long = &H10

' BREAK received


' CTS changed state
' DSR changed state

Private
red
Private
Private
Private
Private
ter
Private
Private

Const EV_ERR As Long = &H80


Const
Const
Const
Const

EV_RING As Long =
EV_RLSD As Long =
EV_RXCHAR As Long
EV_RXFLAG As Long

&H100
&H20
= &H1
= &H2

Const EV_TXEMPTY As Long = &H4


Const EV_NULL As Long = &H0

' Line status error occur


' Ring signal detected
' RLSD changed state
' Any Character received
' Received certain charac
' Transmitt Queue Empty
' No event

'APIs
Private Declare Function ReadFile Lib "kernel32" ( _
ByVal hFile As Long, _
lpBuffer As Any, _
ByVal nNumberOfBytesToRead As
Long, _
lpNumberOfBytesRead As Long, _
lpOverlapped As OVERLAPPED) As
Long
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lp
FileName As String, _
ByVal dw
DesiredAccess As Long, _
ByVal dw
ShareMode As Long, _
lpSecuri
tyAttributes As Any, _
ByVal dw
CreationDisposition As Long, _
ByVal dw
FlagsAndAttributes As Long, _
ByVal hT
emplateFile As Long) As Long
Private Declare Function FormatMessage Lib "kernel32" Alias "FormatMessageA" ( _
B
yVal dwFlags As Long, _
l
pSource As Any, _
B
yVal dwMessageId As Long, _
B
yVal dwLanguageId As Long, _
B
yVal lpBuffer As String, _
B
yVal nSize As Long, _
A
rguments As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As L
ong
Private Declare Function SetCommState Lib "kernel32" (ByVal hCommDev As Long, lp
DCB As DCB) As Long
Private Declare Function SetCommTimeouts Lib "kernel32" (ByVal hFile As Long, lp
CommTimeouts As COMMTIMEOUTS) As Long
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Declare Function GetCommState Lib "kernel32" (ByVal nCid As Long, lpDCB
As DCB) As Long
Private Declare Function GetCommModemStatus Lib "kernel32" (ByVal hFile As Long,

lpModemStat As Long) As Long


Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpString
As String) As Long
Private Declare Function SetCommMask Lib "kernel32" (ByVal hFile As Long, ByVal
dwEvtMask As Long) As Long
Private Declare Function WaitCommEvent Lib "kernel32" (ByVal hFile As Long, lpEv
tMask As Long, lpOverlapped As OVERLAPPED) As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Function Read_Port() As Long
Dim dOverlapped As OVERLAPPED, rtLng As Long
Dim ThisByte As Byte, BytesRead As Long, dwCommModemStatus As Long
'Make sure to set the timeout properties in accordance to your program's obje
ctives.
'Attempting to read data from the port when there is no data available
'or waiting for an event that will not happen may cause the program to
'hang and become unresponsive
With dOverlapped 'Send it as null
.Internal = 0
.InternalHigh = 0
.offset = 0
.OffsetHigh = 0
.hEvent = 0
End With
'Set the events to be monitored
rtLng = SetCommMask(hPort, EV_BREAK Or EV_CTS Or EV_DSR Or EV_ERR Or EV_RING
Or EV_RLSD Or EV_RXCHAR Or EV_RXFLAG Or EV_TXEMPTY Or EV_NULL)
If rtLng > 0 Then 'If any of the above events took place
While hPort <> INVALID_HANDLE_VALUE And hPortReady 'Keep monitoring whil
e the port is ready for reading
rtLng = WaitCommEvent(hPort, dwCommModemStatus, dOverlapped) 'Wait f
or the events to occur
rtLng = SetCommMask(hPort, EV_BREAK Or EV_CTS Or EV_DSR Or EV_ERR Or
EV_RING Or EV_RLSD Or EV_RXCHAR Or EV_RXFLAG Or EV_TXEMPTY Or EV_NULL)
DoEvents
If dwCommModemStatus And EV_RXCHAR Then 'If data has been received
rtLng = ReadFile(hPort, ThisByte, 1, BytesRead, dOverlapped) 'Re
ad the data
If BytesRead = 1 Then 'Success reading data, expect a single byt
e to be read
If ThisByte = 0 Then 'LED is off
Label3.Caption = "The state of
Shape1.FillColor = vbBlack
ElseIf ThisByte = 1 Then
Label3.Caption = "The state of
Shape1.FillColor = vbRed
Else
Label3.Caption = "The state of
End If
End If
Else 'No events detected, close the port &
Close_Port

the LED is: off"


the LED is: on"
the LED is: unknown"
exit

hPort = INVALID_HANDLE_VALUE
Read_Port = -1
Exit Function
End If
Wend
End If
Read_Port = rtLng
End Function
Private Function Prepare_Port(wMsg As Boolean) As Boolean
Dim PortNumber As String, rtLng As Long, PortDCB As DCB
Dim cTimeOuts As COMMTIMEOUTS
Prepare_Port = False
'1) open port
PortNumber = "COM" & Trim(Text1.Text) & ":"
hPort = CreateFile(PortNumber, GENERIC_READ Or GENERIC_WRITE, _
ByVal 0&, ByVal 0&, _
OPEN_EXISTING, ByVal 0&, ByVal 0&)
If hPort = INVALID_HANDLE_VALUE Then
If wMsg Then Display_Error 'Display the error message
rtLng = Close_Port
Prepare_Port = False
Exit Function
End If
'2) Configure port
With PortDCB
.DCBlength = Len(DCB) 'Initialize DCB Length member
.BaudRate = CLng(Val(Trim(Text2.Text))) 'Current Baud rate
.ByteSize = 8 'Number of bits
.StopBits = ONESTOPBIT
.Parity = NOPARITY
End With
rtLng = SetCommState(hPort, PortDCB)
If rtLng = 0 Then 'Failure
If wMsg Then Display_Error
Prepare_Port = False
Exit Function
End If
'3) Set the timeout properties
With cTimeOuts
.ReadIntervalTimeout = MAXDWORD 'How long to wait between receiving cha
rs before timing out
If wMsg Then .ReadTotalTimeoutMultiplier = 1000 'How long to wait befor
e returning
If Not wMsg Then .ReadTotalTimeoutMultiplier = 0 'How long to wait befor
e returning
.ReadTotalTimeoutConstant = 0 'How much additional time to wait before
returning for each byte that was requested in the read operation
.WriteTotalTimeoutMultiplier = 0
.WriteTotalTimeoutConstant = 0
End With

rtLng = SetCommTimeouts(hPort, cTimeOuts)


If rtLng = 0 Then 'Failure
If wMsg Then Display_Error
Prepare_Port = False
Exit Function
End If
Prepare_Port = True
End Function
Private Function Close_Port() As Long
Close_Port = CloseHandle(hPort)
Close_Port = INVALID_HANDLE_VALUE
hPort = INVALID_HANDLE_VALUE
End Function
Private Sub Display_Error()
Dim LastError As Long, msgbuf As String, i As Long
LastError = Err.LastDllError
msgbuf = Space(256)
i = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, LastError, 0, msgbuf, 256,
0)
MsgBox "Error in API: " & Left(msgbuf, i)
End Sub
Private Sub Command1_Click()
hPortReady = False
hPortReady = Prepare_Port(True)
If hPortReady Then
Command1.Enabled = False
Read_Port
End If
End Sub
Private Sub Command2_Click()
Close_Port
hPortReady = False
Command1.Enabled = True
End Sub
Private Sub Command3_Click()
Close_Port
End
End Sub

Você também pode gostar