Escolar Documentos
Profissional Documentos
Cultura Documentos
'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
'Event constants
Private Const EV_BREAK As Long = &H40
Private Const EV_CTS As Long = &H8
Private Const EV_DSR As Long = &H10
Private
red
Private
Private
Private
Private
ter
Private
Private
EV_RING As Long =
EV_RLSD As Long =
EV_RXCHAR As Long
EV_RXFLAG As Long
&H100
&H20
= &H1
= &H2
'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,
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