visual basicnet其實比visual C#更好用,作為嵌入式開發人員,用basic入門更簡單,可以很快做出調試產品工具。附件是完整項目
這個是電腦串口尋找子函數
Private Sub com_channel_get(ByVal getcom1 As Integer)
Dim getcom As Integer
Dim ports As String() = SerialPort.GetPortNames() '必須用命名空間,用SerialPort,獲取計算機的有效串口
Dim port As String
For Each port In ports
Console.WriteLine(port)
Next port
ComboBox1.Items.Clear()
ComboBox1.Text = ""
For Each port In ports
ComboBox1.Items.Add(port) '向combobox中添加項
Next port
getcom = ComboBox1.Items.Count '讀ComboBox1的數值
Dim comnum As Integer
Dim textcom As String
ComboBox1.Enabled = False
' Exit Sub
Dim mBaudRate As Integer
Dim mParity As IO.Ports.Parity
Dim mDataBit As Integer
Dim mStopbit As IO.Ports.StopBits
Dim mPortName As String = "COM1"
mBaudRate = 9600 '比特率
mParity = Parity.None '校驗位檢查設定
mDataBit = 8 '數據位設定值
mStopbit = StopBits.One '停止位設定值
For comnum = 0 To getcom - 1 Step 1
ComboBox1.SelectedIndex() = comnum '第一個端口為默認
mPortName = ComboBox1.GetItemText(ComboBox1.SelectedItem) '欲開啟的通訊端口
' mPortName = "COM3"
Next comnum
RS232 = New IO.Ports.SerialPort(mPortName, mBaudRate, mParity, mDataBit, mStopbit)
If RS232.IsOpen = True Then '尚未開啟
RS232.Close() '開啟通訊端口
End If
Threading.Thread.Sleep(50)
Try
If RS232.IsOpen = False Then '尚未開啟
RS232.Open() '開啟通訊端口
RS232.ReceivedBytesThreshold = 1 '設置引發事件的門限值
End If
Catch
Try
If RS232.IsOpen = True Then '尚未開啟
RS232.Close() '開啟通訊端口
End If
Catch
End Try
End Try
Exit Sub
Dim hexBytes(3) As Byte
hexBytes(0) = &H6A
'RS232.Write(hexBytes, 0, 1)
RS232.Write("AT+DISC" + Chr(&HD) + Chr(&HA))
Threading.Thread.Sleep(500)
' Dim rxdata As String = RS232_Data(3)
Label1.Text = "通信未連接"
Label1.ForeColor = Color.Red
Dim textcompare As String = ""
If (Len(gl_string) > 4) Then
textcompare = Mid(gl_string, 1, 5)
End If
'ERROR OR OK+DISC
If ((textcompare = "ERROR") Or (textcompare = "OK+DI")) Then
Label1.Text = "通信已連接"
Label1.ForeColor = Color.Green
setrxnum = 4
End If
' If (RS232.BytesToRead >= 5) Then
'setrxnum = 8 '打開串口自動讀取
' Exit For
' End If
If RS232.IsOpen = True Then '尚未開啟
RS232.Close() '開啟通訊端口
End If
End Sub
prj_Vbnet.zip
(509.89 KB, 下載次數: 1)
2023-9-5 11:41 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|