Skupiny Google už nepodporujú nové príspevky ani odbery Usenet. Historický obsah zostáva viditeľný.

c# SerialPort problem, write() interrupted

7 zobrazení
Preskočiť na prvú neprečítanú správu

arja...@gmail.com

neprečítané,
18. 9. 2007, 11:10:5318. 9. 2007
komu:
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

neprečítané,
18. 9. 2007, 14:56:3318. 9. 2007
komu:
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

neprečítané,
19. 9. 2007, 3:13:2919. 9. 2007
komu:

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

neprečítané,
20. 9. 2007, 10:20:4120. 9. 2007
komu:

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

0 nových správ