Saturday, October 10, 2015

Serial Port synchronous and asynchronous data reading

Serial Port synchronous and asynchronous data reading

{
/ / Instantiate the serial object (default: COMM1, 9600, e, 8,1)
SerialPort serialPort1 = new SerialPort ();
/ / Change the parameters
serialPort1.PortName = "COM1";
serialPort1.BaudRate = 19200;
serialPort1.Parity = Parity.Odd;
serialPort1.StopBits = StopBits.Two;
/ / The above steps can be called when the instance of the SerialPort class, overloaded constructors
/ / SerialPort serialPort = new SerialPort ("COM1", 19200, Parity.Odd, StopBits.Two);
/ / Open the serial port (open serial port can not be changed after the name, baud rate and other parameters, modify the parameters to change the serial port is closed)
serialPort1.Open ();
/ / Send data
SendStringData (serialPort1);
/ / Form can also be used to send data byte
/ / SendBytesData (serialPort1);
/ / Open the thread to receive data
ReceiveData (serialPort1);
}
/ / Send string data
private void SendStringData (SerialPort serialPort)
{
serialPort.Write (txtSend.Text);
}
/ / /

/ / / Open the thread to receive data
/ / /
private void ReceiveData (SerialPort serialPort)
{
/ / Synchronous blocking receive data thread
Thread threadReceive = new Thread (new ParameterizedThreadStart (SynReceiveData));
threadReceive.Start (serialPort);
/ / Asynchronous receive data thread can also be
/ / Thread threadReceiveSub = new Thread (new ParameterizedThreadStart (AsyReceiveData));
/ / ThreadReceiveSub.Start (serialPort);
}
/ / Send binary data
private void SendBytesData (SerialPort serialPort)
{
byte   bytesSend = System.Text.Encoding.Default.GetBytes (txtSend.Text);
serialPort.Write (bytesSend, 0, bytesSend.Length);
}
/ / Synchronous blocking read
private void SynReceiveData (object serialPortobj)
{
SerialPort serialPort = (SerialPort) serialPortobj;
System.Threading.Thread.Sleep (0);
serialPort.ReadTimeout = 1000;
try
{
/ / Block to read data or time-out (here for 2 seconds)
byte firstByte = Convert.ToByte (serialPort.ReadByte ());
int bytesRead = serialPort.BytesToRead;
byte   bytesData = new byte [bytesRead 1];
bytesData [0] = firstByte;
for (int i = 1; i <= bytesRead; i)
bytesData [i] = Convert.ToByte (serialPort.ReadByte ());
txtReceive.Text = System.Text.Encoding.Default.GetString (bytesData);
}
catch (Exception e)
{
MessageBox.Show (e.Message);
/ / Handle timeout errors
}
serialPort.Close ();
}
/ / Asynchronous read
private void AsyReceiveData (object serialPortobj)
{
SerialPort serialPort = (SerialPort) serialPortobj;
System.Threading.Thread.Sleep (500);
try
{
txtReceive.Text = serialPort.ReadExisting ();
}
catch (Exception e)
{
MessageBox.Show (e.Message);
/ / Handle the error
}
serialPort.Close ();
}
}
static class Program
{
/ / /

/ / / Application main entry point.
/ / /
[STAThread]
static void Main ()
{
Application.EnableVisualStyles ();
Application.SetCompatibleTextRenderingDefault (false);
Application.Run (new frmTESTSerialPort ());
}
}

synchronous and asynchronous data
Path

{
/ / Instantiate the serial object (default: COMM1, 9600, e, 8,1)
SerialPort serialPort1 = new SerialPort ();
/ / Change the parameters
serialPort1.PortName = "COM1";
serialPort1.BaudRate = 19200;
serialPort1.Parity = Parity.Odd;
serialPort1.StopBits = StopBits.Two;
/ / The above steps can be called when the instance of the SerialPort class, overloaded constructors
/ / SerialPort serialPort = new SerialPort ("COM1", 19200, Parity.Odd, StopBits.Two);
/ / Open the serial port (open serial port can not be changed after the name, baud rate and other parameters, modify the parameters to change the serial port is closed)
serialPort1.Open ();
/ / Send data
SendStringData (serialPort1);
/ / Form can also be used to send data byte
/ / SendBytesData (serialPort1);
/ / Open the thread to receive data
ReceiveData (serialPort1);
}
/ / Send string data
private void SendStringData (SerialPort serialPort)
{
serialPort.Write (txtSend.Text);
}
/ / /

/ / / Open the thread to receive data
/ / /
private void ReceiveData (SerialPort serialPort)
{
/ / Synchronous blocking receive data thread
Thread threadReceive = new Thread (new ParameterizedThreadStart (SynReceiveData));
threadReceive.Start (serialPort);
/ / Asynchronous receive data thread can also be
/ / Thread threadReceiveSub = new Thread (new ParameterizedThreadStart (AsyReceiveData));
/ / ThreadReceiveSub.Start (serialPort);
}
/ / Send binary data
private void SendBytesData (SerialPort serialPort)
{
byte   bytesSend = System.Text.Encoding.Default.GetBytes (txtSend.Text);
serialPort.Write (bytesSend, 0, bytesSend.Length);
}
/ / Synchronous blocking read
private void SynReceiveData (object serialPortobj)
{
SerialPort serialPort = (SerialPort) serialPortobj;
System.Threading.Thread.Sleep (0);
serialPort.ReadTimeout = 1000;
try
{
/ / Block to read data or time-out (here for 2 seconds)
byte firstByte = Convert.ToByte (serialPort.ReadByte ());
int bytesRead = serialPort.BytesToRead;
byte   bytesData = new byte [bytesRead 1];
bytesData [0] = firstByte;
for (int i = 1; i <= bytesRead; i)
bytesData [i] = Convert.ToByte (serialPort.ReadByte ());
txtReceive.Text = System.Text.Encoding.Default.GetString (bytesData);
}
catch (Exception e)
{
MessageBox.Show (e.Message);
/ / Handle timeout errors
}
serialPort.Close ();
}
/ / Asynchronous read
private void AsyReceiveData (object serialPortobj)
{
SerialPort serialPort = (SerialPort) serialPortobj;
System.Threading.Thread.Sleep (500);
try
{
txtReceive.Text = serialPort.ReadExisting ();
}
catch (Exception e)
{
MessageBox.Show (e.Message);
/ / Handle the error
}
serialPort.Close ();
}
}
static class Program
{
/ / /

/ / / Application main entry point.
/ / /
[STAThread]
static void Main ()
{
Application.EnableVisualStyles ();
Application.SetCompatibleTextRenderingDefault (false);
Application.Run (new frmTESTSerialPort ());
}
}
}

No comments:

Post a Comment