- 最後登錄
- 2023-5-4
- 在線時間
- 775 小時
- 註冊時間
- 2011-9-27
- 閱讀權限
- 20
- 精華
- 0
- UID
- 10356713
- 帖子
- 1549
- 積分
- 367 點
- 潛水值
- 31991 米
| 若對尊貴或贊助會員有任何疑問,歡迎向我們查詢。我們的即時通或MSN: admin@eyny.com 以下是完整的程式碼- Imports System.IO.Ports
- Imports System.Text
- Public Class Form1
- '**************************************************************
- '表單的Load事件中先將所有的通訊埠先列出來
- '將通訊埠排序,並將第一個通訊埠設為預設值
- '**************************************************************
- Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
- For Each sp As String In SerialPort.GetPortNames()
- cmb1.Items.Add(sp)
- Next
- cmb1.Sorted = True
- cmb1.SelectedIndex = 0
- End Sub
- '**************************************************************
- '『開啟通訊埠』按鈕的Click事件
- '此事件將設定通訊埠參數,並開啟通訊埠
- '**************************************************************
- Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click
- Dim EncodeType As Encoding
- RS232.PortName = cmb1.SelectedItem.ToString
- RS232.BaudRate = 2400
- RS232.Parity = Parity.None
- RS232.DataBits = 8
- RS232.StopBits = StopBits.One
- '檢查編碼方式
- If rbtnCode1.Checked Then EncodeType = Encoding.UTF8
- If rbtnCode2.Checked Then EncodeType = Encoding.UTF7
- If rbtnCode3.Checked Then EncodeType = Encoding.ASCII
- If rbtnCode4.Checked Then EncodeType = Encoding.Unicode
- If Not RS232.IsOpen Then
- RS232.Open()
- Timer1.Interval = 100
- Timer1.Enabled = True
- Else
- MsgBox("通訊埠開啟錯誤(通訊埠已被開啟)", MsgBoxStyle.Critical Or MsgBoxStyle.OkCancel)
- End
- End If
- End Sub
- '**************************************************************
- '『關閉通訊埠』按鈕的Click事件
- '以Close方法關閉通訊埠,並釋放物件所佔用的資源
- '**************************************************************
- Private Sub btn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2.Click
- If RS232 Is Nothing OrElse Not RS232.IsOpen Then
- MsgBox("通訊埠尚未開啟", MsgBoxStyle.Critical Or MsgBoxStyle.OkCancel)
- Else
- Timer1.Enabled = False
- RS232.Close()
- End If
- End Sub
- '**************************************************************
- '『結束程式』按鈕 的Click事件
- '此事件將設定通訊埠參數,並開啟通訊埠
- '**************************************************************
- Private Sub btn4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn4.Click
- If Not RS232 Is Nothing Then
- If RS232.IsOpen Then RS232.Close()
- End If
- End
- End Sub
- '**************************************************************
- '計時器控制項的Timer事件
- '將接收的動作放於其中,只要有資料就會被接收進來
- '**************************************************************
- Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
- Dim instring As String
- Dim dataStr As String
- Dim Sep(0) As Char
- Dim dataSep() As String, i As Integer, j As Integer
- 'Dim x, x1
- instring = ""
- Try
- RS232.ReadTimeout = 1000
- instring = RS232.ReadExisting()
- If instring.Length = 0 Then
- Exit Sub
- Else
- rxdmix_16.Text += instring
- i = rxdmix_16.TextLength
- Textlength.Text = i
- dataStr = rxdmix_16.Text
- Sep(0) = "," '訂定分離字元陣列
- dataSep = dataStr.Split(Sep) '將字串依分離字元分離
- rxdmix_16.Text = ""
- For j = dataSep.GetLowerBound(0) To dataSep.GetUpperBound(0)
- rxdmix_16.Text &= dataSep(j)
- rxdcounter.Text &= dataSep(2 * j)
- rxdvolt.Text &= dataSep((2 * j) + 1)
- 'Dim x, x1
- 'x = "&H" + dataSep(j)
- 'x1 = CLng(x)
- 'rxdmix_10.Text = x1
- Next
- End If
- Catch ex As Exception
- MessageBox.Show("讀取錯誤:" + ex.ToString, "錯誤通知", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
- End Try
- End Sub
- '**************************************************************
- '通訊埠的KeyPress事件,在此不讓使用者輸入資料
- '**************************************************************
- Private Sub txd1_keypress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txd1.KeyPress
- If e.KeyChar = vbCr Then
- If Not RS232 Is Nothing Then
- RS232.Write(txd1.Text)
- End If
- End If
- End Sub
- '**************************************************************
- '傳送文字框的KeyPress事件,當檢測到Enter按鍵時
- '將所有的資料送出
- '**************************************************************
- Private Sub cmb1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles cmb1.KeyPress
- e.KeyChar = ChrW(0)
- End Sub
- End Class
複製代碼 以下是我的介面及錯誤通知
紅色框框裡的就是亂抓的資料... |
附件: 你需要登錄才可以下載或查看附件。沒有帳號?註冊 |