Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

c# SerialPort problem, write() interrupted

7 views
Skip to first unread message

arja...@gmail.com

unread,
Sep 18, 2007, 11:10:53 AM9/18/07
to
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

Doug Semler

unread,
Sep 18, 2007, 2:56:33 PM9/18/07
to
On Sep 18, 11:10 am, arjan...@gmail.com wrote:
> 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%.
>


Did you try Serial.BaseStream.Flush() (don't know if it will work,
just a suggestion).

arja...@gmail.com

unread,
Sep 19, 2007, 3:13:29 AM9/19/07
to

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?

arja...@gmail.com

unread,
Sep 20, 2007, 10:20:41 AM9/20/07
to

Problem solved.. the hardware was holding data back.. Fixed the
hardware so the problem is gone :)

0 new messages