Beispiel #1
0
        public void TryDeleteDll()
        {
            if (File.Exists(fast_printf_dll_address) == true)
            {
                string del_bat_address = @".\Del.bat";

                //将内嵌的资源释放到临时目录下
                FileStream str = new FileStream(del_bat_address, FileMode.OpenOrCreate);
                str.Write(Encoding.ASCII.GetBytes(Properties.Resources.Del), 0, Properties.Resources.Del.Length);
                str.Close();

                Process proc = null;    //批处理可以删除自己!
                try
                {
                    string targetDir = string.Format(@".\");//this is where testChange.bat lies
                    proc = new Process();
                    proc.StartInfo.WorkingDirectory = targetDir;
                    proc.StartInfo.FileName         = "Del.bat";
                    //proc.StartInfo.Arguments = string.Format("10");//this is argument
                    //proc.StartInfo.CreateNoWindow = true;
                    proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;//这里设置DOS窗口不显示,经实践可行

                    proc.Start();
                    //proc.WaitForExit();
                }
                catch (Exception ex)
                {
                    Dbg.WriteLine("Exception Occurred :{0},{1}", ex.Message, ex.StackTrace.ToString());
                }
            }
        }
Beispiel #2
0
        void timer_RcvFlush_Tick(object sender, EventArgs e)
        {
            timer_RcvFlush.Enabled = false;

            //空闲的时候把最后的数据刷到窗体上
            if ((current_rnode != null) && (rcv_recving == false))
            {
                rcv_flushing = true;

                Dbg.WriteLine("Flush rcv data:{0} rcv:{1} sp:{2}", current_rnode.length, rcv_recving,
                              Func.RTC_TimeSpan_MS(last_rcv_data_time));

                if (current_rnode.length > 0)
                {
                    if (efifo_raw_2_str.is_full == true)
                    {
                        Dbg.WriteLine("###3.COM:{0} recv fifo is full:{1}, data miss!!!",
                                      serialport.IsOpen, efifo_raw_2_str.GetValidNum());
                        return;
                    }

                    efifo_raw_2_str.Input(current_rnode);
                    current_rnode = null;

                    event_recv.Set();
                }

                rcv_flushing = false;
            }
        }
Beispiel #3
0
        public T Output()
        {
            T data;

            lock (efifo_lock)
            {
                data = buffer[bottom];

#if SUPPORT_SHOW_FIFO_DATA
                Dbg.WriteLine("out:{0}({1}:{2})", value, top, bottom);
                for (int i = 0; i < buffer_value[bottom]; i++)
                {
                    Dbg.Write(" {0}", buffer_data[bottom][i]);
                }
                Dbg.WriteLine("({0}:{1})", top, bottom);
#endif

                is_full = false;
                bottom++;
                if (bottom >= max_number)
                {
                    bottom = 0;
                }
            }

            return(data);
        }
Beispiel #4
0
        public void Input(T data)
        {
            lock (efifo_lock)
            {
                buffer[top] = data;

#if SUPPORT_SHOW_FIFO_DATA
                Dbg.WriteLine("in:{0}({1}:{2})", value, top, bottom);
                for (int i = 0; i < buffer_value[top]; i++)
                {
                    Dbg.Write(" {0}", buffer_data[top][i]);
                }
                Dbg.WriteLine("({0}:{1})", top, bottom);
#endif
                top++;

                if (top >= max_number)
                {
                    top = 0;
                }
                if (top == bottom)   //如果头部赶上尾部,则FIFO已满
                {
                    is_full = true;
                }
            }
        }
Beispiel #5
0
        public void SendData(byte[] snd_buff)
        {
            Dbg.Assert(snd_buff.Length > 0, "Send length can not be empty!");

            if (is_active == false)
            {
                Enter_MessageQueue(is_server, false, "Client is not connected, return!");
                return;
            }

#if SUPPORT_SHOW_LEN
            Dbg.WriteLine("Snd:{0}", snd_buff.Length);
#endif

#if SUPPORT_SHOW_DATA
            Func.DumpBuffer(snd_buff, snd_buff.Length);
#endif

            try
            {
                if (is_server == true)
                {
                    bw_server_write_to_client.Write(snd_buff);              //向客户端发送字符串
                }
                else
                {
                    bw_client_write_to_server.Write(snd_buff);              //向服务器发送字符串
                }
            }
            catch (Exception ex)
            {
                Enter_MessageQueue(is_server, false, "Tcp break, send fail! " + ex.Message);
            }
        }
Beispiel #6
0
        static public bool Open(SerialPort sp)
        {
            Dbg.WriteLine("PortName:{0}", sp.PortName);
            Dbg.WriteLine("Baudrate:{0}", sp.BaudRate);
            Dbg.WriteLine("Parity:{0}", sp.Parity);
            Dbg.WriteLine("Data:{0}", sp.DataBits);
            Dbg.WriteLine("Stop:{0}", sp.StopBits);

            if ((sp.PortName == "null") ||
                (sp.BaudRate == 1) ||
                (sp.Parity == Parity.Space) ||
                (sp.DataBits == 1))
            {
                MessageBox.Show("Please choose the COM port" + Dbg.GetStack(), "Attention!");
                return(false);
            }

            try
            {
                sp.Open();
                sp.DiscardInBuffer();
                sp.DiscardOutBuffer();
            }
            catch (Exception ex)
            {
                //DebugIF.Assert(false, "###TODO: Why can not open COM " + ex.Message);
                MessageBox.Show(ex.Message + Dbg.GetStack(), "Attention!");
                return(false);
            }

            return(true);
        }
Beispiel #7
0
        public void Init(string str_fp_hex0_path, string str_fp_hex1_path)
        {
            hex0_path = str_fp_hex0_path;
            hex1_path = str_fp_hex1_path;

            Dbg.WriteLine("HEX0:{0}", hex0_path);
            Dbg.WriteLine("HEX1:{0}", hex1_path);

            Form_Main.main_form.button_FPSelect_HEX.Text  = "";
            Form_Main.main_form.button_FPSelect_HEX.Text += "FP HEX0 path: " + hex0_path;
            Form_Main.main_form.button_FPSelect_HEX.Text += "\r\nFP HEX0 path: " + hex1_path;

#if false       //通过调用批处理去删除dll!
            try //不使用FastPrintf的话就把dll删掉,c#没办法调用静态库,又不能卸载dll,所以做不到关闭窗体时删除dll,比较蛋疼
            {
                File.SetAttributes(fast_printf_dll_address, FileAttributes.Normal);
                File.Delete(fast_printf_dll_address);
            }
            //catch(Exception ex)
            //{
            //    Enter_MessageQueue(ex.Message + "   FastPrintf dll clears!!!");
            //}
            catch
            {
                Enter_MessageQueue("FastPrintf.dll clear failed");
            }
#endif
        }
Beispiel #8
0
        public void HandleKeyData(SerialPort com, Keys keyData)
        {
            Key_To_ASCII(keyData);

            Dbg.WriteLine(false, "SEND>>");
            while (true)
            {
                if (Console_FIFO_Chk() == true)
                {
                    Byte ascii_code = Console_FIFO_Output();
                    Dbg.WriteLine(false, "{0:X}", ascii_code);

                    if (ascii_code != 0)
                    {
                        string str = "";
                        str += (char)ascii_code;

                        try
                        {
                            com.Write(str);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message + Dbg.GetStack(), "Warning!");
                        }
                    }
                }
                else
                {
                    break;
                }
            }
        }
