Exemple #1
0
        /// <summary>
        /// 添加客户端连接    (注册客户端)
        /// </summary>
        /// <param name="tel">电话号码</param>
        /// <param name="socket">socket实例</param>
        /// <param name="ip">客户端DTU的IP</param>
        /// <param name="port">客户端DTU端口</param>
        public static void AddClient(string tel, Socket socket)
        {
            LogMg.AddDebug(string.Format("进入AddClient方法  tel:{0}", tel));
            Clazz.DTUClientInfo clientInfo = Clients.SingleOrDefault(c => c.TelOrGprsId == tel);
            XML_Station         station    = SysConfig.DTU_StationConfig.Stations.SingleOrDefault(c => c.Tel == tel);

            lock (Clients)
            {
                if (clientInfo == null) //如果客户端不存在,则添加,如果存在,则修改。
                {
                    clientInfo               = new Clazz.DTUClientInfo();
                    clientInfo.TelOrGprsId   = tel;          //手机号码
                    clientInfo.socket        = socket;       //客户端连接实例
                    clientInfo.RegisterTime  = DateTime.Now; //注册时间
                    clientInfo.LastVisitTime = DateTime.Now; //最后一次访问时间
                    if (station != null)
                    {
                        clientInfo.StationId = station.StationId;  //站点id
                        clientInfo.Protocol  = station.Protocol;
                    }
                    Clients.Add(clientInfo);
                }
                else
                {
                    clientInfo.socket        = socket;
                    clientInfo.LastVisitTime = DateTime.Now;    //最后一次访问时间
                }
                if (ClientChangeHander != null)
                {
                    ClientChangeHander();
                }
            }
            LogMg.AddDebug(string.Format("退出AddClient方法  tel:{0}", tel));
        }
        /// <summary>
        /// 处理考勤数据
        /// </summary>
        /// <param name="bytes"></param>
        private void handlerAttMsg(byte[] bytes)
        {
            try
            {
                LogMg.AddDebug("udp协议接收的考勤数据=============     " + ToHexString(bytes));
                int      stationNo = 0;
                int      pid       = 0;
                int      state     = 0;
                DateTime date      = DateTime.Now;
                parseDataAtt(bytes, ref stationNo, ref pid, ref state, ref date);
                //找出编号对应的站点
                SWS_DB.guangdai_station_link link = V88StationLink.SingleOrDefault(c => Int32.Parse(c.wsid) == stationNo);
                if (link == null)
                {
                    return;
                }

                SWSDataContext sws = new SWSDataContext(ConnectStringHelper.GetConnection(SysConfig.userProfile.DbAddress, link.db_name, SysConfig.userProfile.DbUserName, SysConfig.userProfile.DbPassword));
                updateStationLine(sws, (int)link.station_id);
                users user = findUserByPid(sws, pid);
                if (user == null)
                {
                    return;
                }
                country_attendance att = sws.country_attendance.Where(c => c.RFID == pid.ToString() && c.station_id == link.station_id && c.edate != null && DateTime.Now.AddHours(-1).CompareTo(c.edate) < 0).OrderByDescending(c => c.id).FirstOrDefault();
                if (att == null)
                {
                    att = new country_attendance();
                    if (user == null)
                    {
                        att.userid = 0;
                    }
                    else
                    {
                        att.userid = user.userid;
                    }
                    att.station_id = link.station_id;
                    att.sdate      = date;
                    att.edate      = date;
                    att.type       = "01";
                    att.remark     = "V88设备的考勤数据";
                    att.RFID       = pid.ToString();
                    sws.country_attendance.InsertOnSubmit(att);
                    sws.SubmitChanges();
                }
                else
                {
                    att.edate = date;
                    sws.SubmitChanges();
                }
            }
            catch (Exception ex)
            {
                LogMg.AddError(ex);
            }
        }
Exemple #3
0
        /// <summary>
        /// 拆包
        /// </summary>
        /// <param name="socket"></param>
        /// <param name="pack"></param>
        /// <param name="packLen"></param>
        /// <param name="content"></param>
        /// <param name="conLen"></param>
        /// <param name="type"></param>
        /// <param name="tel"></param>
        /// <returns></returns>
        public static bool UnPack(byte[] pack, int packLen, ref byte[] content, ref int conLen, ref byte type, ref string tel)
        {
            try
            {
                int i;
                for (i = 0; i < packLen; i++)
                {
                    if (pack[i] == HD_DTU.HEAD)
                    {
                        break;
                    }
                }
                type = pack[i + 1];                   //类型
                if (type == HD_DTU.DTU_REGISTER_DATA) //注册数据拆包和
                {
                    //解析接收到的数据
                    int high   = pack[i + 2];      //高8位
                    int low    = pack[i + 3];      //低8位
                    int length = high * 256 + low; //从手机号码到最后的字节数

                    byte[] b_tel = new byte[11];
                    Buffer.BlockCopy(pack, i + 4, b_tel, 0, 11);
                    tel = Encoding.ASCII.GetString(b_tel);
                }
                else if (type == HD_DTU.DTU_User_Data)     //用户数据拆包
                {
                    //解析接收到的数据
                    int high   = pack[i + 2];      //高8位
                    int low    = pack[i + 3];      //低8位
                    int length = high * 256 + low; //从手机号码到最后的字节数

                    byte[] b_tel = new byte[11];
                    Buffer.BlockCopy(pack, i + 4, b_tel, 0, 11);
                    tel = Encoding.ASCII.GetString(b_tel);

                    conLen = length - 16;
                    Buffer.BlockCopy(pack, i + 4 + 11, content, 0, conLen);
                }
                else
                {
                    //日志
                    string str = "";
                    for (int j = 0; j < packLen; j++)
                    {
                        str += pack[j] + " ";
                    }
                    LogMg.AddDebug("收到宏电DTU未知类型的数据:" + str);
                }
            }
            catch (Exception ex)
            {
                LogMg.AddError(ex);
                return(false);
            }
            return(true);
        }
        /// <summary>
        /// 把命令写到日志
        /// </summary>
        /// <param name="arr"></param>
        /// <param name="len"></param>
        private static void AddToLog(byte[] arr, int len)
        {
            string str = "";

            for (int i = 0; i < len; i++)
            {
                str += " " + arr[i];
            }
            LogMg.AddDebug(str);
        }
        /// <summary>
        /// 初始化socket
        /// </summary>
        public static void Init()
        {
            ServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            LogMg.AddDebug("初始化socket成功");

            ///“处理第一次注册并等待注册”的线程池的初始化
            PoolD      = new TaskPool();
            PoolD.Name = "D";
            PoolD.Setminthread(SysConfig.userProfile.NetServerThreadPool_Min);
            PoolD.Setmaxthread(SysConfig.userProfile.NetServerThreadPool_Max);
        }
Exemple #6
0
 /// <summary>
 /// 拍照方法
 /// </summary>
 /// <param name="db">数据库对象</param>
 /// <param name="stationid">站点id</param>
 /// <param name="client">客户端</param>
 private static void takePhoto(SWSDataContext db, int stationid, Clazz.DTUClientInfo client)
 {
     ///根据站点id去指定的数据库中找拍照指令,如果存在拍照的指令,则调用【_485_JBT_Camera】类的拍照方法。
     try
     {
         List <control_command> jbt_commands = db.control_command.Where(c => c.gong_kuang_id == 0 && c.state == 0 && c.tp == "3" && c.station_id == stationid).OrderBy(c => c.add_datetime).ToList();
         foreach (control_command command in jbt_commands)
         {
             if (command.COUNT == null)
             {
                 command.COUNT = 0;
             }
             try
             {
                 bool flag = false;
                 command.state = 1;               //标志为已发送
                 byte[] imageBytes = new byte[1]; //takePhoto方法内部会再次分配图像数组大小
                 while (flag == false && command.COUNT < SysConfig.userProfile.ExecuteFailureCount)
                 {
                     flag = _485_JBT_Camera.TakePhoto(client, 5000, ref imageBytes);
                     command.COUNT++;
                     LogMg.AddDebug("拍照失败" + (int)command.COUNT + "次");
                 }
                 if (flag == true)                           //如果拍照成功
                 {
                     SavePicture(db, stationid, imageBytes); //保存照片到数据库
                     command.execute_result  = 1;            //标记为执行成功
                     command.execute_comment = "拍照成功";
                 }
                 else
                 {
                     command.execute_result  = 0;  //标记为执行失败
                     command.execute_comment = "拍照失败";
                 }
             }
             catch (Exception ex)
             {
                 LogMg.AddError(ex);
                 command.execute_result  = 0;
                 command.execute_comment = "拍照失败";
             }
             finally
             {
                 command.complete_datetime = DateTime.Now;
                 db.SubmitChanges();
             }
         }
     }
     catch (Exception ex)
     {
         LogMg.AddError(ex);
     }
 }
Exemple #7
0
 /// <summary>
 /// 删除客户端注册信息
 /// </summary>
 /// <param name="clientInfo"></param>
 public static void DeleteAllClient()
 {
     lock (Clients)
     {
         Clients.RemoveAll(c => true);  //删除所有客户端连接
         LogMg.AddDebug("删除所有客户端连接");
         //   MessageQueue.Enqueue_RegAndLogout("所有的客户端连接都已断开");
         if (ClientChangeHander != null)
         {
             ClientChangeHander();
         }
     }
 }
Exemple #8
0
 /// <summary>
 /// 删除客户端注册信息
 /// </summary>
 /// <param name="clientInfo"></param>
 public static void DeleteClient(Clazz.DTUClientInfo clientInfo)
 {
     lock (Clients)
     {
         Clients.Remove(clientInfo);
         LogMg.AddDebug("删除客户端注册信息  TEL:" + clientInfo.TelOrGprsId);
         // MessageQueue.Enqueue_RegAndLogout(string.Format("【{0}】: {1}退出系统", DateTime.Now.ToString(), clientInfo.Name));
         if (ClientChangeHander != null)
         {
             ClientChangeHander();
         }
     }
 }
Exemple #9
0
 public static void HandlerData(Socket clientSocket, byte[] content, int conLen, byte type, string tel)
 {
     if (type == HD_DTU.DTU_REGISTER_DATA)
     {
         if (Response_Reigster(clientSocket, tel))
         {
             DTU_ClientManager.AddClient(tel, clientSocket);
         }
         else
         {
             LogMg.AddDebug("tel=" + tel + "  响应客户端注册数据失败");
         }
     }
 }
Exemple #10
0
        /// <summary>
        /// 拆包
        /// </summary>
        /// <param name="str"></param>
        /// <param name="strcon"></param>
        /// <param name="type"></param>
        /// <param name="strlen"></param>
        /// <param name="conlen"></param>
        /// <returns></returns>
        public static bool UnPack(byte[] pack, int packLen, ref byte[] content, ref int conlen, ref byte type, ref string tel)
        {
            try
            {
                //日志
                string str = "";
                for (int j = 0; j < packLen; j++)
                {
                    str += pack[j] + " ";
                }
                LogMg.AddDebug(str);


                int i;
                for (i = 0; i < pack.Length - 1; i++)
                {
                    if (pack[i] == ZG_DTU.HEAD)
                    {
                        break;
                    }
                }
                type = pack[i + 1];                //类型
                if (type == ZG_DTU.USER_DATA)      //用户数据拆包
                {
                    int low    = pack[i + 2];      //低8位
                    int high   = pack[i + 3];      //高8位
                    int length = high * 256 + low; //从手机号码到最后的字节数
                    conlen = length;               //内容长度
                    Buffer.BlockCopy(pack, i + 4, content, 0, conlen);
                }
                else if (type == ZG_DTU.REGISTER_DATA) //注册数据拆包
                {
                    int    low     = pack[i + 2];      //低8位
                    int    high    = pack[i + 3];      //高8位
                    int    length  = high * 256 + low; //从手机号码到最后的字节数
                    byte[] byteTel = new byte[11];     //手机号码字节
                    Buffer.BlockCopy(pack, i + 4, byteTel, 0, 11);
                    tel    = Encoding.ASCII.GetString(byteTel);
                    conlen = length - 11;  //内容长度
                    Buffer.BlockCopy(pack, i + 4 + 11, content, 0, conlen);
                }
            }
            catch (Exception ex)
            {
                LogMg.AddError(ex);
                return(false);
            }
            return(true);
        }
Exemple #11
0
 /// <summary>
 /// 解析注册的数据是DTU协议还是DDP协议
 /// </summary>
 /// <param name="buffer"></param>
 /// <returns></returns>
 public static string CheckProtocolByRegisterData(byte[] buffer, int len)
 {
     if (ZG_DTU.CheckProtocolByRegisterData(buffer, len) == true)
     {
         return(_ZG_DTU);
     }
     if (HD_DTU.CheckProtocolByRegisterData(buffer, len) == true)
     {
         return(_HD_DTU);
     }
     if (JBT_DTU.CheckProtocolByRegisterData(buffer, len) == true)
     {
         return(_JBT_DTU);
     }
     //如果不符合上面的协议标准  则返回空字符串
     LogMg.AddDebug("检验注册数据格式不正确 ,即不符合佐格DTU协议,也不符合宏电DTU协议,也不符号金博通DTU协议");
     return("");
 }
Exemple #12
0
        /// <summary>
        /// 保存数据到数据库
        /// </summary>
        /// <param name="testid"></param>
        /// <param name="value"></param>
        private static void SaveToDatabase(SWSDataContext db, int testid, double value)
        {
            LogMg.AddDebug(string.Format("testid={0}    value={1}", testid, value));
            //  SWSDataContext db = new SWSDataContext(Util.ServerSocketHelper.GetConnection(dbname));
            test test = db.test.SingleOrDefault(c => c.testid == testid);   //查询出检测点

            if (test != null)
            {
                if (test.means.Trim() == "屏幕取词" || test.means.Trim() == "自动获取")
                {
                    realrec realrec = new realrec();
                    realrec.testid   = testid;
                    realrec.value    = (decimal)value;
                    realrec.testtime = DateTime.Now;
                    realrec.remark   = "from 201 Server AutoCollectionThread";
                    db.realrec.InsertOnSubmit(realrec);
                    db.SubmitChanges();     //提交
                }
            }
        }
