how work serial port in c#

Discussion in 'C#' started by morteza bazrafshan, Mar 31, 2009.

  1. morteza bazrafshan

    morteza bazrafshan New Member

    Joined:
    Mar 31, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    hi
    i start work with c# three week ago
    i'm an electronic engineer and i want send some data from micro to pc
    i want work with c# for data monitoring
    how can we work with serial port in c#?
    thank you:)
     
  2. DotNetNewb

    DotNetNewb New Member

    Joined:
    Apr 27, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    I've written a few apps using the System.IO.Ports.SerialPort library.

    It's pretty straight forward.

    Simply create an instance of System.IO.Ports.SerialPort,


    example from MSDN:

    // Create a new SerialPort object with default settings.
    System.IO.Ports.SerialPort _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();


    when you want to write something to the port

    _serialPort.Write(.....);

    there's a few different ways to write, you can write a string, or a byte array.

    when your done with the port,

    _serialPort.Close();
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice