J
Jamie Risk
I'm attempting to improve some serially executing code (that
uses the SerialPort class) bogging Windows down when it runs.
To do the 'antibogging' I'm following the example from MSDN
Windows.IO.Ports.SerialPort page and use threading.
I'm not sure if I'm creating problems with this implementation
and would appreciate your input.
The original serial code:
{
class myPacket
{
...
public bool isValid { get { ... } };
...
// append()
// scan input array (stream) for a valid packet
// set 'isValid' when complete
public append(byte[] data) { ... };
public byte[] ToByteArray() { ... };
...
}
class myComm : System.IO.Ports.SerialPort
{
public myPacket Transaction(myPacket packet)
{
myPacket response;
if (this.IsOpen)
{
// Write out the packet.
Write(packet.ToByteArray(), 0, packet.Length);
// Read until a valid packet is collected
myPacket response = new myPacket();
byte[] data;
while (!response.IsValid)
{
base.Read(data,0,this.BytesToRead);
response.append(data);
}
}
return response;
}
}
}
The class is used thusly:
{
...
myPacket Send;
myPacket Rcv;
...
myComm port = new myComm();
port.Open();
Rcv = port.Transaction(Send);
port.Close();
...
}
My attempt to 'antibog' is to add a simple thread to the
Transaction() function:
class myComm : System.IO.Ports.SerialPort
{
myPacket response;
public myPacket Transaction(myPacket packet)
{
response = null;
if (this.IsOpen)
{
// As a precaution clear out the buffer
while (this.BytesToRead > 0 )
base.Read(data,0,this.BytesToRead);
// Write out the packet.
Write(packet.ToByteArray(), 0, packet.Length);
Thread rdThrd = new Thread(TransactionRead);
myPacket response = new myPacket();
rdThrd.Start();
// kill the thread when a valid packet is created
while (!response.IsValid)
{
}
rdThrd.Join();
}
return response;
}
private void TransactionRead()
{
byte[] data;
while(base.BytesToRead > 0)
{
base.Read(data,0,this.BytesToRead);
response.append(data);
}
}
}
uses the SerialPort class) bogging Windows down when it runs.
To do the 'antibogging' I'm following the example from MSDN
Windows.IO.Ports.SerialPort page and use threading.
I'm not sure if I'm creating problems with this implementation
and would appreciate your input.
The original serial code:
{
class myPacket
{
...
public bool isValid { get { ... } };
...
// append()
// scan input array (stream) for a valid packet
// set 'isValid' when complete
public append(byte[] data) { ... };
public byte[] ToByteArray() { ... };
...
}
class myComm : System.IO.Ports.SerialPort
{
public myPacket Transaction(myPacket packet)
{
myPacket response;
if (this.IsOpen)
{
// Write out the packet.
Write(packet.ToByteArray(), 0, packet.Length);
// Read until a valid packet is collected
myPacket response = new myPacket();
byte[] data;
while (!response.IsValid)
{
base.Read(data,0,this.BytesToRead);
response.append(data);
}
}
return response;
}
}
}
The class is used thusly:
{
...
myPacket Send;
myPacket Rcv;
...
myComm port = new myComm();
port.Open();
Rcv = port.Transaction(Send);
port.Close();
...
}
My attempt to 'antibog' is to add a simple thread to the
Transaction() function:
class myComm : System.IO.Ports.SerialPort
{
myPacket response;
public myPacket Transaction(myPacket packet)
{
response = null;
if (this.IsOpen)
{
// As a precaution clear out the buffer
while (this.BytesToRead > 0 )
base.Read(data,0,this.BytesToRead);
// Write out the packet.
Write(packet.ToByteArray(), 0, packet.Length);
Thread rdThrd = new Thread(TransactionRead);
myPacket response = new myPacket();
rdThrd.Start();
// kill the thread when a valid packet is created
while (!response.IsValid)
{
}
rdThrd.Join();
}
return response;
}
private void TransactionRead()
{
byte[] data;
while(base.BytesToRead > 0)
{
base.Read(data,0,this.BytesToRead);
response.append(data);
}
}
}