Exemple #13
0
        private void dbToXML()
        {
            DateTime start = DateTime.Now;

            LogMg.AddDebug("准备开始从数据库生成站点和检测点配置");
            //if (SysConfig.DTU_StationConfig.GenerateFromDbToXML() == true)
            if (SysConfig.DTU_StationConfig.GenerateFromDbToXML() == true && SysConfig.GD_Config.GenerateConfig() == true)
            {
                LogMg.AddDebug("从数据库生成站点和检测点配置到XML成功");
                //MessageBox.Show("");
                SysConfig.ReadConfig();
                DTU_ClientManager.Update();
                BeginInvoke(new Action <String>(showMessageBox), "已成功从数据库生成配置");
            }
            else
            {
                LogMg.AddDebug("从数据库生成站点和检测点配置失败");
                BeginInvoke(new Action <String>(showMessageBox), "从数据库生成站点和检测点配置失败,请查看日志文件");
            }
            DateTime end = DateTime.Now;

            LogMg.AddInfo("生成xml并读取配置到内存耗时:" + DateUtil.datediff(start, end));
        }
Exemple #14
0
        /// <summary>
        /// 客户端注册
        /// </summary>
        /// <param name="socket"></param>
        /// <param name="pack"></param>
        /// <param name="length"></param>
        public static void RegisterDataHandler(Socket socket, string tel)
        {
            LogMg.AddDebug(string.Format("进入客户端注册方法RegisterDataHandler    Tel:{0}", tel));
            try
            {
                //  string stationName = DTU_ClientManager.GetStationNameByTel(tel);
                //  if (string.IsNullOrEmpty(stationName))
                //  {
                // MessageQueue.Enqueue_RegAndLogout(string.Format("【{0}】: {1}登录系统", DateTime.Now.ToString(), "未知的客户端"));
                //  }
                // else
                // {
                // MessageQueue.Enqueue_RegAndLogout(string.Format("【{0}】: {1}登录系统", DateTime.Now.ToString(), stationName));
                // }

                DTU_ClientManager.AddClient(tel, socket);
            }
            catch (Exception ex)
            {
                LogMg.AddError(ex);
            }
            LogMg.AddDebug(string.Format("退出客户端注册方法RegisterDataHandler    Tel:{0}", tel));
        }
Exemple #15
0
        /// <summary>
        /// 执行DTU控制任务
        /// </summary>
        /// <param name="state"></param>
        private static void ExecuteDtuOrder(object state)
        {
            //1.锁住客户端对象
            //2.找出客户端对应的数据库,并找出需要执行的控制指令。
            //3.循环发送指令到客户端,再接收返回的数据,存入数据库。
            //4.释放客户端对象
            DTUClientInfo client = (DTUClientInfo)state;

            using (MyLock mylock = new MyLock(client, 20000, false))
            {
                if (mylock.IsTimeout == false)
                {
                    //LogManager.AddDebug("ControlCommandThread线程:" + Thread.CurrentThread.ManagedThreadId + " 开始锁了    是否超时" + mylock.IsTimeout);
                    //找出这个站点对应的数据库,   并获取数据库连接
                    SqlConnection connection = GetDbConnectionByTel(client.TelOrGprsId);
                    if (connection == null)   //找不到对应的数据库连接
                    {
                        LogMg.AddDebug(string.Format("根据Tel={0}的设备唯一id,在sysconfig.xml中找不到对应的数据库连接", client.TelOrGprsId.ToString()));
                        return;
                    }
                    SWSDataContext db = new SWSDataContext(connection);

                    takePhoto(db, client.StationId, client);

                    //把那些不需要执行了的数据 state=1  就当这行指令已经执行过了
                    List <int> giveUpCommands = db.ExecuteQuery <int>("SELECT id FROM dbo.control_command  WHERE station_id=" + client.StationId + " AND (state=0 OR state IS NULL) AND id NOT IN (SELECT MAX(id) FROM dbo.control_command   WHERE  station_id=" + client.StationId + " AND   (state=0 OR state IS NULL)  GROUP BY gong_kuang_id,read_or_write) ").ToList();
                    if (giveUpCommands.Count > 0)
                    {
                        db.ExecuteCommand("UPDATE dbo.control_command SET state=1 WHERE id in(" + string.Join(", ", giveUpCommands) + ")");
                    }
                    //db.ExecuteCommand("UPDATE dbo.control_command SET state=1 WHERE station_id=" + client.StationId + " AND (state=0 OR state IS NULL) AND id NOT IN (SELECT MAX(id) FROM dbo.control_command   WHERE  station_id=" + client.StationId + " AND   (state=0 OR state IS NULL)  GROUP BY gong_kuang_id,read_or_write) ");

                    //获取需要执行的指令
                    List <control_command> commands = db.ExecuteQuery <control_command>("SELECT * FROM dbo.control_command WHERE id IN(SELECT MAX(id) FROM dbo.control_command   WHERE  station_id=" + client.StationId + "  AND communication_mode=2 AND   (state=0 OR state IS NULL)  GROUP BY gong_kuang_id,read_or_write) ORDER BY add_datetime").ToList();

                    List <gong_kuang_config> gongKuangs = db.gong_kuang_config.Where(c => c.station_id == client.StationId).ToList();
                    foreach (control_command command in commands)  //循环发送指令
                    {
                        if (command.COUNT == null)
                        {
                            command.COUNT = 0;
                        }
                        try
                        {
                            gong_kuang_config gongkuang = gongKuangs.SingleOrDefault(c => c.id == command.gong_kuang_id);
                            if (gongkuang == null)  //如果找不到command对应和gongkuangid,则把这条command标记为执行失败。
                            {
                                LogMg.AddError("找不到控制命令所对应的工况配置信息");
                                command.execute_result    = 0; //标记为执行失败
                                command.state             = 1; // 就当已经执行过
                                command.execute_comment   = "执行失败,找不到控制命令所对应的工况配置信息";
                                command.complete_datetime = DateTime.Now;
                                db.SubmitChanges();
                                continue;
                            }
                            byte address = (byte)gongkuang.address;
                            ClearSocketCache(client.socket);//清除缓冲区
                            LogMg.AddDebug("hashcode=" + client.socket.GetHashCode());
                            ModbusReturn modbusReturn = new ModbusReturn();
                            if (command.read_or_write == 0)   //读读读读读读读读读读读
                            {
                                while (modbusReturn.success == false && command.COUNT < SysConfig.userProfile.ExecuteFailureCount)
                                {
                                    modbusReturn.clear();
                                    modbus.readdata(client.Protocol, client.socket, "", address, ushort.Parse(gongkuang.read_register), int.Parse(gongkuang.function_code), gongkuang.data_type, gongkuang.decode_order, (int)gongkuang.receive_timeout, modbusReturn);
                                    //LogManager.AddDebug(String.Format("执行读命令   GPRSID:{0}  gongkuangid:{1}   address{2}   读寄存器编号:{3}  读取的值{4}   方法的返回bool值:{5}", client.TelOrGprsId, gongkuang.id, address, gongkuang.read_register, value.ToString(), flag));
                                    command.COUNT++;
                                }
                                command.state = 1;  //标志为已发送
                                if (modbusReturn.success == true)
                                {
                                    DTU_ClientManager.UpdateLastVisitTime(client, DateTime.Now);
                                    command.execute_result    = 1; //标记为执行成功
                                    command.value             = Convert.ToString(modbusReturn.value);
                                    gongkuang.read_value      = Convert.ToString(modbusReturn.value);
                                    gongkuang.execute_comment = "执行读取操作成功";
                                    if (gongkuang.config_type == "05")
                                    {
                                        try
                                        {
                                            test test = db.test.Single(c => c.testid == gongkuang.testid);
                                            test.value = Convert.ToString(modbusReturn.value * gongkuang.Multiple + gongkuang.AddNumber);
                                        }
                                        catch (Exception ex)
                                        {
                                            LogMg.AddError(ex);
                                            LogMg.AddDebug("找不到工况对应的检测点,gongkuangid=" + gongkuang.id + "   testid=" + gongkuang.testid);
                                        }
                                    }
                                }
                                else
                                {
                                    command.execute_result    = 0; //标记为执行失败
                                    gongkuang.execute_comment = "执行读取操作失败";
                                }
                                command.execute_comment = modbusReturn.ToString();
                            }
                            else    //写写写写写写写写写写写写写写写
                            {
                                while (modbusReturn.success == false && command.COUNT < SysConfig.userProfile.ExecuteFailureCount)
                                {
                                    modbusReturn.clear();
                                    modbus.writedata(client.Protocol, client.socket, "", address, ushort.Parse(gongkuang.write_register), gongkuang.data_type, (int)gongkuang.receive_timeout, short.Parse(command.value), modbusReturn);
                                    //LogManager.AddDebug(String.Format("执行写命令   GPRSID:{0}   address{1}   写寄存器编号:{2}  写入的值{3}   方法的返回bool值:{4}", client.TelOrGprsId, address, gongkuang.write_register, command.value, flag));
                                    command.COUNT++;
                                }
                                command.state = 1;  //标志为已发送
                                if (modbusReturn.success == true)
                                {
                                    DTU_ClientManager.UpdateLastVisitTime(client, DateTime.Now);
                                    command.execute_result    = 1; //标记为执行成功
                                    gongkuang.write_value     = command.value;
                                    gongkuang.execute_comment = "执行写操作成功";
                                }
                                else
                                {
                                    command.execute_result    = 0; //标记为执行失败
                                    gongkuang.execute_comment = "执行写操作失败";
                                }
                                command.execute_comment = modbusReturn.ToString();
                            }
                            gongkuang.execute_datetime = DateTime.Now;
                            command.complete_datetime  = DateTime.Now;
                        }
                        catch (Exception ex)
                        {
                            command.execute_result  = 0; //标记为执行失败
                            command.execute_comment = "执行命令失败,服务程序出现了异常";
                            // MessageQueue.Enqueue_DataInfo(string.Format("接收时间:【{0}】,站点:{1},   执行工况配置失败.程序出现异常,请查看日志.  ", DateTime.Now, station.name));
                            LogMg.AddError(ex);
                        }
                        finally
                        {
                            command.complete_datetime = DateTime.Now;
                        }
                        db.SubmitChanges();
                    }
                    //LogManager.AddDebug("ControlCommandThread线程:" + Thread.CurrentThread.ManagedThreadId + " 释放锁了   ");
                }
                else
                {
                    LogMg.AddDebug("ControlCommandThread 锁失败了");
                }
            }
        }