Beispiel #9
0
        public void Enter_MessageQueue(bool _is_server, bool box, string str)
        {
            string role = "";

            if (_is_server == true)
            {
                role = "[TCP Server]:";
            }
            else
            {
                role = "[TCP Client]:";
            }

            if (box == true)
            {
                MessageBox.Show(str, role);
            }
            else
            {
                Dbg.WriteLine(role + str);
            }
            role = role + str;

            Dbg.queue_message.Enqueue(role);
        }
Beispiel #10
0
 private void DbgDumpLog()
 {
     Dbg.WriteLine("Final cnt:%", dbg_cnt);
     for (int i = 0; i < 1024; i++)
     {
         Dbg.WriteLine("i:% op:% got:%", i, dbg_op_log[i], dbg_got_log[i]);
     }
 }
Beispiel #11
0
 public void ShowDebugInfo()
 {
     Dbg.WriteLine("****************Dump KCOM status(%):********************", DateTime.Now.ToString("yy/MM/dd HH:mm:ss"));
     Dbg.WriteLine("check_thread_txtupdate:% Step:% Active:%", Form_Main.check_thread_txtupdate, Form_Main.step_thread_txtupdate, thread_txt_update.IsAlive);
     Dbg.WriteLine("epool_show.got:%", epool_show.nr_got);
     Dbg.WriteLine("eFIFO_str_2_show. Top:% Bottom:% Full:%", efifo_str_2_show.top, efifo_str_2_show.bottom, efifo_str_2_show.is_full);
     Dbg.WriteLine("check_thread_ComRecv:% Step:% Active:%", check_thread_ComRecv, step_thread_ComRecv, thread_recv.IsAlive);
     Dbg.WriteLine("epool_rcv.got:%", epool_rcv.nr_got);
     Dbg.WriteLine("efifo_raw_2_str. Top:% Bottom:% Full:%", efifo_raw_2_str.top, efifo_raw_2_str.bottom, efifo_raw_2_str.is_full);
     Dbg.WriteLine("*****************************************************");
     Dbg.WriteLine("");
 }
Beispiel #12
0
 public static void DumpBuffer(byte[] buffer, int length)
 {
     for (int i = 0; i < length; i++)
     {
         if (i % 16 == 0)
         {
             Dbg.WriteLine("");
         }
         Dbg.WriteLine(false, "{0:x2} ", buffer[i]);
     }
     Dbg.WriteLine("");
 }
Beispiel #13
0
        //为了提高串口显示刷新时间,定时器的周期调整为100ms
        private void timer_backgroud_Tick(object sender, EventArgs e)
        {
            label_com_running.Text = DateTime.Now.ToString("yy/MM/dd HH:mm:ss");

            if ((fp.is_active == true) && (check_hex_change_cnt % 10 == 0))  //1s检查一次
            {
                fp.Check_Hex_Change();
            }
            check_hex_change_cnt++;

            if (OneSecondCount % 100 == 99)
            {
                Dbg.WriteLine("****************Dump KCOM status(%):********************", DateTime.Now.ToString("yy/MM/dd HH:mm:ss"));
                Dbg.WriteLine("check_thread_txtupdate:% Step:% Active:%", check_thread_txtupdate, step_thread_txtupdate, com.thread_txt_update.IsAlive);
                Dbg.WriteLine("epool_show.got:%", com.epool_show.nr_got);
                Dbg.WriteLine("eFIFO_str_2_show. Top:% Bottom:% Full:%", com.efifo_str_2_show.top, com.efifo_str_2_show.bottom, com.efifo_str_2_show.is_full);
                Dbg.WriteLine("check_thread_ComRecv:% Step:% Active:%", com.check_thread_ComRecv, com.step_thread_ComRecv, com.thread_recv.IsAlive);
                Dbg.WriteLine("epool_rcv.got:%", com.epool_rcv.nr_got);
                Dbg.WriteLine("efifo_raw_2_str. Top:% Bottom:% Full:%", com.efifo_raw_2_str.top, com.efifo_raw_2_str.bottom, com.efifo_raw_2_str.is_full);
                Dbg.WriteLine("*****************************************************");
                Dbg.WriteLine("");
            }
            OneSecondCount++;

            if (program_is_close == true)
            {
                program_is_close = false;
                Func_ProgramClose();
            }

            com.Display(label_Rec_Bytes, label_DataRemain, label_MissData,
                        label_Send_Bytes, label_Speed, timer_backgroud.Interval);

            if (Dbg.queue_message.Count > 0)
            {
                string message;
                if (Dbg.queue_message.TryDequeue(out message))
                {
                    textBox_Message.AppendText("\r\n" + DateTime.Now.ToString("yy/MM/dd HH:mm:ss") + message);
                    fp.message_cnt++;
                }
            }

            /***********************网络相关 START***************************/
            int _recv_length = 0;

            byte[] rcv_data = etcp.GetRcvBuffer(ref _recv_length);
            if (_recv_length > 0)
            {
                com.DataHandle(rcv_data, _recv_length, false);
            }
            /***********************网络相关 END*****************************/
        }
