.NET2 SerialPort - 100% CPU problem
Hi All ,
i'm using .net2 beta1 SerialPort class.
the problem is when i first recieve bytes - the CPU is getting up to 100% and stays there.
i tried working with the SerailPort in many ways (starting Recieve thread, etc') but it seems that this problem repeats itself. is the read call is synchronous and does not come back until it finish?
a sample code that cause this
without a seperate thread in this case)
SerialPort m_SP;
m_SP = new SerialPort(portName, baudRate);
// set serial port default properties
m_SP.DataBits = 8;
m_SP.Parity = Parity.None;
m_SP.StopBits = StopBits.One;
m_SP.Handshake = Handshake.None;
m_SP.Open();
m_SP.ReceivedEvent += new SerialReceivedEventHandler(DataReceived);
private void DataReceived(object sender, SerialReceivedEventArgs e)
{
m_nBytesRead = m_SP.Read(m_packet, 0, m_packet.Length);
}
Any Suggestions guys?
Thanks,
[993 byte] By [
Dotan] at [2008-3-3]
well ,
it seems to happend only when i open
multiport-card port (i'm using
DIGI Board AccelePort 8p) - the snippet i poseted is enough to reproduce the problem. as i said , i just open the port and send it something from the other end - this cause the cpu to get stuck on 100% until i close the port.
when i use the original port of my computer (COM1) it works just fine without any problems.
did someone encounter this kind of problems?
tnx,
dotan.
Hi Vikram,
well I already tried that but it doesn't help.
to make it more clear -
1. problem occur only when working through multiport card (Digi Acceleport 8p).
2. as soon as I open the port and send it something from the other connected end - cpu is up to 100% and will get released only when I close the port. as I said before - problem happens before i even make a Read call !
do you think about something to solve this riddle?
Thanks all,
Dotan.
> m_nBytesRead = m_SP.Read(m_packet, 0, m_packet.Length);
I don't see any assignment to m_packet before that, so I'd guess that m_packet.Length might be 0, and the Read method is successfully reading 0 bytes. Since this is in a loop, of course it takes 100% of the CPU.
By the way now that I've seen page
http://msdn2.microsoft.com/library/34t733fh(en-us,vs.80).aspx
now I wonder how it can work. It says:
> Reads a number of bytes from the System.IO.Ports.SerialPort
> input buffer and writes those bytes into a byte array at a given offset.
BUT: the destination isn't a byte array, it's a Char array, known in C++ as a wchar_t array. Does it convert multibyte text input into Unicode, and what does it do if only part of a character has been buffered so far at the time it's called? How does it know whether the input is text or binary? Until things like this get cleared up, sensible programmers will only call the version that really reads into a byte array.