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.Port s.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(myP acket packet)
{
myPacket response;
if (this.IsOpen)
{
// Write out the packet.
Write(packet.To ByteArray(), 0, packet.Length);
// Read until a valid packet is collected
myPacket response = new myPacket();
byte[] data;
while (!response.IsVa lid)
{
base.Read(data, 0,this.BytesToR ead);
response.append (data);
}
}
return response;
}
}
}
The class is used thusly:
{
...
myPacket Send;
myPacket Rcv;
...
myComm port = new myComm();
port.Open();
Rcv = port.Transactio n(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(myP acket packet)
{
response = null;
if (this.IsOpen)
{
// As a precaution clear out the buffer
while (this.BytesToRe ad 0 )
base.Read(data, 0,this.BytesToR ead);
// Write out the packet.
Write(packet.To ByteArray(), 0, packet.Length);
Thread rdThrd = new Thread(Transact ionRead);
myPacket response = new myPacket();
rdThrd.Start();
// kill the thread when a valid packet is created
while (!response.IsVa lid)
{
}
rdThrd.Join();
}
return response;
}
private void TransactionRead ()
{
byte[] data;
while(base.Byte sToRead 0)
{
base.Read(data, 0,this.BytesToR ead);
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.Port s.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(myP acket packet)
{
myPacket response;
if (this.IsOpen)
{
// Write out the packet.
Write(packet.To ByteArray(), 0, packet.Length);
// Read until a valid packet is collected
myPacket response = new myPacket();
byte[] data;
while (!response.IsVa lid)
{
base.Read(data, 0,this.BytesToR ead);
response.append (data);
}
}
return response;
}
}
}
The class is used thusly:
{
...
myPacket Send;
myPacket Rcv;
...
myComm port = new myComm();
port.Open();
Rcv = port.Transactio n(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(myP acket packet)
{
response = null;
if (this.IsOpen)
{
// As a precaution clear out the buffer
while (this.BytesToRe ad 0 )
base.Read(data, 0,this.BytesToR ead);
// Write out the packet.
Write(packet.To ByteArray(), 0, packet.Length);
Thread rdThrd = new Thread(Transact ionRead);
myPacket response = new myPacket();
rdThrd.Start();
// kill the thread when a valid packet is created
while (!response.IsVa lid)
{
}
rdThrd.Join();
}
return response;
}
private void TransactionRead ()
{
byte[] data;
while(base.Byte sToRead 0)
{
base.Read(data, 0,this.BytesToR ead);
response.append (data);
}
}
}
Comment