Beispiel #14
0
        void RcvData_Handle(byte[] buffer, int length)
        {
            string str = System.Text.Encoding.ASCII.GetString(sync_rcv_buffer, 0, sync_rcv_count);

            Dbg.WriteLine("SyncRcv handle. Len:% Str:%\n", sync_rcv_count, str);

            string append_str = "";

            append_str += "[" + DateTime.Now.ToString("yy/MM/dd HH:mm:ss.fff") + "]";
            append_str += "SyncCOM Rcv: " + sync_rcv_count.ToString() + "\r\n";
            append_str += str;
            append_str += "\r\n\r\n";

            Update_TextBox(append_str, COM.tyShowOp.APPEND);

            if (SCOM.run_ecmd == false)
            {
                if (main_port.IsOpen == true)                                //通过串口1透传出去
                {
                    try
                    {
                        main_port.Write(sync_rcv_buffer, 0, sync_rcv_count);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message + Dbg.GetStack(), "#Sync send data error!");
                    }
                }
            }
            else if (str == RunEXE.str_run_exe_code)
            {
                RunEXE.Run_EXE();
            }
            else
            {
                string ext_name = Func.Get_ExternName(str);
                if ((ext_name != null) &&
                    (Func.Char_String_compare(ext_name.ToCharArray(), "cmd", 3) == true))
                {
                    string str_cmd = Func.Get_FileName(str);

                    Dbg.WriteLine("ext name:%", ext_name);
                    Dbg.WriteLine("file name:%", str_cmd);

                    Update_TextBox("Run CMD\r\n", COM.tyShowOp.APPEND);

                    str = RunEXE.RunCMD(str_cmd);

                    Update_TextBox(str, COM.tyShowOp.APPEND);
                }
            }
        }
Beispiel #15
0
        void timer_RcvTO_Tick(object sender, EventArgs e)
        {
#if false
            for (int i = 0; i < sync_rcv_count; i++)
            {
                Dbg.WriteLine("[%]:%", i, sync_rcv_buffer[i]);
            }
#endif

            RcvData_Handle(sync_rcv_buffer, sync_rcv_count);

            sync_rcv_count = 0;
        }
Beispiel #16
0
        private void textBox_ComRec_KeyDown(object sender, KeyEventArgs e)
        {
            Dbg.WriteLine("[KEY]:{0} {1}", e.Control, e.KeyCode);
            if (e.Control && e.KeyCode == Keys.A)                //Ctrl + A 全选
            {
                textBox_ComRec.SelectAll();
            }

            //使用鼠标中键清空,ESC容易被切屏软件误触发
            if ((e.KeyCode == Keys.Escape) && (main_com.cfg.esc_clear_data == true))
            {
                main_com.ClearRec();
            }
        }
Beispiel #17
0
 static public void Ruild_ComNumberList(ComboBox _comboBox_COMNumber)
 {
     _comboBox_COMNumber.Items.Clear();
     string[] strArr = Func.GetHarewareInfo(Func.HardwareEnum.Win32_PnPEntity, "Name");
     foreach (string vPortName in SerialPort.GetPortNames())
     {
         string SerialIn = "";
         SerialIn += vPortName;
         SerialIn += ':';
         foreach (string s in strArr)
         {
             if (s.Contains(vPortName))
             {
                 SerialIn += s;
             }
         }
         Dbg.WriteLine(SerialIn);
         _comboBox_COMNumber.Items.Add(SerialIn);                    //将设备列表里的COM放进下拉菜单上
     }
 }
Beispiel #18
0
        static void Close(SerialPort sp)
        {
            com_is_closing = true;

            /****************串口异常断开则直接关闭窗体 Start**************/
            bool current_com_exist = false;

            string[] strArr = Func.GetHarewareInfo(Func.HardwareEnum.Win32_PnPEntity, "Name");
            foreach (string vPortName in SerialPort.GetPortNames())
            {
                if (vPortName == sp.PortName)
                {
                    current_com_exist = true;                               //当前串口还在设备列表里
                }
            }

            //关闭串口时发现正在使用的COM不见了,由于无法调用com.close(),所以只能异常退出了
            if (current_com_exist == false)
            {
                com_is_closing = false;
                //Dbg.Assert(false, "###TODO: Why can not close COM");
            }
            else
            {
                Dbg.WriteLine("COM is still here");
            }
            /****************串口异常断开则直接关闭窗体 End****************/

            try
            {
                sp.Close();
                Dbg.WriteLine("COM close ok");
            }
            catch (Exception ex)
            {
                com_is_closing = false;
                Dbg.Assert(false, "###TODO: Why can not close COM " + ex.Message);
            }

            com_is_closing = false;
        }
Beispiel #19
0
        //弹出第一个元素
        public eNode <T> PopTop()
        {
            if (IsEmpty())
            {
                Dbg.WriteLine("###eLink is empty");
                return(null);
            }

            lock (elink_lock)    //lock里面return也是会解锁的
            {
                nr_entry--;

                eNode <T> A = next_node;

                next_node = next_node.next;
                if (next_node == null)                                           //源sgl一个都没有了,则把last也清掉
                {
                    last_node = null;
                }

                return(A);
            }
        }
