前端之家收集整理的这篇文章主要介绍了
vb.net 串口数据采集类,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
Imports System.IO.Ports
Imports System.Text
''' <summary>
''' 通讯仪表为称重仪表
''' </summary>
''' <remarks></remarks>
Public Class ClsIC
Dim WithEvents Rs232 As SerialPort
Delegate Sub SeTextCallBack(ByVal InputSting As String)
Const conBaudRate As Integer = 19200
Const conParity As IO.Ports.Parity = Parity.None
Const conDatabit As Integer = 8
Const conStopbit As IO.Ports.StopBits = StopBits.One
Const conPortName As String = "COM3"
Private mBaudRate As Integer
Private mParity As IO.Ports.Parity
Private mDataBit As Integer
Private mStopBit As IO.Ports.StopBits
Private mPortName As String
Private mvarValue As Integer
Public Enum YiBiaoType
YuanYangGu = 1 '托利多仪表
GM = 2 '杰曼
JinZhong = 3 '金钟
End Enum
''' <summary>
''' 返回重量值
''' </summary>
''' <value>只读属性</value>
''' <returns>WeightValue</returns>
''' <remarks></remarks>
ReadOnly Property WeightValue() As Integer
Get
Return mvarValue
End Get
End Property
''' <summary>
''' 设置或返回仪表类型
''' </summary>
''' <remarks></remarks>
Private mvarYibiaoType As Integer = YiBiaoType.YuanYangGu
Public Property Type() As Integer
Get
Return mvarYibiaoType
End Get
Set(ByVal value As Integer)
mvarYibiaoType = value
End Set
End Property
''' <summary>
''' 初始化串口,默认19200,8,n,1,COM3
''' </summary>
''' <remarks></remarks>
Public Sub New()
mBaudRate = conBaudRate
mParity = conParity
mDataBit = conDatabit
mStopBit = conStopbit
mPortName = conPortName
End Sub
''' <summary>
''' 初始化串口
''' </summary>
''' <param name="pPorts"></param>
''' <param name="pBaudRate"></param>
''' <param name="pParity"></param>
''' <param name="pDatabit"></param>
''' <param name="pStopbit"></param>
''' <remarks></remarks>
Public Sub New(ByVal pPorts As String,ByVal pBaudRate As Integer,ByVal pParity As IO.Ports.Parity,ByVal pDatabit As Integer,ByVal pStopbit As IO.Ports.StopBits)
mBaudRate = pBaudRate
mParity = pParity
mDataBit = pDatabit
mStopBit = pStopbit
mPortName = pPorts
End Sub
''' <summary>
''' 打开串口,可以指定阀值,默认1
''' </summary>
''' <param name="pReadbyteThrd"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Function Open(Optional ByVal pReadbyteThrd As Integer = 1) As Boolean
Try
Rs232 = New IO.Ports.SerialPort(mPortName,mBaudRate,mParity,mDataBit,mStopBit)
Rs232.Encoding = Encoding.ASCII
If Not Rs232.IsOpen Then
Rs232.Open()
Rs232.ReceivedBytesThreshold = pReadbyteThrd
Return True
End If
Catch ex As Exception
Throw New Exception(ex.Message & "Open Com error!")
Return False
End Try
End Function
''' <summary>
''' 接收数据
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub Rs232_DataReceived(ByVal sender As Object,ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles Rs232.DataReceived
Try
If e.EventType <> SerialData.Chars Then Exit Sub
Dim Indata As String = Rs232.ReadTo(vbCr)
If mvarYibiaoType = YiBiaoType.YuanYangGu Then
mvarValue = Val(Indata.Substring(4,6)) '将收到托利多仪表的数据入接收文字框中
ElseIf mvarYibiaoType = YiBiaoType.GM Then
mvarValue = Val(Indata.Substring(4,7)) '将收到杰曼仪表的数据入接收文字框中
ElseIf mvarYibiaoType = YiBiaoType.JinZhong Then '将收到金钟仪表的数据入接收文字框中
End If
Catch ex As Exception
MsgBox(ex.Message & "Read Com error!")
End Try
End Sub
''' <summary>
''' 关闭串口
''' </summary>
''' <remarks></remarks>
Public Sub Close()
Try
If Rs232 IsNot Nothing OrElse Rs232.IsOpen Then
Rs232.Close()
Rs232.Dispose()
End If
Catch ex As Exception
Throw New Exception(ex.Message & "Close com Error!")
End Try
End Sub
Public Function Write(ByVal Writestr As String) As Boolean
Try
Rs232.Write(Writestr)
Return True
Catch ex As Exception
Return False
MsgBox(ex.Message & "Write Com error!")
End Try
End Function
End Class