SerialPort.DataBits 속성

정의

바이트 당 데이터 비트의 표준 길이를 가져오거나 설정합니다.

public:
 property int DataBits { int get(); void set(int value); };
public int DataBits { get; set; }
[System.ComponentModel.Browsable(true)]
public int DataBits { get; set; }
member this.DataBits : int with get, set
[<System.ComponentModel.Browsable(true)>]
member this.DataBits : int with get, set
Public Property DataBits As Integer

속성 값

Int32

데이터 비트 길이입니다.

특성

예외

포트의 상태가 올바르지 않은 경우

또는 내부 포트 상태를 설정하지 못한 경우. 예를 들어, 이 SerialPort 개체에서 전달된 매개 변수가 잘못된 경우입니다.

데이터 비트 값이 5보다 작거나 8보다 큰 경우

예제

다음 코드 예제에서는 두 사용자가 null 모뎀 케이블에 의해 연결 된 두 개의 별도 컴퓨터에서 채팅할 수 있도록 클래스를 사용 SerialPort 하는 방법을 보여 줍니다. 이 예제에서는 채팅하기 전에 사용자에게 포트 설정 및 사용자 이름을 묻는 메시지가 표시됩니다. 이 코드 예제는 일부에 대해 제공 된 더 큰 코드 예제는 SerialPort 클래스입니다.

public:
    static void Main()
    {
        String^ name;
        String^ message;
        StringComparer^ stringComparer = StringComparer::OrdinalIgnoreCase;
        Thread^ readThread = gcnew Thread(gcnew ThreadStart(PortChat::Read));

        // Create a new SerialPort object with default settings.
        _serialPort = gcnew SerialPort();

        // Allow the user to set the appropriate properties.
        _serialPort->PortName = SetPortName(_serialPort->PortName);
        _serialPort->BaudRate = SetPortBaudRate(_serialPort->BaudRate);
        _serialPort->Parity = SetPortParity(_serialPort->Parity);
        _serialPort->DataBits = SetPortDataBits(_serialPort->DataBits);
        _serialPort->StopBits = SetPortStopBits(_serialPort->StopBits);
        _serialPort->Handshake = SetPortHandshake(_serialPort->Handshake);

        // Set the read/write timeouts
        _serialPort->ReadTimeout = 500;
        _serialPort->WriteTimeout = 500;

        _serialPort->Open();
        _continue = true;
        readThread->Start();

        Console::Write("Name: ");
        name = Console::ReadLine();

        Console::WriteLine("Type QUIT to exit");

        while (_continue)
        {
            message = Console::ReadLine();

            if (stringComparer->Equals("quit", message))
            {
                _continue = false;
            }
            else
            {
                _serialPort->WriteLine(
                    String::Format("<{0}>: {1}", name, message) );
            }
        }

        readThread->Join();
        _serialPort->Close();
    }

    static void Read()
    {
        while (_continue)
        {
            try
            {
                String^ message = _serialPort->ReadLine();
                Console::WriteLine(message);
            }
            catch (TimeoutException ^) { }
        }
    }
public static void Main()
{
    string name;
    string message;
    StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
    Thread readThread = new Thread(Read);

    // Create a new SerialPort object with default settings.
    _serialPort = new SerialPort();

    // Allow the user to set the appropriate properties.
    _serialPort.PortName = SetPortName(_serialPort.PortName);
    _serialPort.BaudRate = SetPortBaudRate(_serialPort.BaudRate);
    _serialPort.Parity = SetPortParity(_serialPort.Parity);
    _serialPort.DataBits = SetPortDataBits(_serialPort.DataBits);
    _serialPort.StopBits = SetPortStopBits(_serialPort.StopBits);
    _serialPort.Handshake = SetPortHandshake(_serialPort.Handshake);

    // Set the read/write timeouts
    _serialPort.ReadTimeout = 500;
    _serialPort.WriteTimeout = 500;

    _serialPort.Open();
    _continue = true;
    readThread.Start();

    Console.Write("Name: ");
    name = Console.ReadLine();

    Console.WriteLine("Type QUIT to exit");

    while (_continue)
    {
        message = Console.ReadLine();

        if (stringComparer.Equals("quit", message))
        {
            _continue = false;
        }
        else
        {
            _serialPort.WriteLine(
                String.Format("<{0}>: {1}", name, message));
        }
    }

    readThread.Join();
    _serialPort.Close();
}

public static void Read()
{
    while (_continue)
    {
        try
        {
            string message = _serialPort.ReadLine();
            Console.WriteLine(message);
        }
        catch (TimeoutException) { }
    }
}
Public Shared Sub Main()
    Dim name As String
    Dim message As String
    Dim stringComparer__1 As StringComparer = StringComparer.OrdinalIgnoreCase
    Dim readThread As New Thread(AddressOf Read)

    ' Create a new SerialPort object with default settings.
    _serialPort = New SerialPort()

    ' Allow the user to set the appropriate properties.
    _serialPort.PortName = SetPortName(_serialPort.PortName)
    _serialPort.BaudRate = SetPortBaudRate(_serialPort.BaudRate)
    _serialPort.Parity = SetPortParity(_serialPort.Parity)
    _serialPort.DataBits = SetPortDataBits(_serialPort.DataBits)
    _serialPort.StopBits = SetPortStopBits(_serialPort.StopBits)
    _serialPort.Handshake = SetPortHandshake(_serialPort.Handshake)

    ' Set the read/write timeouts
    _serialPort.ReadTimeout = 500
    _serialPort.WriteTimeout = 500

    _serialPort.Open()
    _continue = True
    readThread.Start()

    Console.Write("Name: ")
    name = Console.ReadLine()

    Console.WriteLine("Type QUIT to exit")

    While _continue
        message = Console.ReadLine()

        If stringComparer__1.Equals("quit", message) Then
            _continue = False
        Else
            _serialPort.WriteLine([String].Format("<{0}>: {1}", name, message))
        End If
    End While

    readThread.Join()
    _serialPort.Close()
End Sub

Public Shared Sub Read()
    While _continue
        Try
            Dim message As String = _serialPort.ReadLine()
            Console.WriteLine(message)
        Catch generatedExceptionName As TimeoutException
        End Try
    End While
End Sub

설명

이 속성의 값 범위는 5에서 8까지입니다. 기본값은 8입니다.

적용 대상