Exemplo n.º 1
0
        private void GoBurn()
        {
            UpdateInfo("偵測舵機");
            UpdateLog("偵測舵機.\r\n=> ", true);
            bool waitReply = true;
            int  sendCnt   = 0;
            int  maxSend   = 500;
            byte lastByte  = 0;

            while (waitReply)
            {
                if (sendCnt > maxSend)
                {
                    UpdateLog("\r\n\r\n偵測超時: 舵機沒回應", true);
                    UpdateInfo("偵測超時: 舵機沒回應", UTIL.InfoType.error);
                    return;
                }

                SerialSendByte(0xA5, true);
                Thread.Sleep(10);
                sendCnt++;

                while (receiveBuffer.Count > 0)
                {
                    UpdateLog("\r\n<= ", true);
                    lastByte = receiveBuffer[0];
                    UpdateLog(String.Format(" {0:X2}", lastByte), true);
                    // Just for safety, differnt response from servo, A0 & A1 may cause by previous incomplete write
                    if ((lastByte == 0xA9) || (lastByte == 0xE9) || (lastByte == 0xA1) || (lastByte == 0xA0))
                    {
                        waitReply = false;
                    }
                    receiveBuffer.RemoveAt(0);
                }
            }

            if ((lastByte == 0xA0) || (lastByte == 0xA1))
            {
                string s1 = "偵測到之前燒錄未完成";
                string s2 = "安全起見, 請重置舵機, 再次重新燒錄.";

                UpdateLog(string.Format("\r\n\r\n!!!! {0} !!!!\r\n{1}", s1, s2), true);
                MessageBox.Show(s2, s1);
                return;
            }
            UpdateInfo("燒錄進行中, 請耐心等待......");
            UpdateLog("\r\n\r\n開始燒錄......\r\n", true);
            double nPage   = sendData.Count;
            byte   endPage = sendData.EndPage;

            if (endPage < 0x7F)
            {
                nPage += (0x7F - endPage);
            }
            for (int i = 0; i < sendData.Count; i++)
            {
                Stm8HexData hd = sendData.GetHexData(i);
                if (!SendHexDate(hd, i))
                {
                    return;
                }
                UpdatePorgress((double)100.0 * i / nPage);
            }
            for (int i = endPage + 1; i < 0x80; i++)
            {
                Stm8HexData hd = new Stm8HexData(i);
                hd.Reset();
                if (!SendHexDate(hd, 999))
                {
                    return;
                }
                UpdatePorgress((double)100.0 * (sendData.Count + i) / nPage);
            }
            UpdatePorgress(100);

            UpdateLog("\r\n=> ");
            SerialSendByte(0xA9);
            if (WaitServoReturn(out lastByte))
            {
                if (lastByte == 0xA0)
                {
                    UpdateLog("\r\n\r\n燒錄完成\r\n", true);
                    UpdateInfo("燒錄順利完成");
                    return;
                }
            }

            UpdateLog("\r\n\r\n燒錄完成, 但終結的 0xA9 沒回傳 A0\r\n", true);
            UpdateInfo("燒錄完成, 但終結的 0xA9 沒回傳");
            return;
        }
Exemplo n.º 2
0
        private void GoBurn()
        {
            writeSend = true;
            UpdateInfo("偵測舵機");
            UpdateLog("偵測舵機.\r\n=> ", true);
            bool waitReply = true;
            int  sendCnt   = 0;
            int  maxSend   = 500;
            byte lastByte  = 0;

            receiveBuffer.Clear();
            while (waitReply)
            {
                if (sendCnt > maxSend)
                {
                    UpdateLog("\r\n\r\n偵測超時: 舵機沒回應", true);
                    UpdateInfo("偵測超時: 舵機沒回應", MyUtil.UTIL.InfoType.error);
                    return;
                }

                if (!writeSend)
                {
                    writeSend = true;
                    UpdateLog("\r\n=> ", true);
                }
                SerialSendByte(0xA5, true);
                Thread.Sleep(10);
                sendCnt++;

                while (receiveBuffer.Count > 0)
                {
                    writeSend = false;
                    UpdateLog("\r\n<= ", true);
                    lastByte = receiveBuffer[0];
                    UpdateLog(String.Format(" {0:X2}", lastByte), true);
                    receiveBuffer.RemoveAt(0);

                    if ((lastByte == 0xA9) || (lastByte == 0xA1) || (lastByte == 0xA0))
                    {
                        waitReply = false;
                        break;
                    }
                }
            }

            // cleanup buffer, just for safety
            Thread.Sleep(1000);
            if (receiveBuffer.Count > 0)
            {
                for (int i = 0; i < receiveBuffer.Count; i++)
                {
                    UpdateLog(String.Format(" {0:X2}", receiveBuffer[i]), true);
                }
            }
            receiveBuffer.Clear();

            // for very very very rare case, the data can complete a previous transaction, and got a A0 return.
            // It's almost mission impossible, just keep the logic for safety check
            if (lastByte == 0xA0)
            {
                string s1 = "偵測到之前燒錄未完成";
                string s2 = "安全起見, 請重置舵機, 再次重新燒錄.";

                UpdateLog(string.Format("\r\n\r\n!!!! {0} !!!!\r\n{1}", s1, s2), true);
                MessageBox.Show(s2, s1);
                return;
            }

            UpdateInfo("燒錄進行中, 請耐心等待......");
            UpdateLog("\r\n\r\n開始燒錄......\r\n", true);
            double nPage   = sendData.Count;
            byte   endPage = sendData.EndPage;

            if (endPage < 0x7F)
            {
                nPage += (0x7F - endPage);
            }
            for (int i = 0; i < sendData.Count; i++)
            {
                Stm8HexData hd = sendData.GetHexData(i);
                if (!SendHexDate(hd, i))
                {
                    UpdateInfo(string.Format("Error sending page {0}", i));
                    return;
                }
                UpdatePorgress((double)100.0 * i / nPage);
            }
            for (int i = endPage + 1; i < 0x80; i++)
            {
                Stm8HexData hd = new Stm8HexData(i);
                hd.Reset();
                if (!SendHexDate(hd, 999))
                {
                    UpdateInfo("Error sending last page");
                    return;
                }
                UpdatePorgress((double)100.0 * (sendData.Count + i) / nPage);
            }
            UpdatePorgress(100);

            UpdateLog("\r\n=> ");
            SerialSendByte(0xA9);

            /*
             * if (WaitServoReturn(out lastByte))
             * {
             *  if (lastByte == 0xA0)
             *  {
             *      UpdateLog("\r\n\r\n燒錄順利完成\r\n", true);
             *      UpdateInfo("燒錄順利完成");
             *      return;
             *  }
             * }
             */
            UpdateLog("\r\n\r\n燒錄完成\r\n", true);
            UpdateInfo("燒錄完成");
            return;
        }