Example #1
0
        private void buttonReplay_Click(object sender, EventArgs e)
        {
            if (usbCAN.startDevice() == false)
            {
                return;
            }

            OpenFileDialog open = new OpenFileDialog();

            if (open.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            REPLAYPARAM param = new REPLAYPARAM();

            param.sr = new StreamReader(open.FileName);
            Thread threadReplay = new Thread(new ParameterizedThreadStart(replayThreading));
            int    lines        = 0;

            while (param.sr.ReadLine() != null)
            {
                lines++;
            }
            param.mrEvent = new ManualResetEvent(false);

            param.bar = new Form_ProgressBar(lines, param.mrEvent, threadReplay);

            param.sr.BaseStream.Seek(0, SeekOrigin.Begin);
            threadReplay.Start(param);
            param.bar.ShowDialog();
            if (param.bar.returnResult == DialogResult.Abort)
            {
                MessageBox.Show("你终止了操作");
            }
            else if (param.bar.returnResult == DialogResult.OK)
            {
                MessageBox.Show("重放结束");
            }
            else
            {
                MessageBox.Show("重放文件格式出错");
            }

            threadReplay.Abort();
            threadReplay = null;

            param.sr.Close();
        }
Example #2
0
        unsafe void replayThreading(object replayParm)
        {
            REPLAYPARAM      parm = (REPLAYPARAM)replayParm;
            StreamReader     sr   = parm.sr;
            Form_ProgressBar form = parm.bar;

            parm.mrEvent.WaitOne();

            string line = null;

            line = sr.ReadLine();
            if (line == null)
            {
                goto endProcess;
            }

            Int64 timePrev, timeNow;

            string[] arr = line.Split(' ');
            try { usbCAN.arrSendBuf[0].ID = Convert.ToUInt32(arr[1], 16); }
            catch (Exception) { goto wrongFormat; }

            try { usbCAN.arrSendBuf[0].DataLen = Convert.ToByte(arr[2]); }
            catch (Exception) { goto wrongFormat; }

            try
            {
                for (int d = 0; d < 8; d++)
                {
                    usbCAN.arrSendBuf[0].Data[d] = Convert.ToByte(arr[d + 3], 16);
                }
            }
            catch (Exception) { goto wrongFormat; }

            if (arr[11].Equals("扩展帧"))
            {
                usbCAN.arrSendBuf[0].ExternFlag = 1;
            }
            else if (arr[11].Equals("标准帧"))
            {
                usbCAN.arrSendBuf[0].ExternFlag = 0;
            }
            else
            {
                goto wrongFormat;
            }

            if (arr[12].Equals("远程帧"))
            {
                usbCAN.arrSendBuf[0].RemoteFlag = 1;
            }
            else if (arr[12].Equals("数据帧"))
            {
                usbCAN.arrSendBuf[0].RemoteFlag = 0;
            }
            else
            {
                goto wrongFormat;
            }

            try { timePrev = Convert.ToInt64(arr[0]); }
            catch (Exception) { goto wrongFormat; }

            usbCAN.sendFame(1);
            form.addValue();

            while ((line = sr.ReadLine()) != null)
            {
                arr = line.Split(' ');
                try { timeNow = Convert.ToInt64(arr[0]); }
                catch (Exception) { goto wrongFormat; }

                try { usbCAN.arrSendBuf[0].ID = Convert.ToUInt32(arr[1], 16); }
                catch (Exception) { goto wrongFormat; }


                try { usbCAN.arrSendBuf[0].DataLen = Convert.ToByte(arr[2]); }
                catch (Exception) { goto wrongFormat; }

                try
                {
                    for (int d = 0; d < 8; d++)
                    {
                        usbCAN.arrSendBuf[0].Data[d] = Convert.ToByte(arr[d + 3], 16);
                    }
                }
                catch (Exception) { goto wrongFormat; }

                if (arr[11].Equals("扩展帧"))
                {
                    usbCAN.arrSendBuf[0].ExternFlag = 1;
                }
                else if (arr[11].Equals("标准帧"))
                {
                    usbCAN.arrSendBuf[0].ExternFlag = 0;
                }
                else
                {
                    goto wrongFormat;
                }

                if (arr[12].Equals("远程帧"))
                {
                    usbCAN.arrSendBuf[0].RemoteFlag = 1;
                }
                else if (arr[12].Equals("数据帧"))
                {
                    usbCAN.arrSendBuf[0].RemoteFlag = 0;
                }
                else
                {
                    goto wrongFormat;
                }

                Int64 delay = timeNow - timePrev;
                if (delay > 10)
                {
                    Thread.Sleep(Convert.ToInt32(delay * 0.1));
                }

                timePrev = timeNow;
                usbCAN.sendFame(1);
                form.addValue();
            }

endProcess:
            form.returnResult = DialogResult.OK;
            form.Invoke(new Action(() => { form.Close(); }));
            return;

wrongFormat:
            form.returnResult = DialogResult.No;
            form.Invoke(new Action(() => { form.Close(); }));
        }