Exemple #16
0
        private static void ExecuteGuangDai透传Order(object state)
        {
            try
            {
                Args           arg = (Args)state;
                SWSDataContext db  = new SWSDataContext(ConnectStringHelper.GetConnection(SysConfig.userProfile.DbAddress, arg.dbName, SysConfig.userProfile.DbUserName, SysConfig.userProfile.DbPassword));

                GuangDaiService.CorePlatformWebServiceClient ws = new GuangDaiService.CorePlatformWebServiceClient(arg.gdServerCfg);
                SWS_DBDataContext            SWS_DB             = new SWS_DBDataContext(ConnectStringHelper.GetConnection(SysConfig.userProfile.DbAddress, SysConfig.userProfile.DbName, SysConfig.userProfile.DbUserName, SysConfig.userProfile.DbPassword));
                List <guangdai_station_link> links = SWS_DB.guangdai_station_link.ToList();
                String wscid = getWscId(arg, db, links);
                if (String.IsNullOrEmpty(wscid))
                {
                    LogMg.AddDebug("站点id=" + arg.station_id + "  找不到对应的wscId");
                    return;
                }
                //把那些不需要执行了的数据 state=1  就当这行指令已经执行过了
                List <int> giveUpCommands = db.ExecuteQuery <int>("SELECT id FROM dbo.control_command  WHERE station_id=" + arg.station_id + " AND (state=0 OR state IS NULL) AND id NOT IN (SELECT MAX(id) FROM dbo.control_command   WHERE  station_id=" + arg.station_id + " AND   (state=0 OR state IS NULL)  GROUP BY gong_kuang_id,read_or_write) ").ToList();
                if (giveUpCommands.Count > 0)
                {
                    db.ExecuteCommand("UPDATE dbo.control_command SET state=1 WHERE id in(" + string.Join(", ", giveUpCommands) + ")");
                }
                //db.ExecuteCommand("UPDATE dbo.control_command SET state=1 WHERE station_id=" + arg.station_id + " AND (state=0 OR state IS NULL) AND id NOT IN (SELECT MAX(id) FROM dbo.control_command   WHERE  station_id=" + arg.station_id + " AND   (state=0 OR state IS NULL)  GROUP BY gong_kuang_id,read_or_write) ");

                //获取需要执行的指令
                List <control_command> commands = db.ExecuteQuery <control_command>("SELECT * FROM dbo.control_command WHERE id IN(SELECT MAX(id) as id  FROM dbo.control_command   WHERE  station_id=" + arg.station_id + " AND communication_mode in (5,6)  AND tp is Null AND  (state=0 OR state IS NULL)  GROUP BY gong_kuang_id,read_or_write) ORDER BY add_datetime").ToList();

                TakePhotoTp5(db, ws, arg.station_id, wscid);
                for (int i = 0; i < commands.Count; i++)
                {
                    control_command command = commands[i];
                    if (command.COUNT == null)
                    {
                        command.COUNT = 0;
                    }
                    try
                    {
                        gong_kuang_config gongkuang = db.gong_kuang_config.SingleOrDefault(c => c.id == command.gong_kuang_id);
                        if (gongkuang == null)  //如果找不到command对应和gongkuangid,则把这条command标记为执行失败。
                        {
                            LogMg.AddError("找不到控制命令所对应的工况配置信息");
                            command.execute_result    = 0; //标记为执行失败
                            command.state             = 1; // 就当已经执行过
                            command.execute_comment   = "执行失败,找不到控制命令所对应的工况配置信息";
                            command.complete_datetime = DateTime.Now;
                            db.SubmitChanges();
                            continue;
                        }
                        ModbusReturn modbusReturn = new ModbusReturn();
                        byte         address      = (byte)gongkuang.address;

                        if (command.read_or_write == 0)   //读读读读读读读读读读读
                        {
                            GuangDaiCommunicationModbus.readdata(ws, wscid, address, ushort.Parse(gongkuang.read_register), int.Parse(gongkuang.function_code), gongkuang.data_type, gongkuang.decode_order, modbusReturn);
                            command.state = 1;  //标志为已发送
                            if (modbusReturn.success == true)
                            {
                                command.execute_result = 1;  //标记为执行成功
                                command.value          = Convert.ToString(modbusReturn.value);

                                gongkuang.read_value      = Convert.ToString(modbusReturn.value);
                                gongkuang.execute_comment = "执行读取操作成功";
                                if (gongkuang.config_type == "05")
                                {
                                    try
                                    {
                                        test   test      = db.test.Single(c => c.testid == gongkuang.testid);
                                        double multiple  = gongkuang.Multiple == null ? 1 : (double)gongkuang.Multiple;
                                        double addNumber = gongkuang.AddNumber == null ? 0 : (double)gongkuang.AddNumber;
                                        test.value = Convert.ToString(modbusReturn.value * multiple + addNumber);
                                    }
                                    catch (Exception ex)
                                    {
                                        LogMg.AddError(ex);
                                        LogMg.AddDebug("找不到工况对应的检测点,gongkuangid=" + gongkuang.id + "   testid=" + gongkuang.testid);
                                    }
                                }
                            }
                            else
                            {
                                command.execute_result    = 0; //标记为执行失败
                                gongkuang.execute_comment = "执行读取操作失败";
                            }
                            command.execute_comment = modbusReturn.ToString();
                        }
                        if (command.read_or_write == 1)      //写写写写写写写写写写写写写写写
                        {
                            GuangDaiCommunicationModbus.writedata(ws, wscid, address, ushort.Parse(gongkuang.read_register), int.Parse(gongkuang.function_code), gongkuang.data_type, gongkuang.decode_order, short.Parse(command.value), modbusReturn);
                            //   GuangDaiService.config config = ws.doConfig(wscid, Convert.ToInt32(gongkuang.write_register), (int)command.read_or_write, Convert.ToInt32(command.value), "");
                            // LogManager.AddDebug(String.Format("ws.transmitTransparently({0},{1}, {2}, {3}, {4})   返回结果是 {5}    ", wscid, gongkuang.write_register, command.read_or_write, "-1", "", flag));
                            command.state = 1;  //标志为已发送
                            if (modbusReturn.success == true)
                            {
                                command.execute_result    = 1; //标记为执行成功
                                gongkuang.write_value     = command.value;
                                gongkuang.execute_comment = "执行写操作成功";
                            }
                            else
                            {
                                command.execute_result    = 0; //标记为执行失败
                                gongkuang.execute_comment = "执行写操作失败";
                            }
                            command.execute_comment = modbusReturn.ToString();
                        }
                        gongkuang.execute_datetime = DateTime.Now;
                        command.complete_datetime  = DateTime.Now;
                    }
                    catch (Exception ex)
                    {
                        command.execute_result  = 0; //标记为执行失败
                        command.execute_comment = "执行命令失败,服务程序出现了异常";
                        // MessageQueue.Enqueue_DataInfo(string.Format("接收时间:【{0}】,站点:{1},   执行工况配置失败.程序出现异常,请查看日志.  ", DateTime.Now, station.name));
                        LogMg.AddError(ex);
                    }
                    finally
                    {
                        command.complete_datetime = DateTime.Now;
                    }
                    try
                    {
                        db.SubmitChanges();
                    }
                    catch (Exception ex)
                    {
                        LogMg.AddError(ex.ToString());
                    }
                }
            }
            catch (Exception ex)
            {
                Args arg = (Args)state;
                LogMg.AddError("异常数据库是:" + arg.dbName + "   站点id=" + arg.station_id);
                LogMg.AddError(ex);
            }
        }
        private void handlerV88Msg(string data)
        {
            LogMg.AddDebug("udp协议接收的数据=============     " + data);
            List <String> datas = CaiBao(data);
            DateTime      date  = DateTime.Now;
            List <Dictionary <string, string> > listDir = parseDataV88(datas, ref date);

            foreach (Dictionary <string, string> item in listDir)
            {
                try
                {   //找出设备编号
                    String stationNo = getStationNo(item);
                    if (String.IsNullOrEmpty(stationNo))
                    {
                        continue;
                    }
                    //找出编号对应的站点
                    SWS_DB.guangdai_station_link link = V88StationLink.SingleOrDefault(c => c.wsid == stationNo);
                    if (link == null)
                    {
                        continue;
                    }
                    SWSDataContext sws = new SWSDataContext(ConnectStringHelper.GetConnection(SysConfig.userProfile.DbAddress, link.db_name, SysConfig.userProfile.DbUserName, SysConfig.userProfile.DbPassword));
                    updateStationLine(sws, (int)link.station_id);
                    foreach (string key in item.Keys)
                    {
                        //数据
                        KeyValuePair <string, string> data1 = item.Single(c => c.Key == key);
                        try
                        {
                            KeyValuePair <string, int>?keyAndValue = getRegisterByKey(key);
                            if (keyAndValue == null)
                            {
                                continue;
                            }
                            List <gong_kuang_config> gks = sws.gong_kuang_config.Where(c => c.station_id == link.station_id && c.read_register == ((KeyValuePair <string, int>)keyAndValue).Value.ToString()).ToList();
                            foreach (gong_kuang_config gk in gks)
                            {
                                if (gk == null || gk.testid == null || gk.testid == 0)
                                {
                                    continue;
                                }
                                AutoCollectionThread.updateStationOnlineInfo(sws, (int)link.station_id);
                                //保存数据
                                double multiple  = gk.Multiple == null ? 1 : (double)gk.Multiple;
                                double addNumber = gk.AddNumber == null ? 0 : (double)gk.AddNumber;
                                String strValue  = data1.Value;
                                if (key.StartsWith("k") || key.StartsWith("K"))
                                {
                                    if (strValue == "1")
                                    {
                                        strValue = "0";
                                    }
                                    else
                                    {
                                        strValue = "1";
                                    }
                                }
                                double value = Double.Parse(strValue) * multiple + addNumber;
                                gk.read_value = value.ToString();
                                realrec rec = new realrec();
                                rec.testid   = gk.testid;
                                rec.value    = (decimal)value;
                                rec.testtime = date;
                                rec.remark   = "V88协议的设备数据";
                                sws.realrec.InsertOnSubmit(rec);
                            }
                        }
                        catch (Exception ex)
                        {
                            LogMg.AddError(ex);
                        }
                    }
                    sws.SubmitChanges();
                }
                catch (Exception ex)
                {
                    LogMg.AddError(ex);
                }
            }
        }
 private void saveLog(byte[] bytes)
 {
     LogMg.AddDebug("udp协议接收的其他数据=============     " + ToHexString(bytes));
 }
