By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,209 Members | 1,706 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,209 IT Pros & Developers. It's quick & easy.

c# SerialPort problem, write() interrupted

P: n/a
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

Sep 18 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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).

Sep 18 '07 #2

P: n/a
On Sep 18, 8:56 pm, Doug Semler <dougsem...@gmail.comwrote:
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).
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?

Sep 19 '07 #3

P: n/a
On Sep 19, 9:13 am, arjan...@gmail.com wrote:
On Sep 18, 8:56 pm, Doug Semler <dougsem...@gmail.comwrote:
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).

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 :)

Sep 20 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.