Esempio n. 1
0
        public PNode <T> Get()
        {
            PNode <T> res = null;

            lock (epool_lock)
            {
                int i;
                for (i = 0; i < nr_ent; i++)
                {
                    PNode <T> p = node_buffer[i];

                    if (p.taken == false)
                    {
                        p.taken = true;
                        nr_got++;
                        DbgAddLog(1, nr_got);
                        Dbg.Assert(nr_got <= nr_ent, "###pool get error");
                        res = p;
                        break;
                    }
                }
            }

            return(res);
        }
Esempio n. 2
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);
            }
        }
Esempio n. 3
0
File: Com.cs Progetto: KugaChan/KCOM
        void ThreadEntry_ComRecv()
        {
            while (true)
            {
                check_thread_ComRecv++;
                step_thread_ComRecv = 0;
                if (efifo_raw_2_str.GetValidNum() == 0)
                {
                    step_thread_ComRecv = 1;
                    event_recv.WaitOne(1000);       //FIFO已经空了,则在这里一直等待,直到有事件过来,可以有效降低CPU的占用率
                }
                else
                {
                    step_thread_ComRecv = 2;
                    tyRcvNode output_rnode = efifo_raw_2_str.Output();

                    if (output_rnode.length > 0)
                    {
                        step_thread_ComRecv = 3;
#if false  //false, true
                        //打印发送数据
                        Dbg.Write("com Data[{0}]:", raw_data_buffer.Length);
                        for (int i = 0; i < raw_data_buffer.Length; i++)
                        {
                            Dbg.Write(" {0:X}", raw_data_buffer[i]);
                        }
                        Dbg.Write("\r\n");
#endif
                        if (fm.fp.is_active == true)
                        {
                            step_thread_ComRecv = 4;
                            int    recv_len;
                            byte[] recv_data;
                            recv_len = fm.fp.DataConvert(output_rnode.buffer, output_rnode.length, out recv_data);
                            if (recv_len > 0)
                            {
                                DataHandle(recv_data, recv_len, true);
                            }
                        }
                        else
                        {
                            step_thread_ComRecv = 5;
                            DataHandle(output_rnode.buffer, output_rnode.length, true);
                        }
                    }
                    else
                    {
                        step_thread_ComRecv = 6;
                        Dbg.Assert(false, "###why output data is zero length?");
                    }

                    step_thread_ComRecv = 7;
                    epool_rcv.Put(output_rnode.pnode);
                }
                step_thread_ComRecv = 8;
            }
        }
Esempio n. 4
0
        static public bool button_COMOpen_Click(SerialPort sp)
        {
            MyTimer MyTmr = null;

            for (int i = 0; i < list_timer_scan_com.Count; i++)
            {
                if (list_timer_scan_com[i].serialport == sp)
                {
                    MyTmr = list_timer_scan_com[i];
                    break;
                }
            }

            Dbg.Assert(MyTmr != null, "###Why can not find SP!?");

            bool res = false;

            if ((MyTmr.button_COMOpen.ForeColor == Color.Red) && (sp.IsOpen == false))         //打开串口
            {
                if (Open(sp) == true)
                {
                    MyTmr.Enabled = true;
                    res           = true;
                }
            }
            else if ((MyTmr.button_COMOpen.ForeColor == Color.Green) && (sp.IsOpen == true))   //关闭串口
            {
                MyTmr.Enabled = false;

                Close(sp);

                //重建一次com number 列表,因为可能COM口有变化,同样的select下次打不开了
                int temp_select_index = MyTmr.comboBox_COMNumber.SelectedIndex;
                Ruild_ComNumberList(MyTmr.comboBox_COMNumber);
                if (MyTmr.comboBox_COMNumber.Items.Count > temp_select_index)
                {
                    MyTmr.comboBox_COMNumber.SelectedIndex = temp_select_index;
                }
            }
            //关闭窗口的时候,如果串口已经掉了,则会进来这里,触发comboBox_COMNumber_SelectedIndexChanged,不走关闭串口的路径,列表会重新建立
            else if ((MyTmr.button_COMOpen.ForeColor == Color.Green) && (sp.IsOpen == false))
            {
                MyTmr.comboBox_COMNumber.SelectedIndex = -1;
            }
            //拔掉COM之后,is open会变成false 的
            else
            {
                Dbg.Assert(false, "###TODO: What is this statue?!");
            }

            return(res);
        }
Esempio n. 5
0
        public void Put(PNode <T> free_node)
        {
            lock (epool_lock)
            {
                PNode <T> p = node_buffer[free_node.index];
                Dbg.Assert(p.taken == true, "###pool put error1");
                p.taken = false;
                nr_got--;

                DbgAddLog(-1, nr_got);
                if (nr_got < 0)
                {
                    DbgDumpLog();
                }
                Dbg.Assert(nr_got >= 0, "###pool put error2");
            }
        }
Esempio n. 6
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;
        }
Esempio n. 7
0
        //新增一个元素到队伍尾部
        public void PushTail(eNode <T> new_node)
        {
            lock (elink_lock)    //lock里面return也是会解锁的
            {
                nr_entry++;

                if (next_node == null)
                {
                    next_node = new_node;
                    last_node = new_node;
                    return;
                }

                if (last_node != null)
                {
                    last_node.next = new_node;
                    last_node      = new_node;

                    return;
                }
            }

            Dbg.Assert(false, "###eLink is corrupt!");
        }