Beispiel #20
0
        void Func_ProgramClose()
        {
            if (main_com.serialport.IsOpen == true)
            {
                //COM_Op.Close(main_com.serialport);
                bool res = COM_Op.button_COMOpen_Click(main_com.serialport);
                SetComStatus(res);
            }

            fp.TryDeleteDll();
            if (fp.is_active == true)
            {
                fp.Close();
            }

            if (etcp.is_active == true)
            {
                etcp.Close();
            }

            RunEXE.Close();

            notifyIcon.Dispose();//释放notifyIcon1的所有资源,以保证托盘图标在程序关闭时立即消失

            //后台线程,不需要关闭了
            //thread_com_recv.Abort();
            //thread_Calx_output.Abort();
            //thread_net.Abort();
            main_com.thread_txt_update.Abort();  //必须要关闭该线程,否则关闭窗体时会失败

            Func_PropertiesSettingsSave();

            Dbg.WriteLine("Form Close done");
            //System.Environment.Exit(0);

            //MessageBox.Show("是否关闭KCOM", Func_GetStack("Attention"), MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
        }
Beispiel #21
0
        //为了提高串口显示刷新时间,定时器的周期调整为100ms
        private void timer_backgroud_Tick(object sender, EventArgs e)
        {
            label_RealTime.Text = DateTime.Now.ToString("yy/MM/dd HH:mm:ss");

            if ((fp.is_active == true) && (check_hex_change_cnt % 10 == 0))  //1s检查一次
            {
                fp.Check_Hex_Change();
            }
            check_hex_change_cnt++;

#if false
            if (OneSecondCount % 100 == 99)
            {
                main_com.ShowDebugInfo();
            }
            OneSecondCount++;
#endif

            if (program_is_close == true)
            {
                this.Close();
                program_is_close = false;
                Dbg.WriteLine("Do I come back?");
            }

            main_com.Display(label_Rec_Bytes, label_DataRemain, label_MissData,
                             label_Send_Bytes, label_Speed, timer_backgroud.Interval);

            /***********************网络相关 START***************************/
            int    _recv_length = 0;
            byte[] rcv_data     = etcp.GetRcvBuffer(ref _recv_length);
            if (_recv_length > 0)
            {
                main_com.DataHandle(rcv_data, _recv_length, false);
            }
            /***********************网络相关 END*****************************/
        }
Beispiel #22
0
        public void ThreadEntry_ClientRcv()
        {
            Enter_MessageQueue(is_server, false, "ThreadEntry_ClientRcv run");

            while (true)
            {
                IPEndPoint remoteEP = new IPEndPoint(IPAddress.Parse(str_ip), port);    //远程服务器端地址;
                remoteServer = new TcpClient();

                try
                {
                    remoteServer.Connect(remoteEP);                         //调用connect方法连接远端服务器;
                }
                catch (Exception ex)
                {
                    remoteServer.Close();
                    remoteServer = null;

                    Enter_MessageQueue(is_server, false, "Can't connect to server:" + str_ip + ", " + ex.Message);
                    Thread.Sleep(500);
                    continue;
                }

                is_active = true;

                network_stream_client      = remoteServer.GetStream();
                bw_client_read_from_server = new BinaryReader(network_stream_client);
                bw_client_write_to_server  = new BinaryWriter(network_stream_client);

                Enter_MessageQueue(is_server, false, "Connect to server successfully:" + str_ip);
                Dbg.WriteLine("I'm using {0}.", remoteServer.Client.LocalEndPoint); //打印自己使用的端地址

                while (true)
                {
                    /********************接收数据部分 Start******************/
                    int rcv_length;

                    PNode <tyNode> pnode = epool_rcv.Get();

                    tyNode nnode = pnode.obj;

                    try
                    {
                        //接受服务器发送过来的消息,注意Client已经把数据处理成字符串了!
                        rcv_length = bw_client_read_from_server.Read(nnode.buffer, 0, TCP_MAX_DATA_LEN);
                    }
                    catch (Exception ex)
                    {
                        epool_rcv.Put(pnode);
                        Enter_MessageQueue(is_server, false, "Server lost, Read fail!" + ex.Message);
                        is_active = false;
                        break;
                    }

#if SUPPORT_SHOW_LEN
                    Dbg.WriteLine("Client rcv:{0}", rcv_length);
#endif

#if SUPPORT_SHOW_DATA
                    Func.DumpBuffer(efifo_rcv.Peek(), rcv_length);
#endif
                    if (rcv_length == 0)
                    {
                        epool_rcv.Put(pnode);
                        Enter_MessageQueue(is_server, false, "Server lost, Read error!");
                        is_active = false;
                        break;
                    }
                    else
                    {
                        nnode.length = rcv_length;
                        efifo_rcv.Input(nnode);
                    }
                    /********************接收数据部分 End********************/
                }
            }

            //TcpMessage(is_server, false, "ThreadEntry_ClientRcv end");
        }
