2012-08-15 177 views
-1

首先,我不擅長編程,我儘量在代碼中儘可能地說明問題。我一直在嘗試編寫一個程序,使用戶能夠使用簡單的定製IR調制解調器通過串行端口聊天(接收&發送)消息。我已經構建了調制解調器,現在正試圖編寫我自己的簡單程序,而不是使用終端或其他已編寫的程序(即Tera Term)。順便說一句,我在Windows 7上,並使用Microsoft Visual Express 2010環境使用VB.Net的串行端口通信(端口不顯示)

我已經寫了下面的代碼,基於本網站的教程。 (把它在這裏,以防萬一) 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 Project files

碼:

'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 
+1

你爲什麼要檢查'IsOpen'?我不希望對於剛剛構建的任何*'SerialPort'對象來說這是真的。 – 2012-08-15 10:19:44

+0

我同意@Damien_The_Unbeliever,你不應該只列出Open COM端口。事實上,如果一個端口已經打開,那麼你的應用程序將無法打開它。好像你應該列出那些沒有打開的。 – tcarvin 2012-08-15 11:45:05

+1

「定製紅外調制解調器」聽起來像是一個非常大的失敗鯨魚。你也有一個自定義的驅動程序嗎?驅動程序是否模擬串行端口?如果您沒有看到「設備管理器」中列出的端口,那麼您無法使此代碼正常工作。 – 2012-08-15 12:57:18

回答

-1

還好吧,我也有這個意思用一個端口是「開放」或之前「封閉」 missunderstanding,是由我的朋友指出。從那時起,我固定的代碼的那部分

'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 

@hans:其實電路中使用的終端IR調制解調器(與Windows到來)在實驗室的計算機已經工作。我已經使用這個代碼,事實上,串口做出口和工作

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的項目?

+0

您可能應該編輯您的問題以包含此新信息,而不是將其發佈爲您的問題的答案(考慮它不是答案)。 – Jack 2012-08-15 20:04:27

+0

好的,我要刪除這篇文章,但我認爲最好留在這裏。我編輯了這個問題。謝謝 – Cache 2012-08-16 14:00:11