private void btnSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (XtraMessageBox.Show("确定保存该信息?", "操作确认", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
            {
                bLOODCLEANUPBindingSource.EndEdit();
                bLOODCLEANUPBindingSource.CurrencyManager.EndCurrentEdit();
                try
                {
                    blookCaeanup.LOG_TIME = DateTime.Now;
                    object oID = db.Insert(blookCaeanup);

                    blookCaeanup = new  BLOODCLEANUP();
                    blookCaeanup.REG_ID = _regID;
                    blookCaeanup.BASE_INFO_ID = _baseID;
                    blookCaeanup.OPERATOR = ClsFrmMng.WorkerID;
                    bLOODCLEANUPBindingSource.DataSource = blookCaeanup;

                    if (NewRegistEvt != null)
                        NewRegistEvt();

                    MACHINE_INFO mi = db.SingleOrDefault<MACHINE_INFO>("where FLOOR_ID = @0 and AREA_ID = @1 and  BED_NO= @2",
                        new object[]{ms.FLOOR_ID, ms.AREA_ID, ms.BED_NO});
                    if (mi == null || string.IsNullOrWhiteSpace(mi.COMMIP))
                        throw new Exception("该床位可能没有配置透析机信息, 或该床位所用透析机串口号未配置, 请确认.");
                    else
                    {
                        BLOODCLEANUP_TEMP bt = new BLOODCLEANUP_TEMP();
                        bt.BLOOD_CLEANUP_ID = Convert.ToDecimal(oID);
                        bt.ANA_DATE = DateTime.Now;
                        bt.SERIAL_PORT_NUM = mi.COMMIP;
                        db.Insert(bt);
                    }

                }
                catch (Exception err)
                {
                    XtraMessageBox.Show(err.Message, "错误提示", MessageBoxButtons.OK);
                }
            }
        }
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            Int64 iTicks = 1;
            byte[] by = new byte[] { 0x4b, 0x0d, 0x0a };

            // 打开所有串口
            foreach (KeyValuePair<string, SerialPortManager> item in dicSerialPort)
            {
                try
                {
                    item.Value.StartListening(item.Key, 9600, System.IO.Ports.Parity.None, 8, System.IO.Ports.StopBits.One);
                }
                catch (Exception err)
                {
                    tbData.SafeCall(delegate()
                    {
                        tbData.AppendText("初始化串口" + item.Key + "发生异常: " + err.Message + Environment.NewLine);
                    });
                }
            }

            while (isRun)
            {
                foreach (KeyValuePair<string, SerialPortManager> item in dicSerialPort)
                {
                    try
                    {
                        item.Value.SendMsg(by);
                    }
                    catch (Exception err)
                    {
                        tbData.SafeCall(delegate()
                        {
                            tbData.AppendText("向串口" + item.Key + "发送数据失败: " + err.Message);
                        });
                    }
                }

                tbData.SafeCall(delegate()
                {
                    tbData.Clear();
                });

                foreach (KeyValuePair<string, SerialPortManager> item in dicSerialPort)
                {
                    try
                    {
                        string sData = item.Value.ReadData();

                        tbData.SafeCall(delegate()
                        {
                            tbData.AppendText(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + item.Key + ": " + sData + Environment.NewLine);
                        });

                        if (sData.Length > 20)
                        {
                            try
                            {
                                string sDataTmp = sData.Substring(0, sData.Length - 4);

                                // 写入数据库
                                PetaPoco.Database db = new Database("XE");

                                DEVICECOMMUNICATION_LOG log = new DEVICECOMMUNICATION_LOG();
                                log.MSG = sData.Length > 3999 ? sData.Substring(0, 3999) : sData;
                                log.REMOTE_IP = toolStripTextBox1.Text + item.Key;
                                //log.REMOTE_PORT = remotePort;
                                log.RECEIVE_TIME = DateTime.Now;

                                db.Insert(log);

                                // 写入到台湾人要的表
                                var log1 = GetLog1Entity(sDataTmp, item.Key);
                                db.Insert(log1);

                                if (iTicks % 60 == 0)
                                {
                                    BLOODCLEANUP_TEMP bt = new BLOODCLEANUP_TEMP();
                                    bt = db.SingleOrDefault<BLOODCLEANUP_TEMP>("where to_char( ANA_DATE, 'yyyy-mm-dd') = @0  and SERIAL_PORT_NUM = @1",
                                        new object[] { DateTime.Now.ToString("yyyy-MM-dd"), toolStripTextBox1.Text + item.Key });

                                    if (bt != null)
                                    {
                                        string sTmp = sDataTmp.Substring(log.MSG.LastIndexOf('F') + 1, 5);
                                        string sVp = sDataTmp.Substring(log.MSG.LastIndexOf('H') + 1, 5);
                                        string sBf = sDataTmp.Substring(log.MSG.LastIndexOf('D') + 1, 5);
                                        string sMaxBp = sDataTmp.Substring(log.MSG.LastIndexOf('N') + 1, 5);
                                        string sMinBp = sDataTmp.Substring(log.MSG.LastIndexOf('O') + 1, 5);
                                        string sPulse = sDataTmp.Substring(log.MSG.LastIndexOf('P') + 1, 5);
                                        string sTotalUFAmount = sDataTmp.Substring(log.MSG.LastIndexOf('B') + 1, 5);
                                        string sDC = sDataTmp.Substring(log.MSG.LastIndexOf('G') + 1, 5);
                                        string sTMP = sDataTmp.Substring(log.MSG.LastIndexOf('J') + 1, 5);

                                        BLOODCLEANUP_PROCESS proc = new BLOODCLEANUP_PROCESS();
                                        proc.ANA_TIME = log.RECEIVE_TIME;
                                        proc.TEMP = decimal.Parse(sTmp);
                                        proc.VENOUS_PRESSURE = decimal.Parse(sVp);
                                        proc.BLOOD_FLOW = decimal.Parse(sBf);
                                        proc.BP = decimal.Parse(sMaxBp).ToString() + "~" + decimal.Parse(sMinBp);
                                        proc.P = decimal.Parse(sPulse);
                                        proc.ULTRAFILTRATION = decimal.Parse(sTotalUFAmount);
                                        proc.CONDUCTIVITY = sDC;
                                        proc.ARTERIAL_PRESSURE = decimal.Parse(sTMP);

                                        proc.LOG_TIME = DateTime.Now;
                                        proc.BLOODCLEANUP_ID = bt.BLOOD_CLEANUP_ID;
                                        db.Insert(proc);
                                        //proc.OPERATOR = ClsFrmMng.WorkerID;
                                    }
                                }
                            }
                            catch (Exception err)
                            {
                                tbData.SafeCall(delegate()
                                {
                                    tbData.AppendText("写数据失败: " + err.Message + Environment.NewLine);
                                });
                            }

                        }
                    }
                    catch (Exception err)
                    {
                        tbData.SafeCall(delegate()
                        {
                            tbData.AppendText("读取串口" + item.Key + "数据失败: " + err.Message + Environment.NewLine);
                        });
                    }

                }
                Thread.Sleep(3000);
                iTicks++;
            }
        }