Beispiel #23
0
        public void ThreadEntry_ServerRcv()                                 //线程入口
        {
            Enter_MessageQueue(is_server, false, "ThreadEntry_ServerRcv run");

            while (true)
            {
                while (true)
                {
                    IPEndPoint localEP = new IPEndPoint(IPAddress.Parse(str_ip), port); //本地端地址
                    Listener = new TcpListener(localEP);                                //建立监听类,并绑定到指定的端地址

                    try
                    {
                        Listener.Start();                                               //开始监听
                    }
                    catch (Exception ex)
                    {
                        Listener.Stop();
                        Listener = null;

                        Enter_MessageQueue(is_server, false, "Error local IP:" + localEP.ToString() + "setup. " + ex.Message);

                        Thread.Sleep(500);
                        continue;
                    }

                    Enter_MessageQueue(is_server, false, "IP setup ok!");

                    DateTime listener_pending_mark;
                    DateTime listener_pending_timeout;

                    listener_pending_mark = DateTime.Now;

                    while (true)
                    {
                        if (Listener.Pending() == false)
                        {
                            //为了避免每次都被tcpListener.AcceptTcpClient()阻塞线程,添加了此判断,
                            //no connection requests have arrived
                            //当没有连接请求时,什么也不做,有了请求再执行到tcpListener.AcceptTcpClient()

                            listener_pending_timeout = DateTime.Now;
                            TimeSpan ts          = listener_pending_timeout - listener_pending_mark;
                            int      second_pass = ts.Minutes * 60 + ts.Seconds;
                            if (second_pass >= 5)
                            {
                                Listener.Stop();
                                Listener = null;

                                Enter_MessageQueue(is_server, false, "Can't find any client");

                                Thread.Sleep(500);

                                break;
                            }
                        }
                        else
                        {
                            remoteClient = Listener.AcceptTcpClient();      //等待连接(阻塞)

                            network_stream_server      = remoteClient.GetStream();
                            br_server_read_from_client = new BinaryReader(network_stream_server);
                            bw_server_write_to_client  = new BinaryWriter(network_stream_server);

                            is_active = true;

                            break;
                        }
                    }

                    if (is_active == true)
                    {
                        break;
                    }
                }

                Enter_MessageQueue(is_server, false, "Tcp is active!");

                while (true) //循坏 接收/发送 数据
                {
                    /********************接收数据部分 Start******************/
                    int rcv_length;

                    PNode <tyNode> pnode = epool_rcv.Get();
                    tyNode         nnode = pnode.obj;

                    try
                    {
                        //接收客户端发送的数据
                        rcv_length = br_server_read_from_client.Read(nnode.buffer, 0, TCP_MAX_DATA_LEN);
                    }
                    catch (Exception ex)
                    {
                        Enter_MessageQueue(is_server, false, "Client lost, Read fail!" + ex.Message);
                        is_active = false;
                        break;
                    }

#if SUPPORT_SHOW_LEN
                    Dbg.WriteLine("Servre rcv:{0}", rcv_length);
#endif

#if SUPPORT_SHOW_DATA
                    Func.DumpBuffer(efifo_rcv.Peek(), rcv_length);
#endif

                    if (rcv_length == 0)
                    {
                        epool_rcv.Put(pnode);
                        Enter_MessageQueue(is_server, false, "Client lost, Read error!");
                        is_active = false;
                        break;
                    }
                    else
                    {
                        nnode.length = rcv_length;
                        efifo_rcv.Input(nnode);
                    }
                    /********************接收数据部分 End********************/
                }
            }

            //TcpMessage(is_server, false, "ThreadEntry_ServerRcv end");
        }
Beispiel #24
0
        public void HandlerRecv(byte[] com_recv_buffer, int com_recv_buff_size, ref string com_text_rcv)
        {
            //Dbg.Write("RECV<<");
            for (int i = 0; i < com_recv_buff_size; i++)
            {
                //Dbg.Write("{0:X} ", com_recv_buffer[i]);

                if (com_recv_buffer[i] == 0x08)                    //退格键
                {
                    if (i == 0)
                    {
#if false //以后再做这个优化
                        form_main.Invoke((EventHandler)(delegate
                        {
                            textbox_show.Text = textbox_show.Text.Substring(0, textbox_show.Text.Length - 1);
                            textbox_show.Select(textbox_show.Text.Length, 0);
                            textbox_show.ScrollToCaret();
                            //textbox_show.Text = textbox_show.Text.Remove(textbox_show.Text.Length - 1, 1); //移除掉","
                        }));
#else
                        com_text_rcv = com_text_rcv.Substring(0, com_text_rcv.Length - 1);
#endif
                    }
                    else
                    {
                        com_recv_buffer[i - 1] = 0x00;
                    }

                    com_recv_buffer[i] = 0x00;
                }

                if (com_recv_buffer[i] == 0x1d)
                {
                    com_recv_buffer[i] = 0x00;
                }
            }
            Dbg.WriteLine(" ");

            byte[] com_recv_buffer_fixed  = new byte[com_recv_buff_size + 1];
            int    com_recv_buff_size_fix = 0;
            //Dbg.Write("recv<<");
            for (int i = 0; i < com_recv_buff_size; i++)             //把非0数据复制到fix数组上
            {
                if (com_recv_buffer[i] != 0x00)
                {
                    //Dbg.Write("{0:X} ", com_recv_buffer[i]);
                    com_recv_buffer_fixed[com_recv_buff_size_fix] = com_recv_buffer[i];
                    com_recv_buff_size_fix++;
                }
            }
            Dbg.WriteLine(" ");

            if (com_recv_buff_size_fix == 0)
            {
                Dbg.WriteLine("LEAVE");
            }
            else
            {
                for (int i = 0; i < com_recv_buff_size_fix; i++)     //从fix数组还原到原本数组上
                {
                    com_recv_buffer[i] = com_recv_buffer_fixed[i];
                }
                com_recv_buff_size = com_recv_buff_size_fix;
            }
        }
