Exemplo n.º 1
0
        //成功接收到数据
        public static void ReadCallback(IAsyncResult ar)
        {
            try
            {
                String content = String.Empty;

                StateObject state   = (StateObject)ar.AsyncState;
                Socket      handler = state.workSocket;

                int bytesRead = handler.EndReceive(ar);
                if (bytesRead > 0)
                {
                    string data = Encoding.Default.GetString(state.buffer, 0, bytesRead);
                    state.sb.Append(data);
                    content = state.sb.ToString();
                    //  Send(handler, "recive success");

                    DataProcessCommon dataProcessCommon = new DataProcessCommon();
                    //处理接收的数据
                    dataProcessCommon.ProcessData(data, handler.LocalEndPoint.ToString().Split(':')[1]);

                    //继续监听
                    handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state);
                }
            }
            catch (Exception ex)
            {
                FileOperation.WriteAppenFile("3" + ex.Message);
            }
        }
Exemplo n.º 2
0
        //将数据保持到数据库
        public bool SaveData(List <Model> models, int xmno)
        {
            DataProcessCommon common = new DataProcessCommon();
            OdbcConnection    conn   = DbContext.GetConn(xmno);

            conn.Open();
            OdbcTransaction trans = conn.BeginTransaction();

            try
            {
                foreach (var item in models)
                {
                    string sql = string.Format("insert into data(sno, cycle, time, tdno, res, pointName, dy, port, number, dataType) values('{0}', '{1}', '{2}', '{3}', {4}, '{5}', {6}, {7}, {8}, '{9}')",
                                               item.sno, item.cycle, item.time, item.tdno, item.res, item.pointName, item.dy, item.port, item.number, item.dataType);
                    OdbcCommand comm = new OdbcCommand(sql, conn);
                    comm.Transaction = trans;
                    if (comm.ExecuteNonQuery() <= 0)
                    {
                        trans.Rollback();
                        return(false);
                    }
                }

                trans.Commit();
                return(true);
            }
            catch (Exception ex)
            {
                string info = string.Format("数据保存的数据库失败," + ex.Message);
                FileOperation.WriteAppenFile(info);
                FileOperation.WriteAppenFile(info);
                trans.Rollback();
                return(false);
            }
            finally
            {
                trans.Dispose();
                conn.Close();
            }
        }
Exemplo n.º 3
0
        //根据端口号确定传感器以及数据类型
        public static IDataProcess GetDataProcess(int port, out string dataType, out int xmno)
        {
            try
            {
                IDataProcess      dataProcess       = null;
                DataProcessCommon dataProcessCommon = new DataProcessCommon();

                //根据端口号获取项目编号
                //string xmno = dataProcessCommon.GetXmno(port);
                //根据端口号判断数据类型
                dataType = null;
                xmno     = 0;
                dataProcessCommon.GetDataType(port, out dataType, out xmno);
                Console.WriteLine("收到来自端口{0}的{1}数据", port.ToString(), dataType);

                string info = string.Format("收到来自端口{0}的{1}数据", port.ToString(), dataType);
                FileOperation.WriteAppenFile(info);

                switch (dataType)
                {
                //震弦式
                case "应变":
                case "土压":
                case "锚索":
                case "钢筋":
                case "轴力":
                case "水压":
                    dataProcess = new DataProcessZX(); break;

                    //485式

                    //    dataProcess = new DataProcess485(); break;
                }
                return(dataProcess);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }