All pastes #24814 Raw Edit

Miscellany

public text v1 · immutable
#24814 ·published 2005-10-07 07:28 UTC
rendered paste body
        public void ReceptionProc()
        {
            byte[] mainbuf = new byte[1024];
            int buf_index = 0;


            while (true)
            {
                Thread.Sleep(150);
                byte[] buffer = new byte[256];
                int len = port.Read(buffer, 0, 255);

                if (len >= 2 && buffer[0] == (byte)0x10 && (buffer[1] == (byte)0x30 || buffer[1] == (byte)0x31))
                {
                    buf_index = 0;
                    Console.WriteLine("Received ACK");
                    ready = true;
                    Array.Copy(buffer, 2, mainbuf, buf_index, len - 2);
                    buf_index += (len-2);
                }
                else
                {
                    Array.Copy(buffer, 0, mainbuf, buf_index, len);
                    buf_index += len;
                }

                if (buf_index >= 2)
                {
                    // should add check for NAK, and if NAK then should retransmit packet, should create some FIFO
                    if (mainbuf[buf_index - 2] == (byte)0x10 && (mainbuf[buf_index - 1] == (byte)0x30 || mainbuf[buf_index - 1] == (byte)0x31))
                    {
                        buf_index = 0;
                        Console.WriteLine("Received ACK");
                        ready = true;
                    }
                    else
                        if (buf_index > 2 && mainbuf[buf_index - 3] == (byte)0x10 && mainbuf[buf_index - 2] == (byte)0x03)
                        {
                            
                            
                            Console.WriteLine("Received DLE && STX");
                            if (checkChecksum(mainbuf, buf_index))
                            {
                                sendACK();
                                byte[] pdu = new byte[buf_index-8];
                                Array.Copy(mainbuf, 5, pdu, 0, buf_index - 8);
                                System.Console.Out.WriteLine("We will check that: (" + pdu.Length + ")" + ByteArray.ByteArrayToHexString(pdu, " ", 16));
                                // pdu to analyze.. decode 
                            }
                            else
                                sendNAK();
                            buf_index = 0;
                        }
                }
            }
        }