Beispiel #25
0
        void ISR_COM_DataRec(object sender, SerialDataReceivedEventArgs e)  //串口接受函数
        {
            rcv_recving        = true;
            last_rcv_data_time = DateTime.Now;

            timer_RcvFlush.Stop();
            timer_RcvFlush.Enabled = false;//timer重新计时
            timer_RcvFlush.Enabled = true;

            if ((COM_Op.com_is_closing == true) || (serialport.IsOpen == false) || (rcv_flushing == true))
            {
                rcv_recving = false;
                return;
            }

            event_recv.Set();                                               //无论有没有资源,都唤醒recv线程去取FIFO

            //如果FIFO已经满了,最后一个current_rnode会一直接一直接,然后突破了buffer的长度
            if (efifo_raw_2_str.is_full == true)
            {
                Dbg.WriteLine("###1.COM:{0} recv fifo is full:{1}, data miss!!!",
                              serialport.IsOpen, efifo_raw_2_str.GetValidNum());

                UpdateMissData();

                rcv_recving = false;
                return;
            }

            if (current_rnode == null)
            {
                PNode <tyRcvNode> pnode = epool_rcv.Get();
                if (pnode == null)
                {
                    Dbg.WriteLine("###COM:{0} recv pool is full:{1}({2}), data miss!!!",
                                  serialport.IsOpen, epool_rcv.nr_got, epool_rcv.nr_ent);

                    UpdateMissData();

                    rcv_recving = false;
                    return;
                }
                else
                {
                    current_rnode = pnode.obj;
                }

                current_rnode        = pnode.obj;
                current_rnode.length = 0;               //把长度清零,避免长度越界
            }

            int com_recv_buff_length = serialport.Read(current_rnode.buffer, current_rnode.length, serialport.ReadBufferSize);

            if (com_recv_buff_length > 0)
            {
                bbbb = DateTime.Now;

                current_rnode.length += com_recv_buff_length;

#if false
                current_rnode.log_len[current_rnode.log_cnt % 128] = com_recv_buff_length;
                current_rnode.log_sz[current_rnode.log_cnt % 128]  = current_rnode.length;
                current_rnode.log_cnt++;
#endif
                //当缓存较多的时候,优先提高平滑性(收得太慢/显示得太慢)
                if ((epool_rcv.nr_got < epool_rcv.nr_ent / 128) &&       //64
                    (epool_show.nr_got < epool_show.nr_ent / 128))
                {
                    handle_data_thresdhold = 0;
                }
                else if ((epool_rcv.nr_got < epool_rcv.nr_ent / 64) &&   //128
                         (epool_show.nr_got < epool_show.nr_ent / 64))
                {
                    handle_data_thresdhold = 1024;
                }
                else if ((epool_rcv.nr_got < epool_rcv.nr_ent / 32) ||  //256
                         (epool_show.nr_got < epool_show.nr_ent / 32))
                {
                    handle_data_thresdhold = tyRcvNode.RCV_CACHE_SIZE;
                }

                if (current_rnode.length >= handle_data_thresdhold)
                {
                    if (efifo_raw_2_str.is_full == true)
                    {
                        Dbg.WriteLine("###2.COM:{0} recv fifo is full:{1}, data miss!!!",
                                      serialport.IsOpen, efifo_raw_2_str.GetValidNum());
                        record.miss_data += (uint)current_rnode.length;

                        rcv_recving = false;
                        return;
                    }

                    efifo_raw_2_str.Input(current_rnode);
                    current_rnode = null;
                }
                else
                {
                    rcv_recving = false;
                    return;
                }

                event_recv.Set();
#if false
                Dbg.Write("RECA[{0}]: in:{1}-{2} out:{3}-{4}", com_recv_buff_length,
                          com_recv_fifo_top, com_recv_fifo_buttom, fp_out_top, fp_out_buttom);

                for (int v = 0; v < com_recv_buff_length; v++)
                {
                    Dbg.Write(" {0:X}", rcv_fifo.buffer[v]);
                }
                Dbg.Write("\r\n");
#endif

                rcv_recving = false;
            }
        }
