首先,我不太擅长编程,我试图在我的代码中尽可能地解释清楚。我一直在尝试编写一个程序,使用户能够使用简单的定制红外调制解调器通过串行端口聊天(接收和传输)消息。我已经构建了调制解调器,现在尝试编写自己的简单程序,而不是使用终端或其他已编写的程序(即 Tera Term)。顺便说一句,我在 Windows 7 上并使用 Microsoft Visual Express 2010 环境
我根据本网站上的教程编写了以下代码。(以防万一) MSDN on Serial Port
问题(截至目前)组合框 cmbComPort 似乎没有捕获我计算机上的任何可用端口。我怀疑可能没有!所以我检查了一下,这就是我发现的。基于此,我假设我的计算机上有一个串行端口,因此这不是问题的根源(否则请告诉我,因为我不太确定)。我运行调试并弹出一条错误消息。然后我构建程序来测试它。两个错误消息都附在下面
问题 1. 似乎下面的代码没有捕获可用端口并将它们存储在 myPort 数组中。
'procedure to detect all available ports and store them in the myPort array
For Each port_name As String In IO.Ports.SerialPort.GetPortNames
Dim myPort As New IO.Ports.SerialPort(port_name)
If myPort.IsOpen = True Then
cmbComPort.Items.Add(port_name)
End If
Next
我想知道端口本身是否有问题并且未被检测到。但是,使用下面的代码,我能够确认 COM1 存在并且正在工作。
Imports System
Imports System.IO.Ports
Module SerialPortExample
Sub Main()
' Get a list of serial port names.
Dim ports As String() = SerialPort.GetPortNames()
Console.WriteLine("The following serial ports were found:")
' Display each port name to the console.
Dim port As String
For Each port In ports
Console.WriteLine(port)
Next port
Console.ReadLine()
End Sub
End Module
这是控制台输出的内容 -
找到以下串行端口:COM1
所以,港口就在那里。问题可能在于(我不完全确定)这部分代码?
'procedure to detect all available ports and store them in the myPort array
For Each port_name As String In IO.Ports.SerialPort.GetPortNames
Dim myPort As New IO.Ports.SerialPort(port_name)
cmbComPort.Items.Add(port_name)
Next
无论如何我可以在这一行之后检查 myPort 数组的内容吗?以及 cmbComPort 的项目?
还有一件事,这是一个接受任何额外功能的项目(编写我自己的程序绝对是其中之一)。如果你们能对这些功能提出任何想法,我将不胜感激,无论是与串行端口通信还是程序接口本身有关。我可以想到一些,例如将聊天保存到文件、加载聊天文件、帮助/教程文件 - 一旦我发现上述错误,所有这些都将被实施。此外,我在想无论如何我可以绘制一个“泡泡聊天”来显示对话而不是纯富文本框吗?那样就好了。
提前致谢!:D 项目文件
代码:
'Chatty Raffy Version 1.3
'This is a simple program to demonstrate Serial Ports communication via a simple custom made IR Modem.
Imports System
Imports System.ComponentModel
Imports System.Threading
Imports System.IO.Ports
Public Class frmMain
Dim myPort As Array 'an array to store list of available ports
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)
'Fill up the cmbBaudRate Combo box to common baud rates used
cmbBaudRate.Items.Add(9600)
cmbBaudRate.Items.Add(19200)
cmbBaudRate.Items.Add(38400)
cmbBaudRate.Items.Add(57600)
cmbBaudRate.Items.Add(115200)
'procedure to detect all available ports and store them in the myPort array
For Each port_name As String In IO.Ports.SerialPort.GetPortNames
Dim myPort As New IO.Ports.SerialPort(port_name)
If myPort.IsOpen = True Then
cmbComPort.Items.Add(port_name)
End If
Next
'initiate the combo boxes
cmbComPort.SelectedIndex = 0 'set cmbComPort text to the first COM port detected
cmbBaudRate.SelectedIndex = 0 'set cmbBaudRate text to the first Baud rate on the list
cmbParity.SelectedIndex = 0 'set cmbParity text to the first Baud rate on the list
cmbStopBit.SelectedIndex = 0 'set cmbStopBit text to the first Baud rate on the list
'btnDisconnect.Enabled = False 'disable the disconnect button
End Sub
'open the selected serial port and start the connection
Private Sub btnConnect_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
SerialPort1.PortName = cmbComPort.Text 'set Serial Port to the selected COM port
SerialPort1.BaudRate = cmbBaudRate.Text 'set Baud rate to the selected value Baud rate
SerialPort1.Parity = cmbParity.Text 'set parity setting to the selected value
SerialPort1.StopBits = cmbStopBit.Text 'set stop bit setting to the selected value
SerialPort1.DataBits = 8 'use the default 8 bit for data bit length
SerialPort1.Open() 'open the chosen serial port
btnConnect.Enabled = False 'disable the Connect button
btnDisconnect.Enabled = True 'enable the Disconnect button
picboxDisconnect.Visible = False 'disable the disconnect picture
picboxConnect.Visible = True 'enable the disconnect picture
End Sub
'close the serial port currently in used, hence closing the connection
Private Sub btnDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisconnect.Click
SerialPort1.Close() 'close the used Serial Port
btnConnect.Enabled = True 'esable the Connect button
btnDisconnect.Enabled = False 'disable the Disconnect button
picboxDisconnect.Visible = True 'enable the 'disconnect' picture
picboxConnect.Visible = False 'disable the 'connect' picture
End Sub
'send the text contained in the txtText to the serial port in ASCII using the 'SEND' key
Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
SerialPort1.Write(txtTransmit.Text & vbCr)
End Sub
'detect data at the serial port and call ReceivedText automatically
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs)
ReceivedText(SerialPort1.ReadExisting())
End Sub
'read incoming messages at serial port once data is detected
Private Sub ReceivedText(ByVal [text] As String)
'compare 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
'this section prevents user from making any changes to the current connection without disconnecting
Private Sub cmbComPort_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
If SerialPort1.IsOpen = False Then
SerialPort1.PortName = cmbComPort.Text
Else
'pop an error message if user try to change port without closing the current port in use
MsgBox("Please close the currently used port before making any changes to the connection setting", vbCritical)
End If
End Sub
Private Sub cmbBaudRate_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbBaudRate.SelectedIndexChanged
If SerialPort1.IsOpen = False Then
SerialPort1.BaudRate = cmbBaudRate.Text
Else
'pop an error message if user try to change Baud rate without closing the current port in use
MsgBox("Please close the currently used port before making any changes to the connection setting", vbCritical)
End If
End Sub
Private Sub cmbParity_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbBaudRate.SelectedIndexChanged
If SerialPort1.IsOpen = False Then
SerialPort1.Parity = cmbParity.Text
Else
'pop an error message if user try to change Baud rate without closing the current port in use
MsgBox("Please close the currently used port before making any changes to the connection setting", vbCritical)
End If
End Sub
Private Sub cmbStopBit_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbBaudRate.SelectedIndexChanged
If SerialPort1.IsOpen = False Then
SerialPort1.StopBits = cmbStopBit.Text
Else
'pop an error message if user try to change Baud rate without closing the current port in use
MsgBox("Please close the currently used port before making any changes to the connection setting", vbCritical)
End If
End Sub
End Class