P
pavel.orehov
Hi,
I am using new .Net class SerialPort for read/write data from COM port.
I am experiencing performance problem.
I wrote the test program. All what it does, is read data from COM port
and write it to file on local disk. I am using OnDataReceived event to
read ready data. The data in COM arrive with spped of 100MSec, 60 byte
each interval. My CPU usage on 40% !!!
What's going on ???
I have tried not to register for data receive event and it almost the
same hight CPU usage after I am opening the port.
Any ideas why the CPU usage is so hight.
Here is the code.
using System;
using System.Collections.Generic;
using System.Text;
using System.IO.Ports;
using System.IO;
using System.Diagnostics;
namespace SerialPortRecorder
{
public class ComRecorder
{
private const int READ_BUFFER_SIZE = 1024;
private SerialPort mPort = new SerialPort();
private string mFilename;
private StreamWriter mStreamWriter;
private BinaryWriter mBinaryWriter;
private byte[] mBuffer = new byte[READ_BUFFER_SIZE];
public ComRecorder(string comName, string filename)
{
mFilename = filename;
mStreamWriter = new StreamWriter(mFilename);
mBinaryWriter = new BinaryWriter(mStreamWriter.BaseStream);
mPort.BaudRate = 4800;
mPort.StopBits = StopBits.One;
mPort.Parity = Parity.None;
mPort.PortName = comName;
mPort.DataReceived += new
SerialDataReceivedEventHandler(this.OnDataReceived);
mPort.ErrorReceived += new
SerialErrorReceivedEventHandler(this.OnErrorReceived);
}
public void Start()
{
try
{
mPort.Open();
}
catch (Exception ex)
{
Debug.Print("ComRecorder: Failed to open COM port: " +
ex.Message);
}
}
public void Stop()
{
try
{
mPort.Close();
}
catch (Exception ex)
{
Debug.Print("ComRecorder: Failed to close COM port: " +
ex.Message);
}
}
private void OnDataReceived(object sender,
SerialDataReceivedEventArgs args)
{
// read from port
int readCount = 0;
try
{
readCount = mPort.Read(mBuffer, 0, READ_BUFFER_SIZE);
}
catch (Exception ex)
{
Debug.Print("ComRecorder: Filed to read from COM port:
" + ex.Message);
}
// copy data to new buffer, exact read bytes
byte[] buffer = new byte[readCount];
Buffer.BlockCopy(mBuffer, 0, buffer, 0, readCount);
// write read data to file
mBinaryWriter.Write(buffer);
}
private void OnErrorReceived(object sender,
SerialErrorReceivedEventArgs args)
{
Debug.Print("ComRecorder: error " +
args.EventType.ToString());
}
}
}
I am using new .Net class SerialPort for read/write data from COM port.
I am experiencing performance problem.
I wrote the test program. All what it does, is read data from COM port
and write it to file on local disk. I am using OnDataReceived event to
read ready data. The data in COM arrive with spped of 100MSec, 60 byte
each interval. My CPU usage on 40% !!!
What's going on ???
I have tried not to register for data receive event and it almost the
same hight CPU usage after I am opening the port.
Any ideas why the CPU usage is so hight.
Here is the code.
using System;
using System.Collections.Generic;
using System.Text;
using System.IO.Ports;
using System.IO;
using System.Diagnostics;
namespace SerialPortRecorder
{
public class ComRecorder
{
private const int READ_BUFFER_SIZE = 1024;
private SerialPort mPort = new SerialPort();
private string mFilename;
private StreamWriter mStreamWriter;
private BinaryWriter mBinaryWriter;
private byte[] mBuffer = new byte[READ_BUFFER_SIZE];
public ComRecorder(string comName, string filename)
{
mFilename = filename;
mStreamWriter = new StreamWriter(mFilename);
mBinaryWriter = new BinaryWriter(mStreamWriter.BaseStream);
mPort.BaudRate = 4800;
mPort.StopBits = StopBits.One;
mPort.Parity = Parity.None;
mPort.PortName = comName;
mPort.DataReceived += new
SerialDataReceivedEventHandler(this.OnDataReceived);
mPort.ErrorReceived += new
SerialErrorReceivedEventHandler(this.OnErrorReceived);
}
public void Start()
{
try
{
mPort.Open();
}
catch (Exception ex)
{
Debug.Print("ComRecorder: Failed to open COM port: " +
ex.Message);
}
}
public void Stop()
{
try
{
mPort.Close();
}
catch (Exception ex)
{
Debug.Print("ComRecorder: Failed to close COM port: " +
ex.Message);
}
}
private void OnDataReceived(object sender,
SerialDataReceivedEventArgs args)
{
// read from port
int readCount = 0;
try
{
readCount = mPort.Read(mBuffer, 0, READ_BUFFER_SIZE);
}
catch (Exception ex)
{
Debug.Print("ComRecorder: Filed to read from COM port:
" + ex.Message);
}
// copy data to new buffer, exact read bytes
byte[] buffer = new byte[readCount];
Buffer.BlockCopy(mBuffer, 0, buffer, 0, readCount);
// write read data to file
mBinaryWriter.Write(buffer);
}
private void OnErrorReceived(object sender,
SerialErrorReceivedEventArgs args)
{
Debug.Print("ComRecorder: error " +
args.EventType.ToString());
}
}
}