Exemple #19
0
        /// <summary>
        /// 写寄存器
        /// </summary>
        /// <param name="socket"></param>
        /// <param name="address"></param>
        /// <param name="start"></param>
        /// <param name="registers"></param>
        /// <param name="values"></param>
        /// <returns></returns>
        public static void SendFc16(GuangDaiService.CorePlatformWebServiceClient ws, String wscId, byte address, ushort start, ushort registers, string dataType, short[] values, ModbusReturn modbusReturn)
        {
            LogMg.AddDebug("SendFc16(Socket socket,String  " + wscId + " byte " + address + ", ushort " + start + ", ushort " + registers + ", string " + dataType + ", short " + values[0] + ")");
            int dataLength = 2;    //默认数据长度为两个字节

            if (dataType == "2")   //说明是"32位无符号二进制"   所以要用4个字节
            {
                dataLength = 4;
            }
            //Message is 1 addr + 1 fcn + 2 start + 2 reg + 1 count + 2 * reg vals + 2 CRC
            byte[] message = new byte[9 + dataLength * registers];
            //Function 16 response is fixed at 8 bytes
            byte[] response = new byte[8];

            try
            {
                //Add bytecount to message:
                message[6] = (byte)(registers * dataLength);
                //Put write values into message prior to sending:
                for (int i = 0; i < registers; i++)
                {
                    for (int j = 0; j < dataLength; j++)
                    {
                        message[7 + (dataLength * i) + j] = (byte)(values[i] >> 8 * (dataLength - j - 1));  //将value[i]转换成指定字节长度的字节数组, (高位在前,低位在后)   message下标从第七个开始
                    }
                    //message[7 + 2 * i] = (byte)(values[i] >> 8);
                    //message[8 + 2 * i] = (byte)(values[i]);
                }

                //Build outgoing message:
                BuildMessage(address, (byte)16, start, registers, ref message);
                //string msg = "";
                //for (int i = 0; i < message.Length; i++)
                //{
                //    msg += "\r\n   message[" + i + "]=   值:" + message[i];
                //}
                //LogManager.AddDebug("写  message.length=" + message.Length + msg);
            }
            catch (Exception ex)
            {
                LogMg.AddError(ex);
            }

            //Send Modbus message to Serial Port:
            try
            {
                modbusReturn.sendMsg = message;
                modbusReturn.wscid   = wscId;
                modbusReturn.address = (int)address;

                GuangDaiService.transmitTransparentlyResult result = ws.transmitTransparently(wscId, (int)address, CommonUtil.byteToHexStr(message, ""));
                modbusReturn.RecMsg = result.value;
                if (result.result == true)
                {
                    response = result.value;
                }
                else
                {
                    modbusReturn.ErrorMsg = result.message;
                    modbusReturn.success  = false;
                    return;
                }
                //DTU.SendUserData(protocol, socket, message, message.Length, tel, channel);
                ////sp.Write(message, 0, message.Length);
                //GetResponse(protocol, socket, receiveTimeout, ref response);
            }
            catch (Exception err)
            {
                modbusReturn.ErrorMsg = "Error in write event: " + err.Message;
                modbusReturn.success  = false;
                return;
            }
            //Evaluate message:
            if (CheckResponse(response))
            {
                modbusReturn.ErrorMsg = "Write successful";
                modbusReturn.success  = true;
                return;
            }
            else
            {
                modbusReturn.ErrorMsg = "CRC error";
                modbusReturn.success  = false;
                return;
            }

            /* }
             * else
             * {
             *   modbusStatus = "Serial port not open";
             *   return false;
             * }
             * */
        }