Beispiel #26
0
        void Key_To_ASCII(Keys KeyCode)
        {
            Keys   key_code;
            UInt32 key_func;

            bool key_shift_en = false;
            bool key_ctrl_en  = false;
            bool key_alt_en   = false;

            key_func = (UInt32)KeyCode >> 16;
            key_code = (Keys)((UInt32)KeyCode & (0x0000FFFFu));

            if ((key_func & 0x0001) != 0)              //从打印中看出来的
            {
                key_shift_en = true;
            }
            if ((key_func & 0x0002) != 0)
            {
                key_ctrl_en = true;
            }
            if ((key_func & 0x0004) != 0)
            {
                key_alt_en = true;
            }

            Dbg.WriteLine("(KEY):{0}|code:{1}|func:{2}|alt:{3}|ctrl:{4}|shft:{5}",
                          KeyCode, (UInt32)key_code, (UInt32)key_func, key_alt_en, key_ctrl_en, key_shift_en);

            if (key_code == Keys.Tab)
            {
                Console_FIFO_Input((Byte)'\t');
            }
            if (key_code == Keys.Space)
            {
                Console_FIFO_Input((Byte)' ');
            }
            if (KeyCode == Keys.Escape)
            {
                Console_FIFO_Input(0x1d);
            }                                                           //0x1d
            if (KeyCode == Keys.Back)
            {
                Console_FIFO_Input((Byte)'\b');
            }                                                            //0x08
            if (KeyCode == Keys.Enter)
            {
                Console_FIFO_Input((Byte)'\r');
            }                           //0x0d

            if (KeyCode == Keys.Left)   //left: 1b 5b 44
            {
                Console_FIFO_Input(0x1b); Console_FIFO_Input(0x5b); Console_FIFO_Input(0x44);
            }
            if (KeyCode == Keys.Right)  //right:1b 5b 43
            {
                Console_FIFO_Input(0x1b); Console_FIFO_Input(0x5b); Console_FIFO_Input(0x43);
            }
            if (KeyCode == Keys.Up)     //up:   1b 5b 41
            {
                //console_dir_func();
                Console_FIFO_Input(0x1b); Console_FIFO_Input(0x5b); Console_FIFO_Input(0x41);
            }
            if (KeyCode == Keys.Down)   //down: 1b 5b 42
            {
                //console_dir_func();
                Console_FIFO_Input(0x1b); Console_FIFO_Input(0x5b); Console_FIFO_Input(0x42);
            }

            if (key_shift_en == true)
            {
                if (key_code == Keys.Q)
                {
                    Console_FIFO_Input((Byte)'Q');
                }
                if (key_code == Keys.W)
                {
                    Console_FIFO_Input((Byte)'W');
                }
                if (key_code == Keys.E)
                {
                    Console_FIFO_Input((Byte)'E');
                }
                if (key_code == Keys.R)
                {
                    Console_FIFO_Input((Byte)'R');
                }
                if (key_code == Keys.T)
                {
                    Console_FIFO_Input((Byte)'T');
                }
                if (key_code == Keys.Y)
                {
                    Console_FIFO_Input((Byte)'Y');
                }
                if (key_code == Keys.U)
                {
                    Console_FIFO_Input((Byte)'I');
                }
                if (key_code == Keys.I)
                {
                    Console_FIFO_Input((Byte)'I');
                }
                if (key_code == Keys.O)
                {
                    Console_FIFO_Input((Byte)'O');
                }
                if (key_code == Keys.P)
                {
                    Console_FIFO_Input((Byte)'P');
                }
                if (key_code == Keys.OemOpenBrackets)
                {
                    Console_FIFO_Input((Byte)'{');
                }
                if (key_code == Keys.OemCloseBrackets)
                {
                    Console_FIFO_Input((Byte)'}');
                }
            }
            else
            {
                if (key_code == Keys.Q)
                {
                    Console_FIFO_Input((Byte)'q');
                }
                if (key_code == Keys.W)
                {
                    Console_FIFO_Input((Byte)'w');
                }
                if (key_code == Keys.E)
                {
                    Console_FIFO_Input((Byte)'e');
                }
                if (key_code == Keys.R)
                {
                    Console_FIFO_Input((Byte)'r');
                }
                if (key_code == Keys.T)
                {
                    Console_FIFO_Input((Byte)'t');
                }
                if (key_code == Keys.Y)
                {
                    Console_FIFO_Input((Byte)'y');
                }
                if (key_code == Keys.U)
                {
                    Console_FIFO_Input((Byte)'u');
                }
                if (key_code == Keys.I)
                {
                    Console_FIFO_Input((Byte)'i');
                }
                if (key_code == Keys.O)
                {
                    Console_FIFO_Input((Byte)'o');
                }
                if (key_code == Keys.P)
                {
                    Console_FIFO_Input((Byte)'p');
                }
                if (key_code == Keys.OemOpenBrackets)
                {
                    Console_FIFO_Input((Byte)'[');
                }
                if (key_code == Keys.OemCloseBrackets)
                {
                    Console_FIFO_Input((Byte)']');
                }
            }

            if (key_shift_en == true)
            {
                if (key_code == Keys.A)
                {
                    Console_FIFO_Input((Byte)'A');
                }
                if (key_code == Keys.S)
                {
                    Console_FIFO_Input((Byte)'S');
                }
                if (key_code == Keys.D)
                {
                    Console_FIFO_Input((Byte)'D');
                }
                if (key_code == Keys.F)
                {
                    Console_FIFO_Input((Byte)'F');
                }
                if (key_code == Keys.G)
                {
                    Console_FIFO_Input((Byte)'G');
                }
                if (key_code == Keys.H)
                {
                    Console_FIFO_Input((Byte)'H');
                }
                if (key_code == Keys.J)
                {
                    Console_FIFO_Input((Byte)'J');
                }
                if (key_code == Keys.K)
                {
                    Console_FIFO_Input((Byte)'K');
                }
                if (key_code == Keys.L)
                {
                    Console_FIFO_Input((Byte)'L');
                }
                if (key_code == Keys.OemSemicolon)
                {
                    Console_FIFO_Input((Byte)':');
                }
                if (key_code == Keys.OemQuotes)
                {
                    Console_FIFO_Input((Byte)'\"');
                }
                if (key_code == Keys.OemPipe)
                {
                    Console_FIFO_Input((Byte)'|');
                }
            }
            else
            {
                if (key_code == Keys.A)
                {
                    Console_FIFO_Input((Byte)'a');
                }
                if (key_code == Keys.S)
                {
                    Console_FIFO_Input((Byte)'s');
                }
                if (key_code == Keys.D)
                {
                    Console_FIFO_Input((Byte)'d');
                }
                if (key_code == Keys.F)
                {
                    Console_FIFO_Input((Byte)'f');
                }
                if (key_code == Keys.G)
                {
                    Console_FIFO_Input((Byte)'g');
                }
                if (key_code == Keys.H)
                {
                    Console_FIFO_Input((Byte)'h');
                }
                if (key_code == Keys.J)
                {
                    Console_FIFO_Input((Byte)'j');
                }
                if (key_code == Keys.K)
                {
                    Console_FIFO_Input((Byte)'k');
                }
                if (key_code == Keys.L)
                {
                    Console_FIFO_Input((Byte)'l');
                }
                if (key_code == Keys.OemSemicolon)
                {
                    Console_FIFO_Input((Byte)';');
                }
                if (key_code == Keys.OemQuotes)
                {
                    Console_FIFO_Input((Byte)'\'');
                }
                if (key_code == Keys.OemPipe)
                {
                    Console_FIFO_Input((Byte)'\\');
                }
            }

            if (key_shift_en == true)
            {
                if (key_code == Keys.Z)
                {
                    Console_FIFO_Input((Byte)'Z');
                }
                if (key_code == Keys.X)
                {
                    Console_FIFO_Input((Byte)'X');
                }
                if (key_code == Keys.C)
                {
                    Console_FIFO_Input((Byte)'C');
                }
                if (key_code == Keys.V)
                {
                    Console_FIFO_Input((Byte)'V');
                }
                if (key_code == Keys.B)
                {
                    Console_FIFO_Input((Byte)'B');
                }
                if (key_code == Keys.N)
                {
                    Console_FIFO_Input((Byte)'N');
                }
                if (key_code == Keys.M)
                {
                    Console_FIFO_Input((Byte)'M');
                }
                if (key_code == Keys.Oemcomma)
                {
                    Console_FIFO_Input((Byte)'<');
                }
                if (key_code == Keys.OemPeriod)
                {
                    Console_FIFO_Input((Byte)'>');
                }
                if (key_code == Keys.OemQuestion)
                {
                    Console_FIFO_Input((Byte)'?');
                }
            }
            else
            {
                if (key_code == Keys.Z)
                {
                    Console_FIFO_Input((Byte)'z');
                }
                if (key_code == Keys.X)
                {
                    Console_FIFO_Input((Byte)'x');
                }
                if (key_code == Keys.C)
                {
                    Console_FIFO_Input((Byte)'c');
                }
                if (key_code == Keys.V)
                {
                    Console_FIFO_Input((Byte)'v');
                }
                if (key_code == Keys.B)
                {
                    Console_FIFO_Input((Byte)'b');
                }
                if (key_code == Keys.N)
                {
                    Console_FIFO_Input((Byte)'n');
                }
                if (key_code == Keys.M)
                {
                    Console_FIFO_Input((Byte)'m');
                }
                if (key_code == Keys.Oemcomma)
                {
                    Console_FIFO_Input((Byte)',');
                }
                if (key_code == Keys.OemPeriod)
                {
                    Console_FIFO_Input((Byte)'.');
                }
                if (key_code == Keys.OemQuestion)
                {
                    Console_FIFO_Input((Byte)'/');
                }
            }

            if (key_shift_en == true)
            {
                if (key_code == Keys.Oemtilde)
                {
                    Console_FIFO_Input((Byte)'~');
                }
                if (key_code == Keys.D1)
                {
                    Console_FIFO_Input((Byte)'!');
                }
                if (key_code == Keys.D2)
                {
                    Console_FIFO_Input((Byte)'@');
                }
                if (key_code == Keys.D3)
                {
                    Console_FIFO_Input((Byte)'#');
                }
                if (key_code == Keys.D4)
                {
                    Console_FIFO_Input((Byte)'$');
                }
                if (key_code == Keys.D5)
                {
                    Console_FIFO_Input((Byte)'%');
                }
                if (key_code == Keys.D6)
                {
                    Console_FIFO_Input((Byte)'^');
                }
                if (key_code == Keys.D7)
                {
                    Console_FIFO_Input((Byte)'&');
                }
                if (key_code == Keys.D8)
                {
                    Console_FIFO_Input((Byte)'*');
                }
                if (key_code == Keys.D9)
                {
                    Console_FIFO_Input((Byte)'(');
                }
                if (key_code == Keys.D0)
                {
                    Console_FIFO_Input((Byte)')');
                }
                if (key_code == Keys.OemMinus)
                {
                    Console_FIFO_Input((Byte)'_');
                }
                if (key_code == Keys.Oemplus)
                {
                    Console_FIFO_Input((Byte)'+');
                }
            }
            else
            {
                if (key_code == Keys.Oemtilde)
                {
                    Console_FIFO_Input((Byte)'`');
                }
                if (key_code == Keys.D1)
                {
                    Console_FIFO_Input((Byte)'1');
                }
                if (key_code == Keys.D2)
                {
                    Console_FIFO_Input((Byte)'2');
                }
                if (key_code == Keys.D3)
                {
                    Console_FIFO_Input((Byte)'3');
                }
                if (key_code == Keys.D4)
                {
                    Console_FIFO_Input((Byte)'4');
                }
                if (key_code == Keys.D5)
                {
                    Console_FIFO_Input((Byte)'5');
                }
                if (key_code == Keys.D6)
                {
                    Console_FIFO_Input((Byte)'6');
                }
                if (key_code == Keys.D7)
                {
                    Console_FIFO_Input((Byte)'7');
                }
                if (key_code == Keys.D8)
                {
                    Console_FIFO_Input((Byte)'8');
                }
                if (key_code == Keys.D9)
                {
                    Console_FIFO_Input((Byte)'9');
                }
                if (key_code == Keys.D0)
                {
                    Console_FIFO_Input((Byte)'0');
                }
                if (key_code == Keys.OemMinus)
                {
                    Console_FIFO_Input((Byte)'-');
                }
                if (key_code == Keys.Oemplus)
                {
                    Console_FIFO_Input((Byte)'=');
                }
            }
        }
