c# SerialPort problem, write() interrupted

Tech-Archive recommends: Fix windows errors by optimizing your registry



Hello all,

I have a strange problem with the .NET serialPort class.

When I write some data to the serialport, not all the data is
immediately send: every once in a while, only ~50 characters of the 64
characters I'm sending gets send.

Then, when I do a write() again, the data gets send. This is
unacceptable behavior for my program.

With only a console version, the problem occurs once every 100 times.
But with a UI with some events, this rate goes up to ~10%.

Are there any explanations for this?

The console-code:
static internal SerialPort Serial;
static void Main(string[] args)
{

Console.WriteLine("Welcome to this testapplication!");

Serial = new SerialPort("COM4");

Serial.BaudRate = 9600;
Serial.Handshake = Handshake.RequestToSend;
Serial.Parity = Parity.None;
Serial.StopBits = StopBits.One;
//Serial.RtsEnable = true;
Serial.Encoding = System.Text.Encoding.Default;
Serial.DataBits = 8;
Serial.NewLine = "\r";
Serial.ReceivedBytesThreshold = 20;

Serial.Open();

byte[] values = new byte[256];

for (int j = 0; j < values.Length; j++)
{
values[j] = Convert.ToByte(j);
}

while (true)
{
for (int i = 0; i <= 192; i += 64)
{

send(values, i, 64);
Thread.Sleep(750);
Console.WriteLine("Another 64 bytes send..");
}
Console.WriteLine("----256 bytes send..");
Thread.Sleep(1000);
}

}



static internal int send(byte[] p, int o, int c)
{

if ((o + c) > p.Length)
{
c = p.Length - o;
}
try
{
Serial.Write(p, o, c);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}

return c;

}
}

The equipment I'm writing to uses CTS/RTS handshaking..

Kind Regards,

Arjan

.



Relevant Pages