コード例 #1
0
ファイル: Program.cs プロジェクト: Cougar/HomeAutomation
        private static bool parseInput(string instr)
        {
            if (instr.Equals("load")) {
                Console.WriteLine("loading...");
                //string filepath = "/home/arune/eclipse/c/AVR-App/main.hex";
                string filepath = argHexfile;
                hf = new HexFile();
                if (hf.loadHex(filepath)) {
                    Console.WriteLine("done!");
                    Console.WriteLine("File "+filepath+" loaded.");
                    Console.WriteLine("Size: " + hf.getLength().ToString() + " bytes, end adress " + hf.getAddrUpper().ToString() + "(0x" + hf.getAddrUpper().ToString("X") + ").");
                }
                else Console.WriteLine("Error loading " + filepath + "!");

            }
            else if (instr.Equals("go")) {
                Console.WriteLine("Connecting..");
                sc = new SerialConnection();
                try {
                    sc.setConfiguration(argBaud, System.IO.Ports.Parity.None, argPort, System.IO.Ports.StopBits.One, 8, false);
                }
                catch (Exception e) { Console.WriteLine("Error: " + e.Message); return true; }
                if (!sc.open()) { Console.WriteLine("Error opening port."); return true; }
                dl = new Downloader(hf, sc, argReceiverID, false);
                if (!dl.go()) { Console.WriteLine("Error gooing..."); return true; }
                sc.close();
            }
            else if (instr.Equals("go bios")) {
                Console.WriteLine("Connecting..");
                sc = new SerialConnection();
                try {
                    sc.setConfiguration(argBaud, System.IO.Ports.Parity.None, argPort, System.IO.Ports.StopBits.One, 8, false);
                }
                catch (Exception e) { Console.WriteLine("Error: " + e.Message); return true; }
                if (!sc.open()) { Console.WriteLine("Error opening port."); return true; }
                dl = new Downloader(hf, sc, argReceiverID, true);
                if (!dl.go()) { Console.WriteLine("Error gooing..."); return true; }
                sc.close();
            }
            else if (instr.Equals("start")) {
                Console.WriteLine("Starting..");
                sc = new SerialConnection();
                try {
                    sc.setConfiguration(argBaud, System.IO.Ports.Parity.None, argPort, System.IO.Ports.StopBits.One, 8, false);
                }
                catch (Exception e) { Console.WriteLine("Error: " + e.Message); return true; }
                if (!sc.open()) { Console.WriteLine("Error opening port."); return true; }
                byte[] data = new byte[8];
                CanPacket cp = new CanPacket(CAN_NMT, CAN_NMT_START_APP, MY_ID, argReceiverID, 0, data);
                sc.writePacket(cp);
                sc.close();
            }
            else if (instr.Equals("reset")) {
                Console.WriteLine("Resetting..");
                sc = new SerialConnection();
                try {
                    sc.setConfiguration(argBaud, System.IO.Ports.Parity.None, argPort, System.IO.Ports.StopBits.One, 8, false);
                }
                catch (Exception e) { Console.WriteLine("Error: " + e.Message); return true; }
                if (!sc.open()) { Console.WriteLine("Error opening port."); return true; }

                byte[] data = new byte[8];
                CanPacket cp = new CanPacket(CAN_NMT, CAN_NMT_RESET, MY_ID, argReceiverID, 0, data);
                sc.writePacket(cp);
                sc.close();
            }
            else if (instr.Equals("abort")) {
                if (dl != null) dl.abort();
            }
            else if (instr.Equals("exit")) {
                return false;
            }
            else {
                Console.WriteLine("Unknown command.");
            }

            return true;
        }
