如何在VB.NET中从串口RS232接收数据? [英] How to Receive data from Serial Port RS232 in VB.NET?

查看:165
本文介绍了如何在VB.NET中从串口RS232接收数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有一个应用程序,其中有一台机器(一个容量为升的浴缸)在显示板上。机器中有串口RS232。所以我想要做的是通过RS232端口我直接想在我的应用程序中在一个文本框中显示它。那么如何从VB.NET中的RS232端口访问数据(我使用的是Visual Studio 2008)。我知道有一个名为System.IO的类可用。如果有人知道这个请帮帮我!!!!还想知道如何测试它工作与否的代码,而不是通过串口RS232将其附加到机器上?





谢谢你

Hi to all,

I have one application in which there is one machine (a tub which gives amount of liter) on display board. It is having Serial Port RS232 in machine. So what i would thinking to do is that through RS232 port i directly want to display it in my application in one text box. So how can i access data from RS232 port in VB.NET (I am using Visual Studio 2008). I know there is Class called System.IO available. If anybody know this please help me!!!! Also want to know that how to test the code that it is working or not, without attaching it to machine through a serial port RS232?


Thank you

推荐答案

你可以自己轻松搜索。以下是MSDN的逐步介绍:

http://support.microsoft .com / kb / 823179 / EN-US [ ^ ]
You could have easily googled that on your own. Here is a step by step introduction from the MSDN:
http://support.microsoft.com/kb/823179/EN-US[^]


'串口与VB.net 2010 Express Edition接口

'版权所有(C)2010 Richard Myrick T. Arellaga
'

'此程序是免费软件:您可以根据已发布的GNU通用公共许可证条款重新分发和/或修改

'通过

'自由软件基金会,无论是版本3的许可证,还是

'(根据您的选择)任何更高版本。

'

'这个程序是分发的,希望它有用,

'但没有任何保证;甚至没有暗示的保证

'适销性或特定用途的适用性。有关更多详细信息,请参阅

'GNU通用公共许可证。

'

'您应该已收到GNU通用公共许可证的副本

'以及这个程序。如果没有,请参阅。





进口系统

进口System.ComponentModel

Imports System.Threading

Imports System.IO.Ports

公共类frmMain

Dim myPort As Array在系统上检测到的COM端口将存储在这里

委托Sub SetTextCallback(ByVal [text] As String)'添加以防止在接收数据期间出现线程错误



Private Sub frmMain_Load(ByVal sender As System.Object,ByVal e As System.EventArgs)处理MyBase.Load

'当我们的表单加载时,自动检测系统中的所有串口并填充cmbPort组合框。

myPort = IO.Ports.SerialPort.GetPortNames()'获取所有可用的COM端口

cmbBaud.Items.Add(9600)'将cmbBaud组合框填充到常用波特率二手

cmbBaud.Items.Add(19200)

cmbBaud.Items.Add(38400)

cmbB aud.Items.Add(57600)

cmbBaud.Items.Add(115200)



for i = 0 To UBound(myPort)

cmbPort.Items.Add(myPort(i))

下一页

cmbPort.Text = cmbPort.Items.Item(0)'设置cmbPort检测到第一个COM端口的文本

cmbBaud.Text = cmbBaud.Items.Item(0)'将cmbBaud文本设置为列表中的第一个波特率



btnDisconnect.Enabled = False'最初断开连接按钮已禁用



结束次级



Private Sub btnConnect_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)处理btnConnect.Click

SerialPort1.PortName = cmbPort.Text'在启动时将SerialPort1设置为选定的COM端口

SerialPort1.BaudRate = cmbBaud.Text'将波特率设置为所选值



'其他串行端口属性

SerialPort1.Parity = IO.Ports.Parity.None

SerialPort1.StopBits = IO.Ports.StopBits.One

SerialPort1.DataBits = 8 '打开我们的串口

SerialPort1.Open()



btnConnect.Enabled = False'禁用连接按钮

btnDisconnect.Enabled = True'和启用断开按钮



End Sub



Private Sub btnDisconnect_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)处理btnDisconnect.Click

SerialPort1.Close()'关闭我们的串口



btnConnect.Enabled = True

btnDisconnect.Enabled = False

End Sub



Private Sub btnSend_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)处理btnSend.Click

SerialPort1.Write(txtTransmit.Text& vbCr)

'txtText中包含的文本将被发送到串口ascii

'加上回车键(回车键)如果回车符号可以省略另一端不需要它

End Sub



Private Sub SerialPort1_DataReceived(ByVal sender As Object,ByVal e As System.IO.Ports .SerialDataReceivedEventArgs)处理SerialPort1.DataReceived

ReceivedText(SerialPort1.ReadExisting())'每次在serialPort收到数据时自动调用

End Sub

Private Sub ReceivedText(ByVal [text] As String)

'将创建线程的ID与调用线程的ID进行比较

如果是Me.rtbReceived。 InvokeRequired然后

Dim x As New SetTextCallback(AddressOf ReceivedText)

Me.Invoke(x,New Object(){(text)})

Else

Me.rtbReceived .Text&= [text]

结束如果

结束Sub



Private Sub cmbPort_SelectedIndexChanged(ByVal sender As System.Object,ByVal e As System.EventArgs)处理cmbPort.SelectedIndexChanged

如果SerialPort1.IsOpen = False那么

SerialPort1.PortName = cmbPort.Text'弹出一个消息框用户,如果他正在更换端口

Else'没有先断开连接。

MsgBox(仅在端口关闭时有效,vbCritical)

结束如果

End Sub



Private Sub cmbBaud_SelectedIndexChanged(ByVal sender As System.Object,ByVal e As System.EventArgs)处理cmbBaud.SelectedIndexChanged

如果SerialPort1.IsOpen = False那么

SerialPort1.BaudRate = cmbBaud.Text'如果他正在改变波特率,则向用户弹出一个消息框

其他'首先没有断开连接。

MsgBox(仅在端口关闭时有效,vbCritical)

结束如果

结束子



Private Sub rtbReceived_TextChanged(ByVal sender As System.Object,ByVal e As System.EventArgs)Handles rtbReceived.TextChanged



结束Sub



Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)处理Button1.Click

rtbReceived。文字=



结束次级







Private Sub txtTransmit_TextChanged(ByVal sender As System.Object,ByVal e As System.EventArgs)Handles txtTransmit.TextChanged



End Sub



Private Sub Timer1_Tick(ByVal sender As System.Object,ByVal e As System.EventArgs)处理Timer1.Tick

SerialPort1.Write(txtTransmit.Text&vb Cr)

结束子

结束班
'Serial Port Interfacing with VB.net 2010 Express Edition
'Copyright (C) 2010 Richard Myrick T. Arellaga
'
'This program is free software: you can redistribute it and/or modify
'it under the terms of the GNU General Public License as published by
'the Free Software Foundation, either version 3 of the License, or
'(at your option) any later version.
'
'This program is distributed in the hope that it will be useful,
'but WITHOUT ANY WARRANTY; without even the implied warranty of
'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
'GNU General Public License for more details.
'
' You should have received a copy of the GNU General Public License
' along with this program. If not, see .


Imports System
Imports System.ComponentModel
Imports System.Threading
Imports System.IO.Ports
Public Class frmMain
Dim myPort As Array 'COM Ports detected on the system will be stored here
Delegate Sub SetTextCallback(ByVal [text] As String) 'Added to prevent threading errors during receiveing of data

Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'When our form loads, auto detect all serial ports in the system and populate the cmbPort Combo box.
myPort = IO.Ports.SerialPort.GetPortNames() 'Get all com ports available
cmbBaud.Items.Add(9600) 'Populate the cmbBaud Combo box to common baud rates used
cmbBaud.Items.Add(19200)
cmbBaud.Items.Add(38400)
cmbBaud.Items.Add(57600)
cmbBaud.Items.Add(115200)

For i = 0 To UBound(myPort)
cmbPort.Items.Add(myPort(i))
Next
cmbPort.Text = cmbPort.Items.Item(0) 'Set cmbPort text to the first COM port detected
cmbBaud.Text = cmbBaud.Items.Item(0) 'Set cmbBaud text to the first Baud rate on the list

btnDisconnect.Enabled = False 'Initially Disconnect Button is Disabled

End Sub

Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
SerialPort1.PortName = cmbPort.Text 'Set SerialPort1 to the selected COM port at startup
SerialPort1.BaudRate = cmbBaud.Text 'Set Baud rate to the selected value on

'Other Serial Port Property
SerialPort1.Parity = IO.Ports.Parity.None
SerialPort1.StopBits = IO.Ports.StopBits.One
SerialPort1.DataBits = 8 'Open our serial port
SerialPort1.Open()

btnConnect.Enabled = False 'Disable Connect button
btnDisconnect.Enabled = True 'and Enable Disconnect button

End Sub

Private Sub btnDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisconnect.Click
SerialPort1.Close() 'Close our Serial Port

btnConnect.Enabled = True
btnDisconnect.Enabled = False
End Sub

Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
SerialPort1.Write(txtTransmit.Text & vbCr)
'The text contained in the txtText will be sent to the serial port as ascii
'plus the carriage return (Enter Key) the carriage return can be ommitted if the other end does not need it
End Sub

Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
ReceivedText(SerialPort1.ReadExisting()) 'Automatically called every time a data is received at the serialPort
End Sub
Private Sub ReceivedText(ByVal [text] As String)
'compares the ID of the creating Thread to the ID of the calling Thread
If Me.rtbReceived.InvokeRequired Then
Dim x As New SetTextCallback(AddressOf ReceivedText)
Me.Invoke(x, New Object() {(text)})
Else
Me.rtbReceived.Text &= [text]
End If
End Sub

Private Sub cmbPort_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbPort.SelectedIndexChanged
If SerialPort1.IsOpen = False Then
SerialPort1.PortName = cmbPort.Text 'pop a message box to user if he is changing ports
Else 'without disconnecting first.
MsgBox("Valid only if port is Closed", vbCritical)
End If
End Sub

Private Sub cmbBaud_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbBaud.SelectedIndexChanged
If SerialPort1.IsOpen = False Then
SerialPort1.BaudRate = cmbBaud.Text 'pop a message box to user if he is changing baud rate
Else 'without disconnecting first.
MsgBox("Valid only if port is Closed", vbCritical)
End If
End Sub

Private Sub rtbReceived_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rtbReceived.TextChanged

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
rtbReceived.Text = " "

End Sub



Private Sub txtTransmit_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtTransmit.TextChanged

End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
SerialPort1.Write(txtTransmit.Text & vbCr)
End Sub
End Class


这篇关于如何在VB.NET中从串口RS232接收数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