在VB.NET中如何访问COM串口?

CityFantom 2003-12-22 12:16:32
在VB.NET中如何访问COM串口?不用Windows API,也不用ActiveX控件,有没有相关的.NET类
...全文
61 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
yzjd 2004-02-18
  • 打赏
  • 举报
回复
使用RS232类!有好东西别望了我也来一份,谢谢!zjdmail@ynmail.com
dunheng 2004-02-18
  • 打赏
  • 举报
回复
有实例程序发一份给我,谢谢.peposi@mainone.com.cn
okaka 2004-02-18
  • 打赏
  • 举报
回复
good!
come on study!
msdn_vipuser 2004-02-18
  • 打赏
  • 举报
回复
不错!
bpy 2004-02-17
  • 打赏
  • 举报
回复
gz
keyzh 2004-01-14
  • 打赏
  • 举报
回复
能发给我一份么? key_zh@hotmail.com
另外:MsComm控件好象只能支持最大串口号<16吧,有时需要使用比较多串口时就会有问题了
aycn 2004-01-14
  • 打赏
  • 举报
回复
能不能也给我一份,谢谢!
aycn@163.com
LVOLCANO 2004-01-14
  • 打赏
  • 举报
回复
MK
littlechang 2004-01-08
  • 打赏
  • 举报
回复
也可以用MSComm控件的.
xiaoxiaolili 2004-01-08
  • 打赏
  • 举报
回复
发给我一份,谢谢:
mine_he@163.net
CityFantom 2004-01-07
  • 打赏
  • 举报
回复
对不起我有一段时间没上来了,请也给我发一件吧?谢谢 liyich@sina.com
foxbuilder 2003-12-26
  • 打赏
  • 举报
回复
发给我一份,谢谢:
pcjgp@163.net
rickleo 2003-12-26
  • 打赏
  • 举报
回复
如果一般应用,还是建议使用VB6的MSCOMM32控件。

这个控件比较方便,使用稳定,例子也很多。
JimexLee 2003-12-25
  • 打赏
  • 举报
回复
也发给我一份好吗?谢谢
chiyang@heshan.gov.cn
长沙侯 2003-12-23
  • 打赏
  • 举报
回复
请发给我一份,谢谢!!!
tommy_hou@163.com
hainer 2003-12-22
  • 打赏
  • 举报
回复

#Region "Structures"
' This is the DCB structure used by the calls to the Windows API.
<StructLayout(LayoutKind.Sequential, Pack:=1)> Private Structure DCB
Public DCBlength As Integer
Public BaudRate As Integer
Public Bits1 As Integer
Public wReserved As Int16
Public XonLim As Int16
Public XoffLim As Int16
Public ByteSize As Byte
Public Parity As Byte
Public StopBits As Byte
Public XonChar As Byte
Public XoffChar As Byte
Public ErrorChar As Byte
Public EofChar As Byte
Public EvtChar As Byte
Public wReserved2 As Int16
End Structure

' This is the CommTimeOuts structure used by the calls to the Windows API.
<StructLayout(LayoutKind.Sequential, Pack:=1)> Private Structure COMMTIMEOUTS
Public ReadIntervalTimeout As Integer
Public ReadTotalTimeoutMultiplier As Integer
Public ReadTotalTimeoutConstant As Integer
Public WriteTotalTimeoutMultiplier As Integer
Public WriteTotalTimeoutConstant As Integer
End Structure

' This is the CommConfig structure used by the calls to the Windows API.
<StructLayout(LayoutKind.Sequential, Pack:=1)> Private Structure COMMCONFIG
Public dwSize As Integer
Public wVersion As Int16
Public wReserved As Int16
Public dcbx As DCB
Public dwProviderSubType As Integer
Public dwProviderOffset As Integer
Public dwProviderSize As Integer
Public wcProviderData As Byte
End Structure

' This is the OverLapped structure used by the calls to the Windows API.
<StructLayout(LayoutKind.Sequential, Pack:=1)> Public Structure OVERLAPPED
Public Internal As Integer
Public InternalHigh As Integer
Public Offset As Integer
Public OffsetHigh As Integer
Public hEvent As Integer
End Structure
#End Region

#Region "Exceptions"

' This class defines a customized channel exception. This exception is
' raised when a NACK is raised.
Public Class CIOChannelException : Inherits ApplicationException
Sub New(ByVal Message As String)
MyBase.New(Message)
End Sub
Sub New(ByVal Message As String, ByVal InnerException As Exception)
MyBase.New(Message, InnerException)
End Sub
End Class

' This class defines a customized timeout exception.
Public Class IOTimeoutException : Inherits CIOChannelException
Sub New(ByVal Message As String)
MyBase.New(Message)
End Sub
Sub New(ByVal Message As String, ByVal InnerException As Exception)
MyBase.New(Message, InnerException)
End Sub
End Class

