System.IO.Ports.SerialPort performance problem

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());
}

}
}
 
G

Guest

1. How are you measuring CPU usage
2. Is this causing an actual problem?
3. What kind of media are you writing to?

-Chris
 
P

pavel.orehov

1. How are you measuring CPU usage
2. Is this causing an actual problem?
3. What kind of media are you writing to?

-Chris

1. Am am watching in TaskManager.
2. Yes, it slow down all real application.
3. It is not matter, I have tried only read the data without writing to
the disk, the same problem. Moreover it starts slow down when I only
open the port without registering to the OnDataReceived event.

Besides I have checked the performance with perfmon. I did the sampling
on .Net marhsaling and it very hight. It looks like there are a lot of
marshaling inside this class implementation.
 
G

Guest

Task Manager? Is this a desktop application? You realize this is a compact
framework newsgroup?

-Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top