Exemple #20
0
        public static void DownLoad(Socket socket, string json)
        {
            S_To_C_Data <ClientConfig> sendObj = new S_To_C_Data <ClientConfig>();
            ClientConfig _clientConfig         = new ClientConfig();

            try
            {
                C_To_S_Data <ClientConfig>            obj      = Utility.JsonHelper.JsonDeserialize <C_To_S_Data <ClientConfig> >(json);
                Clazz.Config.ClientConfig.XML_Station _station = SysConfig.clientConfig.AllStation.SingleOrDefault(c => c.TransferCode == obj.TransferCode);

                if (_station == null)
                {
                    LogMg.AddDebug("TransferCode:" + obj.TransferCode + " 不存在");
                }
                else
                {
                    _clientConfig.OrgId           = SysConfig.clientConfig.GetOrgIdByTransferCode(obj.TransferCode);
                    _clientConfig.StationId       = SysConfig.clientConfig.GetStationIdByTransferCode(obj.TransferCode);
                    _clientConfig.ListCountryTest = (from c in SysConfig.clientConfig.AllCountryTest
                                                     where c.StationUniqueId == _station.UniqueId
                                                     select new CSDataStandard.Config.CountryTest
                    {
                        NodeId = c.NodeId,
                        TestId = c.TestId,
                        Multiple = c.Multiple
                    }).ToList();
                    _clientConfig.ListDevice = new List <DeviceControl>();
                    //(from c in SysConfig.clientConfig.AllDevice
                    //                        where c.StationUniqueId == _station.UniqueId
                    //                        select new CSDataStandard.Config.DeviceControl
                    //                        {
                    //                            Number = c.Number,
                    //                            DeviceId = c.DeviceId
                    //                        }).ToList();
                    _clientConfig.ListPMQCTest = (from c in SysConfig.clientConfig.AllPMQCTest
                                                  where c.StationUniqueId == _station.UniqueId
                                                  select new CSDataStandard.Config.PMQCTest
                    {
                        X = c.X,
                        Y = c.Y,
                        TestId = c.TestId,
                        Id = c.Id,
                        Name = c.Name
                    }).ToList();
                    _clientConfig.ListMobileDetection = (from c in SysConfig.clientConfig.AllMobileDetection
                                                         where c.StationUniqueId == _station.UniqueId
                                                         select new CSDataStandard.Config.MobileDetection
                    {
                        TestId = c.TestId,
                        TestTarger = c.TestTarger
                    }).ToList();
                    _clientConfig.ListMcgsTest = (from c in SysConfig.clientConfig.AllMCGSTest
                                                  where c.StationUniqueId == _station.UniqueId
                                                  select new CSDataStandard.Config.MCGSTest
                    {
                        TestId = c.TestId,
                        ColumnName = c.ColumnName,
                        TestName = c.TestName
                    }).ToList();
                }
            }
            catch (Exception ex)
            {
                LogMg.AddError(ex);
            }

            sendObj.Data = new List <ClientConfig>();
            sendObj.Data.Add(_clientConfig);                                 //添加数据
            sendObj.Flag    = CSDataStandard.Enum.HandleFlag.DownLoadConfig; //类型为  下载配置文件
            sendObj.Success = true;

            string sendJSON = Utility.JsonHelper.JsonSerializer(sendObj);

            socket.Send(Encoding.Unicode.GetBytes(sendJSON));
        }
Exemple #21
0
        private static void TakePhotoTp5(SWSDataContext db, GuangDaiService.CorePlatformWebServiceClient ws, int stationId, string wscid)
        {
            //  List<control_command> commands = db.control_command.Where(c => c.station_id == stationId && c.tp.Trim() == "5" && c.state == 0).ToList();
            List <control_command> commands = db.ExecuteQuery <control_command>("SELECT * FROM dbo.control_command WHERE station_id=" + stationId + " AND state=0 AND tp='5'").ToList();

            foreach (control_command command in commands)
            {
                if (command.COUNT == null)
                {
                    command.COUNT = 0;
                }
                try
                {
                    gong_kuang_config gongkuang = db.gong_kuang_config.SingleOrDefault(c => c.id == command.gong_kuang_id);
                    if (gongkuang == null)  //如果找不到command对应和gongkuangid,则把这条command标记为执行失败。
                    {
                        LogMg.AddError("找不到控制命令所对应的工况配置信息");
                        command.execute_result    = 0; //标记为执行失败
                        command.state             = 1; // 就当已经执行过
                        command.execute_comment   = "执行失败,找不到控制命令所对应的工况配置信息";
                        command.complete_datetime = DateTime.Now;
                        db.SubmitChanges();
                        continue;
                    }
                    byte address = (byte)gongkuang.address;

                    GuangDaiService.config config = ws.doConfig(wscid, Convert.ToInt32(gongkuang.write_register), (int)command.read_or_write, Convert.ToInt32(command.value), "");
                    LogMg.AddDebug(String.Format(" ws.doConfig({0},{1}, {2}, {3}, {4})   返回结果是   result={5}   value={6}   message={7}  ", wscid, gongkuang.write_register, command.read_or_write, "-1", "", config.result, config.value, config.message));
                    command.state = 1;  //标志为已发送
                    if (config.result == true)
                    {
                        command.execute_result    = 1; //标记为执行成功
                        command.execute_comment   = "执行写操作成功";
                        gongkuang.write_value     = command.value;
                        gongkuang.execute_comment = "执行写操作成功";
                    }
                    else
                    {
                        command.execute_result    = 0; //标记为执行失败
                        command.execute_comment   = "执行广岱拍照操作失败";
                        gongkuang.execute_comment = "执行广岱拍照操作失败";
                    }

                    gongkuang.execute_datetime = DateTime.Now;
                    command.complete_datetime  = DateTime.Now;
                }
                catch (Exception ex)
                {
                    command.execute_result  = 0; //标记为执行失败
                    command.execute_comment = "执行广岱拍照操作失败,服务程序出现了异常";
                    // MessageQueue.Enqueue_DataInfo(string.Format("接收时间:【{0}】,站点:{1},   执行工况配置失败.程序出现异常,请查看日志.  ", DateTime.Now, station.name));
                    LogMg.AddError(ex);
                }
                finally
                {
                    command.complete_datetime = DateTime.Now;
                }
                try
                {
                    db.SubmitChanges();
                }
                catch (Exception ex)
                {
                    LogMg.AddError(ex.ToString());
                }
            }
        }
Exemple #22
0
        private static void ExecuteGD透传Order(object state)
        {
            GD_Station     gd_station = (GD_Station)state;
            List <XML_Org> orgs       = SysConfig.orgConfig.Orgs.Where(c => c.OrgId == gd_station.OrgId).ToList();

            if (orgs == null || orgs.Count == 0)
            {
                LogMg.AddError("找不到orgId:" + gd_station.OrgId);
                return;
            }
            XML_Org org = orgs[0];

            GuangDaiService.CorePlatformWebServiceClient ws = new GuangDaiService.CorePlatformWebServiceClient(org.gdServerCfg);
            if (gd_station != null && gd_station.tests != null)
            {
                foreach (Clazz.Config.XML_Test test in gd_station.tests)
                {
                    try
                    {
                        ModbusReturn modbusReturn = new ModbusReturn();
                        int          count        = 0; //执行错误的次数
                        while (modbusReturn.success == false && count < SysConfig.userProfile.ExecuteFailureCount)
                        {
                            modbusReturn.clear();
                            //LogManager.AddDebug(string.Format("开始接收  时间:{0},站点:{1}  testid:{2}", DateTime.Now.ToString(), gd_station.name, test.TestId));
                            GuangDaiCommunicationModbus.readdata(ws, gd_station.wscId, test.Address, test.RegisterNo, test.FunctionCode, test.DataType, test.DecodeOrder, modbusReturn);
                            // result = modbus.readdata(gd_station.Protocol, gd_station.socket, gd_station.TelOrGprsId, test.Address, test.RegisterNo, test.FunctionCode, test.DataType, test.DecodeOrder, test.ReceiveTimeout, ref value);
                            log.Info("wscId=" + gd_station.wscId + " testid=" + test.TestId + "  address=" + test.Address + "  " + modbusReturn.ToString());
                            count++;
                        }

                        if (modbusReturn.success)                                                                 //接收数据成功
                        {
                            if (Between(modbusReturn.value * test.Multiple + test.AddNumber, test.Min, test.Max)) //如果值不在取值范围内,则不要
                            {
                                SWSDataContext db = new SWSDataContext(Util.ServerSocketHelper.GetConnection(gd_station.dbName));
                                SaveToDatabase(db, test.TestId, modbusReturn.value * test.Multiple + test.AddNumber);    //保存数据
                                LogMg.AddDebug(string.Format("接收时间:{0}, 站点:{1},value:{2},testid:{3}", DateTime.Now.ToString(), gd_station.name, modbusReturn.value * test.Multiple + test.AddNumber, test.TestId));
                                // MessageQueue.Enqueue_DataInfo(string.Format("接收时间:【{0}】,站点:{1},testid:{2},值:{3}", DateTime.Now.ToString(), station.Name, test.TestId, value * test.Multiple + test.AddNumber));
                                updateStationOnlineInfo(db, gd_station.stationId);
                            }
                            else
                            {
                                LogMg.AddDebug(string.Format("接收时间:【{0}】,站点:{1},testid:{2},乘以倍率之后的值:{3}   由于值不在范围内[{4},{5}],丢弃", DateTime.Now.ToString(), gd_station.name, test.TestId, modbusReturn.value * test.Multiple + test.AddNumber, test.Min, test.Max));
                            }
                        }
                        else
                        {
                            //接收数据失败
                            LogMg.AddDebug(string.Format("数据库名:{0}    testid:{1}   接收数据失败   原因:{2}", gd_station.dbName, test.TestId, modbusReturn.ErrorMsg));
                            //   MessageQueue.Enqueue_DataInfo(string.Format("接收时间:【{0}】,站点:{1},testid:{2}, 接收数据失败", DateTime.Now.ToString(), station.Name, test.TestId));
                        }
                    }
                    catch (SocketException)
                    {
                    }
                    catch (Exception ex)
                    {
                        LogMg.AddError(ex);
                        DEBUG.MsgBox(ex.ToString());
                    }
                }
            }
        }
