Hi,
Maybe it's a bit off topic but think it may be useful for all interfacing C# and AVR.
For my AVR project I use bootloader that accepts HEX files on serial port. All this type of bootloaders use XON/XOFF protocol to synchronize flashing and data packets on serial link.
I wrote a piece of C# code to upgrade main program via serial port using HEX bootloader. This looks very simple (and it is) but unfortunately in my case it took some time to get C# program working.
The problem was c# completely ignored XON/XOFF characters. It just send all data without paying attention what AVR is doing.
I sent the hex file at once using only one call to Write() function:
serial.Write(AvrData, 0, AvrData.Length);
Before you copy this code I tell you: it does not work - XON/XOFF are ignored.
To get it working it must be replaced with:
for (int i = 0; i < AvrData.Length; i++) { serial.Write(AvrData, i, 1); }
It this example XON/XOFF are processed properly.
I do not know whether it's a problem of C# component or windows serial driver. Maybe when the block of data is sent incoming data is not interpreted by the component responsible for handshaking.
And at the end, piece of code that sends hex file to bootloader:
byte AvrData[] = System.IO.File.ReadAllBytes("myprog.hex"); SerialPort serial = new SerialPort(SerialPortName, 19200, Parity.None, 8); serial.Handshake = Handshake.XOnXOff; serial.WriteTimeout = 1000; serial.Open(); for (int i = 0; i < AvrData.Length; i++) { serial.Write(AvrData, i, 1); } serial.Close();