C# Serial Port Readtimeout

This article will demonstrate how to write and receive data from a device connected to a serial port in C and. NET. We will be writing the received data to a TextBox on a form, so this will also deal with threading.

In the past, to communicate with a Serial Port using. Net 1.1, you had to either use the Windows API, or use a third-party control. With. Net 2.0, Microsoft added this support with the inclusion of the SerialPort class as part of the System.IO.Ports namespace. Implementation of the SerialPort class is very straight-forward. To create an instance of the SerialPort class, you simply pass the SerialPort options to the constructor of the class:

// all of the options for a serial device

// ---- can be sent through the constructor of the SerialPort class

// ---- PortName COM1, Baud Rate 19200, Parity None,

// ---- Data Bits 8, Stop Bits One, Handshake None

SerialPort _serialPort new SerialPort COM1, 19200, Parity.None, 8, StopBits.One ;

_serialPort.Handshake Handshake.None;

To receive data, we will need to create an EventHandler for the SerialDataReceivedEventHandler :

// sp_DataReceived is a custom method that I have created

_serialPort.DataReceived new SerialDataReceivedEventHandler sp_DataReceived ;

You can also set other options, such as the ReadTimeout and WriteTimeout:

// milliseconds _serialPort.ReadTimeout 500;

_serialPort.WriteTimeout 500;

Once you are ready to use the Serial Port, you will need to open it: 

// Opens serial port

_serialPort.Open ; 

Now we are ready to receive data. However, to write this data to the TextBox on a form, we need to create a delegate.. Net does not allow cross-thread action, so we need to use a delegate. The delegate is used to write to the UI thread from a non-UI thread.

// delegate is used to write to a UI control from a non-UI thread

private delegate void SetTextDeleg string text ; We will now create the sp_DataReceived method that will be executed when data is received through the serial port:

void sp_DataReceived object sender, SerialDataReceivedEventArgs e

Thread.Sleep 500 ;

string data _serialPort.ReadLine ;

// Invokes the delegate on the UI thread, and sends the data that was received to the invoked method.

// ---- The si_DataReceived method will be executed on the UI thread which allows populating of the textbox.

this.BeginInvoke new SetTextDeleg si_DataReceived, new object data ;

Now we create our si_DataReceived method:

private void si_DataReceived string data textBox1.Text data.Trim ;

We can now receive data from a serial port device and display it on a form. Some devices will send data without being prompted. However, some devices need to be send certain commands, and it will reply with the data that the command calls for. For these devices, you will write data to the serial port, and use the previous code to get the data that will be sent back. In my example, I will be communicating with a scale. For this particular scale, sending the command SI r n will force it to return the weight of whatever is on the scale. This command is specific for this scale. You will need to read the documentation of your serial device to find commands that it will receive. To write to the serial port, I have created a Start button on the form. I have added code to it s Click_Event:

private void btnStart_Click object sender, EventArgs e

// Makes sure serial port is open before trying to write

try

if. _serialPort.IsOpen

_serialPort.Open ;

_serialPort.Write SI r n ;

catch Exception ex

MessageBox.Show Error opening/writing to serial port :: ex.Message, Error. ;

And that is all you need to do. I have attached the Visual Studio 2005 solution.

Sending Files. Here are two helpful little methods for sending files through the serial port. Of course, these are the bare essentials and as always, you should check.

  • This article shows how to communicated with Serial Port using C.
  • Feb 24, 2014  Of course if won t work this way. You can send data to serial port from the button event handler, but you cannot read this way. This is blocking operation.
  • How To Read Data From Serial Port: Dear Sir, Please Find the Below lines of code.. in asp.net we dont have begininvoke please find sp_DataReceived,First time i.
  • USB to Serial to and fro communication using C language. This is a discussion on USB to Serial to and fro communication using C language within the C.
  • I ve built a C application which reads and writes data from a serial port. The device connected to the serial port is a FTDI USB to serial converter which.

Selecting a Port. Figure 1 shows a Windows Form for an application that uses a serial port to control an LED on a remote device. Figure 1. Clicking a button sends a.

Communicating with Serial Port in C#

Siteme her gün bir makale eklemek istiyorum ama bir şekilde ne oluyorsa ekleyemiyorum. Okul beklediğimden de çok vaktimi aldı üstüne birde çalıştığım.

Open and read COM port data using System.IO.Ports in C and VB. NET 2.0 Author: Zunnair Download Source Code : 727_ComPortAPP.zip In this simple article you will.