Exemple #23
0
        /// <summary>
        /// 读寄存器或线圈
        /// </summary>
        /// <param name="address">485总线上的设备地址</param>
        /// <param name="RegisterStartAdd">寄存器起始地址      寄存器个数为1,方法内部已写死。</param>
        /// <param name="ModbusfunctionCode">功能码   </param>
        /// <param name="dataType">寄存器类型     1代表16位无符号二进制的寄存器,2代表32位的寄存器</param>
        /// <param name="DecodeOrder">获取的数据的高低位顺序</param>
        /// <param name="receiveTimeout">超时时间 </param>
        /// <param name="channel">GPRS控制器的通道号,也是串口号</param>
        /// <param name="values">返回的value值   虽然是个数组,但数据只有一个,下标为0</param>
        /// <returns></returns>
        private static void SendFc03(GuangDaiService.CorePlatformWebServiceClient ws, String wscId, byte address, ushort RegisterStartAdd,
                                     int ModbusfunctionCode, string dataType, string DecodeOrder, ModbusReturn modbusReturn)
        {
            int dataLength = 2;    //16位无符号二进制的寄存器数据长度为两个字节     默认为16位寄存器

            if (dataType == "2")   //说明是"32位无符号二进制"   所以要用4个字节
            {
                dataLength = 4;
            }
            LogMg.AddDebug("dataLength=" + dataLength);

            byte[] message = new byte[8];
            //Function 3 response buffer:
            byte[] response = new byte[5 + dataLength * 1];

            //Build outgoing modbus message:
            LogMg.AddDebug(" BuildMessage(" + address + ", " + ModbusfunctionCode + ", " + RegisterStartAdd + ", 1, ref message);");
            BuildMessage(address, (byte)ModbusfunctionCode, RegisterStartAdd, ushort.Parse(dataType), ref message);
            //Send modbus message to Serial Port:
            //
            try
            {
                //   GuangDaiService.transmitTransparentlyResult result = ws.transmitTransparently("160512027", 1, "01030098000105E5");
                modbusReturn.sendMsg = message;
                modbusReturn.wscid   = wscId;
                modbusReturn.address = (int)address;
                String hexCommand = CommonUtil.byteToHexStr(message, "");
                GuangDaiService.transmitTransparentlyResult result = ws.transmitTransparently(wscId, (int)address, hexCommand);
                modbusReturn.RecMsg = response;
                if (result.result == true)
                {
                    response            = result.value;
                    modbusReturn.RecMsg = result.value;
                }
                else
                {
                    modbusReturn.ErrorMsg = result.message;
                    modbusReturn.success  = false;
                    return;
                }
                ////日志
                //for (int i = 0; i < response.Length; i++)
                //{
                //    msg += response[i] + " ";
                //}
                //LogManager.AddDebug("functionCode=" + ModbusfunctionCode + "     response.length=" + response.Length + "    response=" + msg);
            }
            catch (SocketException ex)
            {
                modbusReturn.ErrorMsg = ex.Message;
                modbusReturn.success  = false;

                return;
            }
            catch (Exception ex)
            {
                LogMg.AddError(ex);
                modbusReturn.ErrorMsg = "Error in read event: " + ex.Message;
                modbusReturn.success  = false;
                return;
            }
            //处理返回结果
            if (CheckResponse(response))
            {
                int datalength = response[2];  //数据长度(单位:字节)
                LogMg.AddDebug("返回的数据长度datalength=" + datalength);
                //设置默认解码顺序
                if (string.IsNullOrEmpty(DecodeOrder))
                {
                    if (datalength == 2)
                    {
                        DecodeOrder = "12";
                    }
                    else if (datalength == 4)
                    {//三四个字节为高位,一二两个字节为低位
                        DecodeOrder = "3412";
                    }
                }
                //解析返回的modbus数据
                modbusReturn.value = 0;
                try
                {
                    for (int i = 0; i < DecodeOrder.Length; i++)
                    {
                        int n = Convert.ToInt32(DecodeOrder[i].ToString());
                        modbusReturn.value <<= 8;
                        modbusReturn.value  += response[2 + n];
                    }
                }
                catch (Exception)
                {
                    modbusReturn.ErrorMsg = "解析返回的modbus时出错,modbus指令为:" + response;
                    modbusReturn.success  = false;
                    return;
                }
                modbusReturn.ErrorMsg = "Read successful";
                modbusReturn.success  = true;
                return;
            }
            else
            {
                modbusReturn.ErrorMsg = "CRC error";
                modbusReturn.success  = false;
                return;
            }
        }
Exemple #24
0
        private static void ExecuteOrder(object state)
        {
            ///第一步:找出需要采集的指标
            ///第二步:循环采集的指标,取出一个指标,
            ///第三步:锁住客户端
            ///第四步:采集
            ///第五步:释放客户端
            ///第六步:回到第二步
            ///结束
            DTUClientInfo client = (DTUClientInfo)state;

            Clazz.Config.XML_Station station = SysConfig.DTU_StationConfig.GetStationByTel(client.TelOrGprsId);
            if (station == null)
            {
                return;
            }
            List <Clazz.Config.XML_Test> listTest = station.ListTest;

            if (station != null && listTest != null)
            {
                foreach (Clazz.Config.XML_Test test in listTest)
                {
                    using (MyLock mylock = new MyLock(client, 3000, false))   // 在循环内锁
                    {
                        if (mylock.IsTimeout == false)
                        {
                            //LogManager.AddDebug("AutoCollectionThread 线程:" + Thread.CurrentThread.ManagedThreadId + " 开始锁了    是否超时" + mylock.IsTimeout);

                            try
                            {
                                ModbusReturn modbusReturn = new ModbusReturn();
                                int          count        = 0; //执行错误的次数
                                while (modbusReturn.success == false && count < SysConfig.userProfile.ExecuteFailureCount)
                                {
                                    modbusReturn.clear();
                                    LogMg.AddDebug(string.Format("开始接收  时间:{0},站点:{1} tel:{2},testid:{3}", DateTime.Now.ToString(), client.Name, client.TelOrGprsId.ToString(), test.TestId));
                                    modbus.readdata(client.Protocol, client.socket, client.TelOrGprsId, test.Address, test.RegisterNo, test.FunctionCode, test.DataType, test.DecodeOrder, test.ReceiveTimeout, modbusReturn);
                                    count++;
                                }

                                if (modbusReturn.success) //接收数据成功
                                {
                                    DTU_ClientManager.UpdateLastVisitTime(client, DateTime.Now);
                                    if (Between(modbusReturn.value * test.Multiple + test.AddNumber, test.Min, test.Max))    //如果值不在取值范围内,则不要
                                    {
                                        SWSDataContext db = new SWSDataContext(Util.ServerSocketHelper.GetConnection(station.Org.DBName));
                                        SaveToDatabase(db, test.TestId, modbusReturn.value * test.Multiple + test.AddNumber);    //保存数据
                                        LogMg.AddDebug(string.Format("接收时间:{0}, tel:{1},value:{2},testid:{3}", DateTime.Now.ToString(), client.TelOrGprsId.ToString(), modbusReturn.value * test.Multiple + test.AddNumber, test.TestId));
                                        // MessageQueue.Enqueue_DataInfo(string.Format("接收时间:【{0}】,站点:{1},testid:{2},值:{3}", DateTime.Now.ToString(), station.Name, test.TestId, value * test.Multiple + test.AddNumber));
                                    }
                                    else
                                    {
                                        LogMg.AddDebug(string.Format("接收时间:【{0}】,站点:{1},testid:{2},乘以倍率之后的值:{3}   由于值不在范围内[{4},{5}],丢弃", DateTime.Now.ToString(), station.Name, test.TestId, modbusReturn.value * test.Multiple + test.AddNumber, test.Min, test.Max));
                                    }
                                }
                                else
                                {
                                    //接收数据失败
                                    LogMg.AddDebug(string.Format("接收数据失败"));
                                    //   MessageQueue.Enqueue_DataInfo(string.Format("接收时间:【{0}】,站点:{1},testid:{2}, 接收数据失败", DateTime.Now.ToString(), station.Name, test.TestId));
                                    if (modbusReturn.ErrorMsg.Contains("设备未在线"))
                                    {
                                        return;
                                    }
                                }
                            }
                            catch (SocketException)
                            {
                            }
                            catch (Exception ex)
                            {
                                LogMg.AddError(ex);
                                DEBUG.MsgBox(ex.ToString());
                            }
                            //LogManager.AddDebug("AutoCollectionThread 线程:" + Thread.CurrentThread.ManagedThreadId + " 释放锁了   ");
                        }
                        else
                        {
                            LogMg.AddDebug("AutoCollectionThread 锁失败了");
                        }
                    }
                }
            }
        }