一段测试RS232用的VB代码

我现在把电脑和一个天平用一个RS232导线连在一起,但是我确定一下,所以我用下面的代码:PublicClassForm1PublicsptAsIO.Ports.Seria... 我现在把电脑和一个天平用一个RS232导线连在一起,但是我确定一下,所以我用下面的代码:

Public Class Form1
Public spt As IO.Ports.SerialPort = My.Computer.Ports.OpenSerialPort("COM1")

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim incoming As String
incoming = spt.ReadLine()
TextBox1.Text = incoming
End Sub
End Class

我觉得如果天平和电脑连上,应该会有数据送到电脑,所以只是想看看是不是有数据过来,不过每次我一运行这个程序,它就不会停下来,请问到底是怎么回事?

先谢谢大家了
展开
 我来答
萍水e相逢
2010-06-21 · TA获得超过3万个赞
知道大有可为答主
回答量:5.9万
采纳率:46%
帮助的人:1.5亿
展开全部
cnatsa71 有 个 例子

下面的代码示例演示如何使用 SerialPort 类以允许两位用户分别在两台通过 NULL 调制解调器电缆连接的独立计算机上聊天。本示例中,在聊天之前将提示用户输入端口设置和用户名。这两台计算机必须同时执行该程序才能实现本示例的全部功能。

Imports System
Imports System.IO.Ports
Imports System.Threading

Public Class PortChat
Shared _continue As Boolean
Shared _serialPort As SerialPort

Public Shared Sub Main()
Dim name As String
Dim message As String
Dim sComparer As StringComparer = StringComparer.OrdinalIgnoreCase
Dim readThread As Thread = New Thread(AddressOf Read)

' Create a new SerialPort object with default settings.
_serialPort = New SerialPort()

' Allow the user to set the appropriate properties.
_serialPort.PortName = SetPortName(_serialPort.PortName)
_serialPort.BaudRate = SetPortBaudRate(_serialPort.BaudRate)
_serialPort.Parity = SetPortParity(_serialPort.Parity)
_serialPort.DataBits = SetPortDataBits(_serialPort.DataBits)
_serialPort.StopBits = SetPortStopBits(_serialPort.StopBits)
_serialPort.Handshake = SetPortHandshake(_serialPort.Handshake)

' Set the read/write timeouts
_serialPort.ReadTimeout = 500
_serialPort.WriteTimeout = 500

_serialPort.Open()
_continue = True
readThread.Start()

Console.Write("Name: ")
name = Console.ReadLine()

Console.WriteLine("Type QUIT to exit")

While (_continue)
message = Console.ReadLine()

If sComparer.Equals("quit", message) Then
_continue = False
Else
_serialPort.WriteLine( _
String.Format("<{0}>: {1}", name, message))
End If
end while

readThread.Join()
_serialPort.Close()
End Sub

Public Shared Sub Read()
While (_continue)
Try
Dim message As String = _serialPort.ReadLine()
Console.WriteLine(message)
Catch ex As TimeoutException
' Do nothing
End Try
End While
End Sub

Public Shared Function SetPortName(ByVal defaultPortName As String) As String
Dim newPortName As String

Console.WriteLine("Available Ports:")
Dim s As String
For Each s In SerialPort.GetPortNames()
Console.WriteLine(" {0}", s)
Next s

Console.Write("COM port({0}): ", defaultPortName)
newPortName = Console.ReadLine()

If newPortName = "" Then
newPortName = defaultPortName
End If
Return newPortName
End Function

Public Shared Function SetPortBaudRate(ByVal defaultPortBaudRate As Integer) As Integer
Dim newBaudRate As String

Console.Write("Baud Rate({0}): ", defaultPortBaudRate)
newBaudRate = Console.ReadLine()

If newBaudRate = "" Then
newBaudRate = defaultPortBaudRate.ToString()
End If

Return Integer.Parse(newBaudRate)
End Function

Public Shared Function SetPortParity(ByVal defaultPortParity As Parity) As Parity
Dim newParity As String

Console.WriteLine("Available Parity options:")
Dim s As String
For Each s In [Enum].GetNames(GetType(Parity))
Console.WriteLine(" {0}", s)
Next s

Console.Write("Parity({0}):", defaultPortParity.ToString())
newparity = Console.ReadLine()

If newparity = "" Then
newparity = defaultPortParity.ToString()
End If

Return CType([Enum].Parse(GetType(Parity), newParity), Parity)
End Function

Public Shared Function SetPortDataBits(ByVal defaultPortDataBits As Integer) As Integer
Dim newDataBits As String

Console.Write("Data Bits({0}): ", defaultPortDataBits)
newDataBits = Console.ReadLine()

If newDataBits = "" Then
newDataBits = defaultPortDataBits.ToString()
End If

Return Integer.Parse(newDataBits)
End Function

Public Shared Function SetPortStopBits(ByVal defaultPortStopBits As StopBits) As StopBits
Dim newStopBits As String

Console.WriteLine("Available Stop Bits options:")
Dim s As String
For Each s In [Enum].GetNames(GetType(StopBits))
Console.WriteLine(" {0}", s)
Next s

Console.Write("Stop Bits({0}):", defaultPortStopBits.ToString())
newStopBits = Console.ReadLine()

If newStopBits = "" Then
newStopBits = defaultPortStopBits.ToString()
End If

Return CType([Enum].Parse(GetType(StopBits), newStopBits), StopBits)
End Function

Public Shared Function SetPortHandshake(ByVal defaultPortHandshake As Handshake) As Handshake
Dim newHandshake As String

Console.WriteLine("Available Handshake options:")
Dim s As String
For Each s In [Enum].GetNames(GetType(Handshake))
Console.WriteLine(" {0}", s)
Next s

Console.Write("Stop Bits({0}):", defaultPortHandshake.ToString())
newHandshake = Console.ReadLine()

If newHandshake = "" Then
newHandshake = defaultPortHandshake.ToString()
End If

Return CType([Enum].Parse(GetType(Handshake), newHandshake), Handshake)
End Function
End Class
aspd199
2010-06-20 · TA获得超过906个赞
知道小有建树答主
回答量:865
采纳率:50%
帮助的人:488万
展开全部
直接连电脑才是COM1 通过RS232连接一般都是COM3的

参考资料: VB探讨超级群 48029944

已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式