Beispiel #27
0
        unsafe public bool Start()
        {
            string cpu0_hex_path = hex0_path;
            string cpu1_hex_path = hex1_path;

            if ((File.Exists(@cpu0_hex_path) == false) || (File.Exists(@cpu1_hex_path) == false))
            {
                MessageBox.Show("HEX not exist!   " + cpu0_hex_path + "   " + cpu1_hex_path + Dbg.GetStack(), "Warning!");
                return(false);
            }

            FileInfo fi = new FileInfo(cpu0_hex_path);

            last_hex_time = fi.LastWriteTime;                               //记录上一次打开HEX的时间
            Dbg.WriteLine("First. Create:" + fi.CreationTime.ToString() + "  Write:" + fi.LastWriteTime + "  Access:" + fi.LastAccessTime);

            char[] pwd_hex0 = hex0_path.ToCharArray();
            char[] pwd_hex1 = hex1_path.ToCharArray();

            Dbg.WriteLine("pwd_hex0:{0}", hex0_path);
            Dbg.WriteLine("pwd_hex1:{0}", hex1_path);

            //将内嵌的资源释放到临时目录下
            if (File.Exists(fast_printf_dll_address) == false)
            {
                FileStream str = new FileStream(fast_printf_dll_address, FileMode.OpenOrCreate);
                str.Write(Properties.Resources.FastPrintf, 0, Properties.Resources.FastPrintf.Length);
                str.Close();
            }

            mi_fa = new memory_desc();
            mi_fb = new memory_desc();

            mi_fa.pBuff = (byte *)Marshal.AllocHGlobal(MEMORY_MAX_BANK * MEMORY_BANK_SIZE);
            mi_fb.pBuff = (byte *)Marshal.AllocHGlobal(MEMORY_MAX_BANK * MEMORY_BANK_SIZE);

            mi_fa.bank = (memory_bank *)Marshal.AllocHGlobal(sizeof(memory_bank) * MEMORY_MAX_BANK);
            mi_fb.bank = (memory_bank *)Marshal.AllocHGlobal(sizeof(memory_bank) * MEMORY_MAX_BANK);

            hex2bin_mount(ref mi_fa);
            hex2bin_mount(ref mi_fb);

            if (hex2bin_read_hex(pwd_hex0, ref mi_fa) == 0)
            {
                MessageBox.Show("###open hex0 fail!", "Error!");
                Close();
                return(false);
            }
            if (hex2bin_read_hex(pwd_hex1, ref mi_fb) == 0)
            {
                MessageBox.Show("###open hex1 fail!", "Error!");
                return(false);
            }

            input_data  = (byte *)Marshal.AllocHGlobal(1024 * 1024);
            output_data = (byte *)Marshal.AllocHGlobal(1024 * 1024);

            is_active = true;

            return(true);
        }