1

运行时出现“System.IO.IOException”错误。

出现的错误信息如下:

System.IO.IOException:参数不正确。

at Sysytem>IO.Ports.InternalResources.WinIOError(Int32 errorCode, String str)
at System.IO.Ports.SerialStream.BeginWriteCore(Byte[]array, Int32 offset, Int32 numbytes, AsyncCallback userCallback, Object stateObject)
at System.IO.Ports.SerialStream.Write(Byte[]array, Int32 offset, Int32 count, Int32 timeout)
at System.IO.Ports.Serialport.Write(String text)
at WindowsApplication1.Form1.Button3_Click(Object sender, EventArgs e) in C:\Documents and Settings\user\Desktop\Nikunj\data5\WindowsApplication1\Neuation.vb:Line 151

现在第 151 行是:SerialPort1.Write("2)" & vbCrLf)

这是我的整个代码:

公开课形式1

Private Sub Form1_Load( _
       ByVal sender As System.Object, _
       ByVal e As System.EventArgs) _
       Handles MyBase.Load

    For i As Integer = 0 To _
       My.Computer.Ports.SerialPortNames.Count - 1
        cbbCOMPorts.Items.Add( _
           My.Computer.Ports.SerialPortNames(i))
    Next
    Button7.Enabled = False
    Timer1.Enabled = False
End Sub
' Private Sub NumericUpDown1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDown1.ValueChanged

'End Sub


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    'While Button1.Enabled = True
    If IsNumeric(Button1) = False Then
        If NumericUpDown1.Value < 20000 Then
            NumericUpDown1.Value = NumericUpDown1.Value + 10
            TextBox1.Text = NumericUpDown1.Value
            'If ProgressBar1.Value < 175 Then
            'ProgressBar1.Value = ProgressBar1.Value + 4
            'End If
        End If
    End If
    Button1.Enabled = False
    Button1.Enabled = True
    'End While
End Sub

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged



End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    If IsNumeric(Button2) = False Then
        If NumericUpDown1.Value > 10 Then
            NumericUpDown1.Value = NumericUpDown1.Value - 10
            'TextBox1.Text = "RCF"
            'If ProgressBar1.Value < 175 Then
            'ProgressBar1.Value = ProgressBar1.Value - 4
            'End If
        End If

    End If
End Sub



' Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
'    Try

'SerialPort1.Write("2&" & RichTextBox2.Text & vbCrLf)
'       If NumericUpDown1.Value < 100 Then
'          SerialPort1.Write("2&000" & NumericUpDown1.Value & vbCrLf)
'     End If
'    If NumericUpDown1.Value < 1000 Then
'       SerialPort1.Write("2&00" & NumericUpDown1.Value & vbCrLf)
'  End If
'        If NumericUpDown1.Value < 10000 Then
'           SerialPort1.Write("2&0" & NumericUpDown1.Value & vbCrLf)
'      End If
'     If NumericUpDown1.Value > 9999 Then
'        SerialPort1.Write("2&" & NumericUpDown1.Value & vbCrLf)
'   End If
' SerialPort1.Write("2&" & NumericUpDown1.Value & vbCrLf)

'        With RichTextBox1
'           .SelectionColor = Color.Black
'.AppendText(RichTextBox2.Text & vbCrLf)
'          .ScrollToCaret()
'     End With
'    RichTextBox2.Text = String.Empty
' Catch ex As Exception
'    MsgBox(ex.ToString)
' End Try
' End Sub

Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)



End Sub

Private Sub RichTextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox2.TextChanged

End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
    If SerialPort1.IsOpen Then
    End If
    Try
        With SerialPort1
            .PortName = cbbCOMPorts.Text
            .BaudRate = 19200
            .Parity = IO.Ports.Parity.None
            .DataBits = 8
            .StopBits = IO.Ports.StopBits.One
            '.Encoding = System.Text.Encoding.Unicode
        End With
        SerialPort1.Open()
        RichTextBox2.Text = cbbCOMPorts.Text & " connected."
        Button4.Enabled = False
        Button7.Enabled = True
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
End Sub