コード例 #2
0
        public bool getPacket(out CanPacket cp)
        {
            cp = null;
            if ((serial_conn != null) || (udpserver != null)) {

                if (serial_conn != null) {
                   	int bytesToRead = serial_conn.BytesToRead;
                    if (bytesToRead > 0) {
                        byte[] data = new byte[bytesToRead];

                        try {
                            serial_conn.Read(data, 0, data.Length);
                        } catch (Exception e) {
                            Console.WriteLine("Exception " + e + " occured.\n");
                        }

                        Array.Copy(data, 0, iBuff, iBuffPointer, data.Length);
                        iBuffPointer += bytesToRead;
                    }
                } else if (udpserver != null) {
                   	int bytesToRead = udpserver.Available;
                    if (udpserver.Available > 0) {
                        byte[] data = new byte[bytesToRead];
                        try {
                            EndPoint tmpRemote = (EndPoint)iep;
                            udpserver.ReceiveFrom(data, ref tmpRemote);
                        } catch (Exception e) {
                            Console.WriteLine("Exception " + e + " occured.\n");
                        }

                        int bytestomove = data.Length;
                        if (bytestomove ==18) {
                            bytestomove = data.Length -1;
                        }
                        Array.Copy(data, 0, iBuff, iBuffPointer, bytestomove);
                        iBuffPointer += bytestomove;
                    }

                }

                // N�r array st�rre eller lika med PACKET_LENGTH...
                while (iBuffPointer >= PACKET_LENGTH) {
                    int startIndex = 0;

                    // S�k igenom efter start byte fr�n b�rjan.
                    for (int i = 0; i < iBuffPointer; i++) {
                        // Poppa alla bytes som inte �r startbyte.
                        if (iBuff[i] != UART_START_BYTE) startIndex++;
                        else {
                            // N�r startbyte hittas, kolla om �terst�ende l�ngd �r st�rre eller lika med PACKET_LENGTH (inkl startbyte)
                            if ((iBuffPointer - startIndex) >= PACKET_LENGTH) {
                                //om s�, kolla PACKET_LENGTH-1 byte fram.
                                if (iBuff[startIndex + PACKET_LENGTH - 1] == UART_END_BYTE) {
                                    // Om byte PACKET_LENGTH-1 �r slutbyte s� extraktas startIndex till slutbyteindex.
                                    CanPacket cm = new CanPacket(iBuff, (uint)startIndex + 1);
                                    // S�tt ny startindex och avsluta loop.
                                    startIndex += PACKET_LENGTH;
                                    cp = cm;
                                    // och i slutet g�ra en array copy.
                                    // Flytta ner allt efter slutbyte till index 0 i array.
                                    Array.Copy(iBuff, startIndex, iBuff, 0, iBuffPointer - PACKET_LENGTH);
                                    iBuffPointer -= startIndex;

                                    return true;
                                }
                            }
                        }
                    }
                    // och i slutet g�ra en array copy.
                    // Flytta ner allt efter slutbyte till index 0 i array.
                    Array.Copy(iBuff, startIndex, iBuff, 0, iBuffPointer - PACKET_LENGTH);
                    iBuffPointer -= startIndex;
                }
                return false;

            } else {
                return false;
            }
        }
