vbvb1net进行串口编程.docx
《vbvb1net进行串口编程.docx》由会员分享,可在线阅读,更多相关《vbvb1net进行串口编程.docx(26页珍藏版)》请在冰豆网上搜索。
![vbvb1net进行串口编程.docx](https://file1.bdocx.com/fileroot1/2022-11/26/448615ac-da09-43e3-879c-49ea7b035dc6/448615ac-da09-43e3-879c-49ea7b035dc61.gif)
vbvb1net进行串口编程
vb/进行串口编程
2007-05-2809:
12
2005中有相对应的类直接使用
2005中有相对应的类直接使用。
2003以及以前的看下面的类
OptionStrictOn
ImportsSystem.Runtime.InteropServices
ImportsSystem.Text
ImportsSystem.Threading
'Thisclassprovidesallthenecessarysupportforcommunicating
' withtheCommPort(otherwiseknownastheSerialPort,or
' RS232port).
PublicClassRs232
'Declarethenecessaryclassvariables,andtheirinitialvalues.
PrivatemhRSAsInteger=-1 'HandletoComPort
PrivatemiPortAsInteger=1 'DefaultisCOM1
PrivatemiTimeoutAsInteger=70 'Timeoutinms
PrivatemiBaudRateAsInteger=9600
PrivatemeParityAsDataParity=0
PrivatemeStopBitAsDataStopBit=0
PrivatemiDataBitAsInteger=8
PrivatemiBufferSizeAsInteger=512 'Bufferssizedefaultto512bytes
PrivatemabtRxBufAsByte() 'Receivebuffer
PrivatemeModeAsMode 'Classworkingmode
PrivatembWaitOnReadAsBoolean
PrivatembWaitOnWriteAsBoolean
PrivatembWriteErrAsBoolean
PrivatemuOverlappedAsOVERLAPPED
PrivatemuOverlappedWAsOVERLAPPED
PrivatemuOverlappedEAsOVERLAPPED
PrivatemabtTmpTxBufAsByte() 'TemporarybufferusedbyAsyncTx
PrivatemoThreadTxAsThread
PrivatemoThreadRxAsThread
PrivatemiTmpBytes2ReadAsInteger
PrivatemeMaskAsEventMasks
PrivateMutAsNewMutex
#Region"Enums"
'ThisenumerationprovidesDataParityvalues.
PublicEnumDataParity
Parity_None=0
Pariti_Odd
Parity_Even
Parity_Mark
EndEnum
'ThisenumerationprovidesDataStopBitvalues.
' Itissettobeginwithaone,sothattheenumerationvalues
' matchtheactualvalues.
PublicEnumDataStopBit
StopBit_1=1
StopBit_2
EndEnum
'Thisenumerationcontainsvaluesusedtopurgethevariousbuffers.
PrivateEnumPurgeBuffers
RXAbort=&H2
RXClear=&H8
TxAbort=&H1
TxClear=&H4
EndEnum
'ThisenumerationprovidesvaluesforthelinessenttotheCommPort
PrivateEnumLines
SetRts=3
ClearRts=4
SetDtr=5
ClearDtr=6
ResetDev=7 ' Resetdeviceifpossible
SetBreak=8 ' Setthedevicebreakline.
ClearBreak=9 ' Clearthedevicebreakline.
EndEnum
'ThisenumerationprovidesvaluesfortheModemStatus,since
' we'llbecommunicatingprimarilywithamodem.
'NotethattheFlags()attributeissettoallowforabitwise
' combinationofvalues.
PublicEnumModemStatusBits
ClearToSendOn=&H10
DataSetReadyOn=&H20
RingIndicatorOn=&H40
CarrierDetect=&H80
EndEnum
'ThisenumerationprovidesvaluesfortheWorkingmode
PublicEnumMode
NonOverlapped
Overlapped
EndEnum
'ThisenumerationprovidesvaluesfortheCommMasksused.
'NotethattheFlags()attributeissettoallowforabitwise
' combinationofvalues.
PublicEnumEventMasks
RxChar=&H1
RXFlag=&H2
TxBufferEmpty=&H4
ClearToSend=&H8
DataSetReady=&H10
ReceiveLine=&H20
Break=&H40
StatusError=&H80
Ring=&H100
EndEnum
#EndRegion
#Region"Structures"
'ThisistheDCBstructureusedbythecallstotheWindowsAPI.
=1)>PrivateStructureDCB
PublicDCBlengthAsInteger
PublicBaudRateAsInteger
PublicBits1AsInteger
PublicwReservedAsInt16
PublicXonLimAsInt16
PublicXoffLimAsInt16
PublicByteSizeAsByte
PublicParityAsByte
PublicStopBitsAsByte
PublicXonCharAsByte
PublicXoffCharAsByte
PublicErrorCharAsByte
PublicEofCharAsByte
PublicEvtCharAsByte
PublicwReserved2AsInt16
EndStructure
'ThisistheCommTimeOutsstructureusedbythecallstotheWindowsAPI.
=1)>PrivateStructureCOMMTIMEOUTS
PublicReadIntervalTimeoutAsInteger
PublicReadTotalTimeoutMultiplierAsInteger
PublicReadTotalTimeoutConstantAsInteger
PublicWriteTotalTimeoutMultiplierAsInteger
PublicWriteTotalTimeoutConstantAsInteger
EndStructure
'ThisistheCommConfigstructureusedbythecallstotheWindowsAPI.
=1)>PrivateStructureCOMMCONFIG
PublicdwSizeAsInteger
PublicwVersionAsInt16
PublicwReservedAsInt16
PublicdcbxAsDCB
PublicdwProviderSubTypeAsInteger
PublicdwProviderOffsetAsInteger
PublicdwProviderSizeAsInteger
PublicwcProviderDataAsByte
EndStructure
'ThisistheOverLappedstructureusedbythecallstotheWindowsAPI.
=1)>PublicStructureOVERLAPPED
PublicInternalAsInteger
PublicInternalHighAsInteger
PublicOffsetAsInteger
PublicOffsetHighAsInteger
PublichEventAsInteger
EndStructure
#EndRegion
#Region"Exceptions"
'Thisclassdefinesacustomizedchannelexception.Thisexceptionis
' raisedwhenaNACKisraised.
PublicClassCIOChannelException:
InheritsApplicationException
SubNew(ByValMessageAsString)
MyBase.New(Message)
EndSub
SubNew(ByValMessageAsString,ByValInnerExceptionAsException)
MyBase.New(Message,InnerException)
EndSub
EndClass
'Thisclassdefinesacustomizedtimeoutexception.
PublicClassIOTimeoutException:
InheritsCIOChannelException
SubNew(ByValMessageAsString)
MyBase.New(Message)
EndSub
SubNew(ByValMessageAsString,ByValInnerExceptionAsException)
MyBase.New(Message,InnerException)
EndSub
EndClass
#EndRegion
#Region"Events"
'TheseeventsallowtheprogramusingthisclasstoreacttoCommPort
' events.
PublicEventDataReceived(ByValSourceAsRs232,ByValDataBuffer()AsByte)
PublicEventTxCompleted(ByValSourceAsRs232)
PublicEventCommEvent(ByValSourceAsRs232,ByValMaskAsEventMasks)
#EndRegion
#Region"Constants"
'Theseconstantsareusedtomakethecodeclearer.
PrivateConstPURGE_RXABORTAsInteger=&H2
PrivateConstPURGE_RXCLEARAsInteger=&H8
PrivateConstPURGE_TXABORTAsInteger=&H1
PrivateConstPURGE_TXCLEARAsInteger=&H4
PrivateConstGENERIC_READAsInteger=&H80000000
PrivateConstGENERIC_WRITEAsInteger=&H40000000
PrivateConstOPEN_EXISTINGAsInteger=3
PrivateConstINVALID_HANDLE_VALUEAsInteger=-1
PrivateConstIO_BUFFER_SIZEAsInteger=1024
PrivateConstFILE_FLAG_OVERLAPPEDAsInteger=&H40000000
PrivateConstERROR_IO_PENDINGAsInteger=997
PrivateConstWAIT_OBJECT_0AsInteger=0
PrivateConstERROR_IO_INCOMPLETEAsInteger=996
PrivateConstWAIT_TIMEOUTAsInteger=&H102&
PrivateConstINFINITEAsInteger=&HFFFFFFFF
#EndRegion
#Region"Properties"
'ThispropertygetsorsetstheBaudRate
PublicPropertyBaudRate()AsInteger
Get
ReturnmiBaudRate
EndGet
Set(ByValValueAsInteger)
miBaudRate=Value
EndSet
EndProperty
'ThispropertygetsorsetstheBufferSize
PublicPropertyBufferSize()AsInteger
Get
ReturnmiBufferSize
EndGet
Set(ByValValueAsInteger)
miBufferSize=Value
EndSet
EndProperty
'ThispropertygetsorsetstheDataBit.
PublicPropertyDataBit()AsInteger
Get
ReturnmiDataBit
EndGet
Set(ByValValueAsInteger)
miDataBit=Value
EndSet
EndProperty
'Thiswrite-onlypropertysetsorresetstheDTRline.
PublicWriteOnlyPropertyDtr()AsBoolean
Set(ByValValueAsBoolean)
IfNotmhRS=-1Then
IfValueThen
EscapeCommFunction(mhRS,Lines.SetDtr)
Else
EscapeCommFunction(mhRS,Lines.ClearDtr)
EndIf
EndIf
EndSet
EndProperty
'Thisread-onlypropertyreturnsanarrayofbytesthatrepresents
' theinputcomingintotheCommPort.
OverridableReadOnlyPropertyInputStream()AsByte()
Get
ReturnmabtRxBuf
EndGet
EndProperty
'Thisread-onlypropertyreturnsastringthatrepresents
' thedatacomingintototheCommPort.
OverridableReadOnlyPropertyInputStreamString()AsString
Get
DimoEncoderAsNewSystem.Text.ASCIIEncoding
ReturnoEncoder.GetString(Me.InputStream)
EndGet
EndProperty
'ThispropertyreturnstheopenstatusoftheCommPort.
ReadOnlyPropertyIsOpen()AsBoolean
Get