none
我的电脑有两个串口,只要有一个串口打开,另外一个串口就没办法打开了,那位高手帮我看看,那里出问题? RRS feed

  • 问题

  • Imports System.IO.Ports
    Imports System.Text
    Public Class Form1
        Dim inpudata As String = ""
        Public Event DataReceived As IO.Ports.SerialDataReceivedEventHandler
        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()
                ComboBox1.Items.Add(sp) '将所有的通信端口先列出来
            Next
            Dim mPortName As String
            ComboBox1.Sorted = True      '排序
            ComboBox1.SelectedIndex = 0  '第一个默认排序
            mPortName = ComboBox1.SelectedItem.ToString
            SerialPort1.PortName = mPortName
            SerialPort1.Encoding = Encoding.ASCII
            SerialPort1.Handshake = IO.Ports.Handshake.None
            '***************************添加波特率*********************************************************************
            ComboBox2.Items.Add("2400")
            ComboBox2.Items.Add("4800")
            ComboBox2.Items.Add("9600")
            ComboBox2.Items.Add("14400")
            ComboBox2.Items.Add("19200")
            ComboBox2.Items.Add("38400")
            ComboBox2.Items.Add("56000")
            ComboBox2.Items.Add("57600")
            ComboBox2.Items.Add("115200")
            '***************************添加数据位*********************************************************************
            ComboBox3.Items.Add("8")
            ComboBox3.Items.Add("7")
            ComboBox3.Items.Add("6")
            ComboBox3.Items.Add("5")
            '***************************添加停止位*********************************************************************
            ComboBox4.Items.Add("1")
            ComboBox4.Items.Add("1.5")
            ComboBox4.Items.Add("2")
            '***************************添加校验位*********************************************************************
            ComboBox5.Items.Add("None")
            ComboBox5.Items.Add("Mark")
            ComboBox5.Items.Add("Even")
            ComboBox5.Items.Add("Odd")
            ComboBox5.Items.Add("Space")
            '***************************设置波特率********************************************************’
            'If ComboBox2.Text.ToString().Trim() = "" Then
            'MessageBox.Show("请设置波特率!")
            'Else
            Select Case ComboBox2.SelectedText.ToString
                Case ComboBox2.Text = "2400"
                    SerialPort1.BaudRate = 2400
                Case ComboBox2.Text = "4800"
                    SerialPort1.BaudRate = 4800
                Case ComboBox2.Text = "9600"
                    SerialPort1.BaudRate = 9600
                Case ComboBox2.Text = "14400"
                    SerialPort1.BaudRate = 14400
                Case ComboBox2.Text = "19200"
                    SerialPort1.BaudRate = 19200
                Case ComboBox2.Text = "38400"
                    SerialPort1.BaudRate = 38400
                Case ComboBox2.Text = "56000"
                    SerialPort1.BaudRate = 56000
                Case ComboBox2.Text = "57600"
                    SerialPort1.BaudRate = 57600
                Case ComboBox2.Text = "115200"
                    SerialPort1.BaudRate = 115200
            End Select
            'End If
            '***************************设置数据位********************************************************’
            'If ComboBox3.Text.ToString().Trim() = "" Then
            'MessageBox.Show("请设置数据位!")
            'Else
            Select Case ComboBox3.SelectedText.ToString
                Case ComboBox3.Text = "8"
                    SerialPort1.DataBits = 8
                Case ComboBox3.Text = "7"
                    SerialPort1.DataBits = 7
                Case ComboBox3.Text = "6"
                    SerialPort1.DataBits = 6
                Case ComboBox3.Text = "5"
                    SerialPort1.DataBits = 5
            End Select
            'End If
            '***************************设置停止位********************************************************’
            'If ComboBox4.Text.ToString().Trim() = "" Then
            'MessageBox.Show("请设置停止位!")
            'Else
            Select Case ComboBox4.SelectedText.ToString
                Case ComboBox4.Text = "1"
                    SerialPort1.StopBits = IO.Ports.StopBits.One
                Case ComboBox4.Text = "1.5"
                    SerialPort1.StopBits = IO.Ports.StopBits.OnePointFive
                Case ComboBox4.Text = "2"
                    SerialPort1.StopBits = IO.Ports.StopBits.Two
            End Select
            'End If
            '***************************设置校验********************************************************’
            'If ComboBox5.Text.ToString().Trim() = "" Then
            'MessageBox.Show("请设置校验方式!")
            'Else
            Select Case ComboBox5.SelectedText.ToString
                Case ComboBox5.Text = "None"
                    SerialPort1.Parity = IO.Ports.Parity.None
                Case ComboBox5.Text = "Mark"
                    SerialPort1.Parity = IO.Ports.Parity.Mark
                Case ComboBox5.Text = "Even"
                    SerialPort1.Parity = IO.Ports.Parity.Even
                Case ComboBox5.Text = "Odd"
                    SerialPort1.Parity = IO.Ports.Parity.Odd
                Case ComboBox5.Text = "Space"
                    SerialPort1.Parity = IO.Ports.Parity.Space
            End Select
            'End If
        End Sub

        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            If SerialPort1.IsOpen = True Then
                SerialPort1.Write(TextBox2.Text())
            End If
        End Sub

        Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, _
                    ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _
                    Handles SerialPort1.DataReceived
            inpudata = SerialPort1.ReadExisting
            Me.Invoke(New EventHandler(AddressOf DoUpdate))

        End Sub

        Private Sub DoUpdate()
            TextBox1.Text = TextBox1.Text & inpudata
        End Sub

        Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
                    SerialPort1.Handshake = IO.Ports.Handshake.None
            If Not SerialPort1.IsOpen And Not ComboBox1.Text = "" And Not ComboBox2.Text = "" And Not ComboBox3.Text = "" _
                And Not ComboBox4.Text = "" And Not ComboBox5.Text = "" Then  '还未打开
                SerialPort1.Open()  '打开通信端口
                Button2.Text = "关闭串口"
                Button2.ForeColor = Color.Red
            ElseIf SerialPort1.IsOpen Then
                SerialPort1.Close()
                Button2.Text = "打开串口"
                Button2.ForeColor = Color.Black
            Else
                MsgBox("~~通信端口打开错误(请设置正确的端口参数或通信端口已被打开)~~", MsgBoxStyle.Critical)

            End If
        End Sub
    End Class

    2012年8月25日 4:38

答案

全部回复