SerialPort error

When opening a serial port in the dotnet 2.0, using stopbits.none causes an ArgumentOutOfRange Exception. Otherwise this feature works great. Here is a code example-

Imports System.IO.Ports

Imports System.ComponentModel

Imports System.Windows.Forms

Public Class frmComm

Dim comport As New SerialPort()

Public Sub portHandle()

If comport.IsOpen Then

comport.Close()

End If

comport.DataBits = 8

comport.StopBits = StopBits.None

comport.Parity = Parity.None

comport.BaudRate = 9600

comport.PortName = "COM1"

comport.Open()

End Sub

End Class



Answer this question

SerialPort error

  • JensLundberg

    Thanks, that cleared things up. I think ReneeC is right also, it must be impossible to set a 0.
  • Marcin Obel

    I'm not sure about hyperterm, but if you pass StopBits.One to the SerialPort, the value that gets put in the DCB is zero (which is what you'd expect)

    Check out http://msdn.microsoft.com/library/default.asp url=/library/en-us/devio/base/dcb_str.asp for more information on the DCB structure...

    I believe the confusion here is caused by the fact that the numerical value in the API is different from what the name of the enumeration sounds like, which in turn is different from what will actually be set in the LCR:

    Enum Numerical .NET FX LCR Bit 2 StopBits
    value
  • Danz.

    I agree that the framework shouldn't expose .none as a value. It might be to have a value for "not specified" to enforce the developer to determine what it is, but in most cases I have encountered, the stop bit value is 1, so it would be good to use 1 as default value.
  • Kade

    I just read up on this Johan. "Zero stop bits" means one stop bit in RS-232.



  • Zakary

    I'm not a serial port expert, but is zero stop bits really a valid value From what I can recall, you could use 1, 1.5 or 2 stop bits, but that there were some restrictions on when you could use 1.5 and 2 respectively.

    I'm not sure if the StopBits.None should really be exposed here...

    I would recommend that you open an issue on the MSDN product feedback center. I'm sure that the team that owns the SerialPort class can explain this better than I can...

    Best regards,
    Johan Stenberg



  • Cristiano Muzi

    ReneeC,

    I'm sorry if my answer wasn't clear - I also have the impression that you can't have 0 stop bits, which is why I'm not sure that StopBits.None (the row in red in the table in my previous post) should be exposed by the framework.

    Best regards,
    Johan Stenberg



  • ZhangQing

    I am trying to use stopbits.none because when using hyperterm on the same control system, you have to declare none as the stopbits parameter. Does none in .net not mean the same thing as none in hyperterm
  • Miketrix

    Johan,

    Then I'm confused. What I read was that stop bits = 0 was impossible because the stop bit represents the end of the bit frame. It tells hardware the the byte is complete.



  • SerialPort error