Exemplo n.º 1
0
        void comArduino_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            System.Threading.Thread.Sleep(50);//如果不sleep的话得到的串会分开
            int n = comArduino.BytesToRead;//先记录下来,避免某种原因,人为的原因,操作几次之间时间长,缓存不一致
            byte[] buf = new byte[n];//声明一个临时数组存储当前来的串口数据

            comArduino.Read(buf, 0, n);//读取缓冲数据
            builder.Clear();//清除字符串构造器的内容
            //因为要访问ui资源,所以需要使用invoke方式同步ui。
            this.Invoke((EventHandler)(delegate
            {

                builder.Append(Encoding.ASCII.GetString(buf));
                char[] trimchars = { '\r', '\n' };
                string originaldata = builder.ToString().Trim(trimchars);
                string[] dataarray = originaldata.Split(',');

                //这里需要读取相关信息,然后显示在dataadd中
                //同时将数据插入相关数据库

                DataOperate dataoperator = new DataOperate();

                IList<ParamInfo> paraminfo = new List<ParamInfo>();

                paraminfo = dataoperator.GetParamByParamName(dataarray[1]);

                int deviceid = dataoperator.GetDeviceIDbyDeviceDesc(dataarray[0]);

                OriginalDataInfo datainfo = new OriginalDataInfo();

                datainfo.DeviceID = deviceid;
                datainfo.InsertTime = DateTime.Now;
                datainfo.ParamID = paraminfo[0].ParamID;
                datainfo.ParamValue = float.Parse( dataarray[2]);

                dataoperator.InsertData(datainfo);

                string dataadd = "设备信息:" + dataarray[0] + "  " + "参数描述:" + paraminfo[0].ParamDesc + "  " + "时间:" + datainfo.InsertTime + "  " + "参数值:" + dataarray[2];
                
                




                if (lbxData.Items.Count > 1000)
                {
                    lbxData.Items.RemoveAt(0);
                }
                lbxData.Items.Add(dataadd);

            }));
        }
Exemplo n.º 2
0
        public void InsertData(OriginalDataInfo datainfo)
        {

            StringBuilder moniterPlaceSQL = new StringBuilder();
            //获得每个命令的参数数组
            SqlParameter[] moniterPlaceParms = GetMoniterPlaceParameters();

            SqlCommand cmd = new SqlCommand();

            //设置的参数
            
            moniterPlaceParms[0].Value = datainfo.DeviceID;
            moniterPlaceParms[1].Value = datainfo.InsertTime;
            moniterPlaceParms[2].Value = datainfo.ParamID;
            moniterPlaceParms[3].Value = datainfo.ParamValue;
           

            foreach (SqlParameter parm in moniterPlaceParms)
                cmd.Parameters.Add(parm);
            //创建数据库连接
            using (SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringOrderDistributedTransaction))
            {
                //插入数据
                moniterPlaceSQL.Append(SQL_INSERT_ORIGINALDATA);
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = moniterPlaceSQL.ToString();

                //读取输出的查询
                using (SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    rdr.Read();
                }
                //清除参数
                cmd.Parameters.Clear();
            }




        }