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
Did you try Serial.BaseStream.Flush() (don't know if it will work,
just a suggestion).
Calling the Flush() function has the same effect as calling
writeLine(""): it'll transmit the buffers, most of the time..
Still, both ways are a work around for a problem that shouldn't be
there in the first place. Or do I have too high expections of serial
communication?
Problem solved.. the hardware was holding data back.. Fixed the
hardware so the problem is gone :)