找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 1449|回復(fù): 1
打印 上一主題 下一主題
收起左側(cè)

原創(chuàng)-visual basic net串口樣板架構(gòu)

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:328023 發(fā)表于 2023-9-5 11:41 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
visual basicnet其實(shí)比visual C#更好用,作為嵌入式開發(fā)人員,用basic入門更簡(jiǎn)單,可以很快做出調(diào)試產(chǎn)品工具。附件是完整項(xiàng)目
這個(gè)是電腦串口尋找子函數(shù)
   Private Sub com_channel_get(ByVal getcom1 As Integer)

        Dim getcom As Integer
        Dim ports As String() = SerialPort.GetPortNames() '必須用命名空間,用SerialPort,獲取計(jì)算機(jī)的有效串口
        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中添加項(xiàng)
        Next port
        getcom = ComboBox1.Items.Count '讀ComboBox1的數(shù)值


        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  '校驗(yàn)位檢查設(shè)定
        mDataBit = 8    '數(shù)據(jù)位設(shè)定值
        mStopbit = StopBits.One  '停止位設(shè)定值



        For comnum = 0 To getcom - 1 Step 1
            ComboBox1.SelectedIndex() = comnum '第一個(gè)端口為默認(rèn)
            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        '設(shè)置引發(fā)事件的門限值
            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 '打開串口自動(dòng)讀取
        '   Exit For
        '    End If


        If RS232.IsOpen = True Then  '尚未開啟
            RS232.Close()  '開啟通訊端口
        End If


    End Sub
prj_Vbnet.zip (509.89 KB, 下載次數(shù): 1)



評(píng)分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎(jiǎng)勵(lì)!

查看全部評(píng)分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:262 發(fā)表于 2024-1-22 02:43 | 只看該作者
好資料,51黑有你更精彩!!!
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表