#End Region

#Region "Events"
' These events allow the program using this class to react to Comm Port
' events.
Public Event DataReceived(ByVal Source As rs232, ByVal DataBuffer() As Byte)
Public Event TxCompleted(ByVal Source As rs232)
Public Event CommEvent(ByVal Source As rs232, ByVal Mask As EventMasks)
#End Region

#Region "Constants"
' These constants are used to make the code clearer.
Private Const PURGE_RXABORT As Integer = &H2
Private Const PURGE_RXCLEAR As Integer = &H8
Private Const PURGE_TXABORT As Integer = &H1
Private Const PURGE_TXCLEAR As Integer = &H4
Private Const GENERIC_READ As Integer = &H80000000
Private Const GENERIC_WRITE As Integer = &H40000000
Private Const OPEN_EXISTING As Integer = 3
Private Const INVALID_HANDLE_VALUE As Integer = -1
Private Const IO_BUFFER_SIZE As Integer = 1024
Private Const FILE_FLAG_OVERLAPPED As Integer = &H40000000
Private Const ERROR_IO_PENDING As Integer = 997
Private Const WAIT_OBJECT_0 As Integer = 0
Private Const ERROR_IO_INCOMPLETE As Integer = 996
Private Const WAIT_TIMEOUT As Integer = &H102&
Private Const INFINITE As Integer = &HFFFFFFFF


#End Region
hainer 2003-12-22
  • 打赏
  • 举报
回复
Option Strict On

Imports System.Runtime.InteropServices
Imports System.Text
Imports System.Threading

' This class provides all the necessary support for communicating
' with the Comm Port (otherwise known as the Serial Port, or
' RS232 port).
Public Class RS232
' Declare the necessary class variables, and their initial values.
Private mhRS As Integer = -1 ' Handle to Com Port
Private miPort As Integer = 1 ' Default is COM1
Private miTimeout As Integer = 70 ' Timeout in ms
Private miBaudRate As Integer = 9600
Private meParity As DataParity = 0
Private meStopBit As DataStopBit = 0
Private miDataBit As Integer = 8
Private miBufferSize As Integer = 512 ' Buffers size default to 512 bytes
Private mabtRxBuf As Byte() ' Receive buffer
Private meMode As Mode ' Class working mode
Private mbWaitOnRead As Boolean
Private mbWaitOnWrite As Boolean
Private mbWriteErr As Boolean
Private muOverlapped As OVERLAPPED
Private muOverlappedW As OVERLAPPED
Private muOverlappedE As OVERLAPPED
Private mabtTmpTxBuf As Byte() ' Temporary buffer used by Async Tx
Private moThreadTx As Thread
Private moThreadRx As Thread
Private miTmpBytes2Read As Integer
Private meMask As EventMasks

#Region "Enums"

' This enumeration provides Data Parity values.
Public Enum DataParity
Parity_None = 0
Pariti_Odd
Parity_Even
Parity_Mark
End Enum

' This enumeration provides Data Stop Bit values.
' It is set to begin with a one, so that the enumeration values
' match the actual values.
Public Enum DataStopBit
StopBit_1 = 1
StopBit_2
End Enum

' This enumeration contains values used to purge the various buffers.
Private Enum PurgeBuffers
RXAbort = &H2
RXClear = &H8
TxAbort = &H1
TxClear = &H4
End Enum

' This enumeration provides values for the lines sent to the Comm Port
Private Enum Lines
SetRts = 3
ClearRts = 4
SetDtr = 5
ClearDtr = 6
ResetDev = 7 ' Reset device if possible
SetBreak = 8 ' Set the device break line.
ClearBreak = 9 ' Clear the device break line.
End Enum
' This enumeration provides values for the Modem Status, since
' we'll be communicating primarily with a modem.
' Note that the Flags() attribute is set to allow for a bitwise
' combination of values.
<Flags()> Public Enum ModemStatusBits
ClearToSendOn = &H10
DataSetReadyOn = &H20
RingIndicatorOn = &H40
CarrierDetect = &H80
End Enum

' This enumeration provides values for the Working mode
Public Enum Mode
NonOverlapped
Overlapped
End Enum

' This enumeration provides values for the Comm Masks used.
' Note that the Flags() attribute is set to allow for a bitwise
' combination of values.
<Flags()> Public Enum EventMasks
RxChar = &H1
RXFlag = &H2
TxBufferEmpty = &H4
ClearToSend = &H8
DataSetReady = &H10
ReceiveLine = &H20
Break = &H40
StatusError = &H80
Ring = &H100
End Enum
#End Region
hainer 2003-12-22
  • 打赏
  • 举报
回复
我使用RS232类,很好用,你试一试。
下面代码很长,我贴不上。告诉我你的MAil,发给你

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