コード例 #3
0
ファイル: Downloader.cs プロジェクト: Cougar/HomeAutomation
        public void downloader()
        {
            dState pgs = dState.SEND_START;
            int t = 0;
            CanPacket outCm = null;
            CanPacket cm = null;
            byte[] data = new byte[8];
            uint byteSent = 0;
            ulong currentAddress = 0;
            bool done = false;
            bool errorOccured = false;

            try {
                if (isBiosUpdate) {
                    dlOffset = hf.getAddrLower();
                    //Console.WriteLine(dlOffset);
                    //dlOffset = 200;
                }
                while (!done) {
                    bool hasMessage = sc.getPacket(out cm);
                    if (hasMessage) {
                        Array.Copy(cm.getData(), data, 8);
                    }

                    switch (pgs) {
                        case dState.SEND_START:
                            // Send boot start packet to target.
                            // and wait for ack.
                            currentAddress = hf.getAddrLower();
                            //data[RID_INDEX] = TARGET_ID;
                            data[ADDR0_INDEX] = (byte)((currentAddress-dlOffset) & 0x0000FF);
                            data[ADDR1_INDEX] = (byte)(((currentAddress-dlOffset) & 0x00FF00) >> 8);
                            data[ADDR2_INDEX] = (byte)(((currentAddress-dlOffset) & 0xFF0000) >> 16);
                            data[ADDR3_INDEX] = 0;
                            outCm = new CanPacket(CAN_NMT, CAN_NMT_PGM_START, MY_ID, receiveID, 4, data);
                            sc.writePacket(outCm);
                            t = Environment.TickCount;
                            pgs = dState.WAIT_ACK_PRG;
                            timeStart = Environment.TickCount;
                            break;

                        case dState.WAIT_ACK_PRG:
                            // Check for timeout, resend start packet in that case..
                            if ((Environment.TickCount - t) > TIMEOUT_MS) {
                                Console.WriteLine("Timeout while waiting for start prg ack.");
                                errorOccured = true;
                                pgs = dState.SEND_RESET;
                            }
                            Thread.Sleep(1);
                            // If received ack.
                            if (hasMessage && cm.getPktClass() == CAN_NMT && cm.getSid() == receiveID) {
                                // If no error
                                if (cm.getType() == CAN_NMT_PGM_ACK) {
                                    // Start sending program data..
                                    byteSent = 0;
                                    pgs = dState.SEND_PGM_DATA;
                                }
                                else if (cm.getType() == CAN_NMT_PGM_NACK ) {
                                    // else ..
                                    Console.WriteLine("Nack on CAN_NMT_PGM_START.");
                                    errorOccured = true;
                                    pgs = dState.SEND_RESET;
                                }
                            }

                            break;

                        case dState.SEND_PGM_DATA:
                            //Console.WriteLine("byteSent = "+byteSent);
                            Console.Write(".");
                            // Send program data.
                            byte datalength = 2;
                            for (ulong i = 0; i < 6; i++) {
                                // Populate data.
                                data[i+2] = hf.getByte(currentAddress + i + byteSent);
                                if (currentAddress + i + byteSent > hf.getAddrUpper()) {
                                    break;	//then done
                                }
                                datalength++;
                            }
                            data[0] = (byte)(byteSent & 0x00FF);
                            data[1] = (byte)((byteSent & 0xFF00) >> 8);
                            outCm = new CanPacket(CAN_NMT, CAN_NMT_PGM_DATA, MY_ID, receiveID, datalength, data);
                            sc.writePacket(outCm);
                            t = Environment.TickCount;
                            byteSent += 6;
                            pgs = dState.WAIT_ACK_DATA;
                            break;

                        case dState.WAIT_ACK_DATA:
                            // Wait for pgm ack.
                            if ((Environment.TickCount - t) > 2*TIMEOUT_MS) {
                                // Woops, error.
                                Console.WriteLine("Timeout while waiting for data ack.");
                                errorOccured = true;
                                pgs = dState.SEND_RESET;
                            }
                            Thread.Sleep(1);
                            // If received ack.
                            if (hasMessage && cm.getPktClass() == CAN_NMT && cm.getSid() == receiveID) {
                                // If no error
                                if (cm.getType() == CAN_NMT_PGM_ACK) {
                                    //currentAddress += 2;
                                    // Check if end.
                                    if (currentAddress + byteSent > hf.getAddrUpper()) {
                                        // Yes, we are done, send done.
                                        pgs = dState.SEND_DONE;
                                    }
                                    else {
                                        // More to write.
                                        //byteSent = 0;
                                        pgs = dState.SEND_PGM_DATA;
                                    }
                                }
                                else if (cm.getType() == CAN_NMT_PGM_NACK) {
                                    // else ..
                                    Console.WriteLine("Nack on CAN_NMT_PGM_DATA.");
                                    errorOccured = true;
                                    pgs = dState.SEND_RESET;
                                }
                            }

                            break;

                        case dState.SEND_DONE:
                            // Send done
                            //data[0] = 0;
                            //data[1] = 0;
                            outCm = new CanPacket(CAN_NMT, CAN_NMT_PGM_END, MY_ID, receiveID, 0, data);
                            sc.writePacket(outCm);
                            t = Environment.TickCount;
                            pgs = dState.WAIT_DONE;
                            break;

                        case dState.WAIT_DONE:
                            // Check for timeout, resend done packet in that case..
                            if ((Environment.TickCount - t) > TIMEOUT_MS) {
                                Console.WriteLine("Timeout while waiting for end prg ack.");
                                errorOccured = true;
                                pgs = dState.SEND_RESET;
                            }
                            Thread.Sleep(1);
                            // If received ack.
                            if (hasMessage && cm.getPktClass() == CAN_NMT && cm.getSid() == receiveID) {
                                // If no error
                                if (cm.getType() == CAN_NMT_PGM_ACK) {
                                    Console.WriteLine("");
                                    //
                                    int crc = calcCRC(hf);

                                    if (cm.getDataLength() == 2) {
                                        byte[] cmdata = cm.getData();
                                        if ((cmdata[0] == (crc & 0xFF)) && (cmdata[1] == (crc>>8))) {
                                            if (!isBiosUpdate) {
                                                Console.WriteLine("Done, successfully programmed application.");
                                                pgs = dState.SEND_RESET;
                                            } else {
                                                Console.WriteLine("Done, successfully programmed bios to application space.");
                                                pgs = dState.SEND_BIOS_UPDATE;
                                            }
                                        } else {
                                            string nodecrc = String.Format("{0:x4}", (int)cmdata[0] + (int)(cmdata[1]<<8));
                                            nodecrc = "0x"+nodecrc;
                                            string realcrc = String.Format("{0:x4}", (int)(crc & 0xFF) + (int)(crc & 0xFF00));
                                            realcrc = "0x"+realcrc;
                                            Console.WriteLine("CRC failed. Node sent CRC "+nodecrc+" but should be "+realcrc+".");
                                            errorOccured = true;
                                            pgs = dState.SEND_RESET;
                                        }

                                        byteSent = 0;
                                    }
                                    //Console.WriteLine("crc: " + crc + " " + (crc>>8) + " " + (crc & 0xFF));
                                    //Console.WriteLine("ACKpkg with CRC: " + cm.ToString());
                                    //
                                }
                                else if (cm.getType() == CAN_NMT_PGM_NACK) {
                                    // else resend addr..
                                    Console.WriteLine("Nack on CAN_NMT_PGM_END.");
                                    errorOccured = true;
                                    pgs = dState.SEND_RESET;
                                }
                            }
                            break;

                        case dState.SEND_BIOS_UPDATE:
                            data[0] = (byte)(0 & 0x00FF);
                            data[1] = (byte)((0 & 0xFF00) >> 8);
                            data[2] = (byte)(dlOffset & 0x00FF);
                            data[3] = (byte)((dlOffset & 0xFF00) >> 8);
                            data[4] = (byte)((hf.getAddrUpper()-hf.getAddrLower()+1) & 0x00FF);
                            data[5] = (byte)(((hf.getAddrUpper()-hf.getAddrLower()+1) & 0xFF00) >> 8);
                            outCm = new CanPacket(CAN_NMT, CAN_NMT_PGM_COPY, MY_ID, receiveID, 6, data);
                            Console.WriteLine("Now say a prayer, moving bios");
                            sc.writePacket(outCm);
                            t = Environment.TickCount;
                            pgs = dState.DONE;
                            break;

                        case dState.SEND_RESET:
                            // Send reset
                            outCm = new CanPacket(CAN_NMT, CAN_NMT_RESET, MY_ID, receiveID, 0, data);
                            sc.writePacket(outCm);
                            t = Environment.TickCount;
                            pgs = dState.DONE;
                            break;

                        case dState.DONE:
                            done = true;
                            //down.Abort();
                            if (!errorOccured) {
                                long tsecs = ((Environment.TickCount - timeStart) / 1000);
                                double bitRate = hf.getLength() / ((double)(Environment.TickCount - timeStart) / 1000.0);

                                if (tsecs <= 0) tsecs = 1;
                                long mins = tsecs / 60;
                                long secs = tsecs % 60;
                                string pad = "";
                                if (secs < 10) {
                                    pad = "0";
                                }
                                Console.WriteLine("Time spent: " + mins + ":" + pad + secs + ", " + Math.Round(bitRate, 2) + " bytes/s");
                            }
                            //Console.Write("> ");
                            break;
                    }
                }

            } catch(Exception) {
                Console.WriteLine("Aborted or done.");
                //Console.WriteLine(e);
                //skriva ut antal bytes skrivna vore bra här, kanske nån annan form av hjälp för debug, något gick snett liksom

                Console.Write("> ");
            }
        }
コード例 #4
0
        public bool writePacket(CanPacket cp)
        {
            if (serial_conn != null) {
                if (serial_conn.IsOpen) {
                    byte[] bytes = new byte[PACKET_LENGTH];
                    Array.Copy(cp.getBytes(), 0, bytes, 1, PACKET_LENGTH - 2);
                    bytes[0] = UART_START_BYTE;
                    bytes[PACKET_LENGTH - 1] = UART_END_BYTE;
                    serial_conn.Write(bytes, 0, PACKET_LENGTH);
                    return true;
                }
                return false;
            } else if (udpserver != null) {
                byte[] bytes = new byte[PACKET_LENGTH];
                Array.Copy(cp.getBytes(), 0, bytes, 1, PACKET_LENGTH - 2);
                bytes[0] = UART_START_BYTE;
                bytes[PACKET_LENGTH - 1] = UART_END_BYTE;

                udpserver.SendTo(bytes, bytes.Length, SocketFlags.None, new IPEndPoint(IPAddress.Parse(remoteIP), remotePort));

                return true;
            } else {
                return false;
            }
        }