Private Sub DataReceived( _
   ByVal sender As Object, _
   ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _
   Handles SerialPort1.DataReceived

    ' RichTextBox1.Invoke(New _
    '  myDelegate(AddressOf updateTextBox), _
    '   New Object() {})
End Sub


Public Delegate Sub myDelegate()
Public Sub updateTextBox()
    'With RichTextBox1
    '.Font = New Font("Garamond", 12.0!, FontStyle.Bold)
    '.SelectionColor = Color.Red
    ' .AppendText(SerialPort1.ReadExisting)
    '.ScrollToCaret()

    'End With

End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    Try
        If NumericUpDown1.Value < 100 Then
            SerialPort1.Write("2&000" & NumericUpDown1.Value & vbCrLf)
        End If
        If NumericUpDown1.Value < 1000 Then
            SerialPort1.Write("2&00" & NumericUpDown1.Value & vbCrLf)
        End If
        If NumericUpDown1.Value < 10000 Then
            SerialPort1.Write("2&0" & NumericUpDown1.Value & vbCrLf)
        End If
        If NumericUpDown1.Value > 9999 Then
            SerialPort1.Write("2&" & NumericUpDown1.Value & vbCrLf)
        End If
        ' SerialPort1.Write("2&" & NumericUpDown1.Value & vbCrLf)
        SerialPort1.Write("2)" & vbCrLf)
        'With RichTextBox1
        ' .SelectionColor = Color.Black
        '.AppendText(RichTextBox2.Text & vbCrLf)
        ' .ScrollToCaret()
        'End With
        RichTextBox2.Text = cbbCOMPorts.Text & " connected."
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
    ' SerialPort1.Write("2)" & vbCrLf)
    Timer1.Enabled = True
    Button3.Enabled = False
    Button6.Enabled = True
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
    SerialPort1.Write("2*" & vbCrLf)
    Button3.Enabled = True
    Button6.Enabled = False
    Timer1.Enabled = False
End Sub

Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
    Try
        SerialPort1.Close()
        RichTextBox2.Text = SerialPort1.PortName & " disconnected."
        Button4.Enabled = True
        Button7.Enabled = False
        Timer1.Enabled = False
        Button3.Enabled = True
        Button6.Enabled = False
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
End Sub

Private Sub cbbCOMPorts_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbbCOMPorts.SelectedIndexChanged

End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    If NumericUpDown3.Value > 0 Then
        NumericUpDown3.Value = NumericUpDown3.Value - 1
        If NumericUpDown3.Value = 0 Then
            ' If NumericUpDown2.Value > 0 Then
            'NumericUpDown2.Value = NumericUpDown2.Value - 1
            'If NumericUpDown2.Value >= 0 And NumericUpDown3.Value = 0 Then
            ' NumericUpDown3.Value = 59

            'End If
            'End If
        End If
    Else 'NumericUpDown3.Value = 59
        If NumericUpDown2.Value > 0 Then
            NumericUpDown2.Value = NumericUpDown2.Value - 1
            If NumericUpDown2.Value >= 0 And NumericUpDown3.Value = 0 Then
                NumericUpDown3.Value = 59

            End If
        End If
    End If



    If NumericUpDown3.Value = 0 And NumericUpDown2.Value = 0 Then
        Timer1.Enabled = False
        SerialPort1.Write("2*" & vbCrLf)
        Timer1.Enabled = False
        Button3.Enabled = True
        Button6.Enabled = False
    End If
End Sub

Private Sub NumericUpDown2_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDown2.ValueChanged

End Sub

Private Sub NumericUpDown3_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDown3.ValueChanged

End Sub

Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

End Sub

Private Sub Display_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles Display.LinkClicked


End Sub


Private Sub ContextMenuStrip1_Opening(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs)

End Sub

Private Sub NumericUpDown1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDown1.ValueChanged

End Sub

结束类

另一件事,此错误仅在我尝试在 Windows XP 中运行应用程序时出现。它在 Windows 7 中没有任何错误。

请帮助我如何解决异常错误。我的事情错误与串行端口1有关。它无法传输数据。

应用程序是运行电机。它很容易连接到 COM 端口,但是当我单击开始按钮时,应用程序停止并显示上述错误消息。

请帮我解决问题。预先感谢。

4

0 回答 0