Ejemplo n.º 1
0
        /// <summary>
        /// 单个电表查询线程的执行过程
        /// </summary>
        /// <param name="deviceInfo"></param>
        protected override void InquiryTask(ModbusDeviceInfo deviceInfo)
        {
            TcpSocketCommunicator inquirer = new TcpSocketCommunicator();
            try
            {
                // 与设备模块进行连接(Connect)
                // 设定Receive的接收超时时间为3000毫秒
                AppendUITextBox("	开始连接: " + deviceInfo.DeviceName);
                inquirer.Connect(deviceInfo.HostName, deviceInfo.PortNum, 3000);
                AppendUITextBox("	" + deviceInfo.DeviceName + "连接成功!");
                System.Threading.Thread.Sleep(100);
                // 向设备模块发送查询指令(Modbus协议)
                //  第一个字节是通信地址(设备号)
                //  第二个字节是功能码0x03(读数据)
                //  后面依次是读的起始地址0x0000和读长度0x004E(=78, 读到"正向(吸收)有功电能")
                //  最后两个字节是CRC16校验码
                byte[] tmpBytes = { (byte)deviceInfo.DeviceAddr, 0x03, 0x00, 0x00, 0x00, 0x4E };

                // 计算CRC校验码
                UInt16 crc16 = CRC16(tmpBytes, 6);
                byte crcLowByte = (byte)(crc16 & 0x00FF);
                byte crcHighByte = (byte)((crc16 & 0xFF00) >> 8);

                byte[] sendBytes = { (byte)deviceInfo.DeviceAddr, 0x03, 0x00, 0x00, 0x00, 0x4E, crcLowByte, crcHighByte };

                // 向设备模块发送Modbus读数查询指令
                AppendUITextBox("	查询 " + deviceInfo.DeviceName + " 指令发送!");
                inquirer.Send(sendBytes);

                // 接收设备模块返回的读数查询结果
                ReceiveData rd = inquirer.Receive();
                AppendUITextBox("	接收到 " + deviceInfo.DeviceName + " 应答数据: " + rd.RcvLen.ToString() + " 字节.");
                // 对应答数据进行检查, 返回的第一个字节应该跟设备地址号一致
                if (    (rd.RcvLen >= 1)
                    &&  (deviceInfo.DeviceAddr != rd.RcvBytes[0])    )
                {
                    AppendUITextBox("	" + "收到的应答设备地址不一致: " + rd.RcvBytes[0].ToString());
                    return;
                }

                float fValue;
                string insertStr = GetReportString(rd, deviceInfo, out fValue);
                if (null == insertStr)
                {
                    return;
                }
                AppendUITextBox("	" + deviceInfo.DeviceName + " : 读数值 = "
                    + fValue.ToString() + " + " + deviceInfo.Adjustment.ToString() + " = "
                    + (fValue + deviceInfo.Adjustment).ToString());
                // 上报给服务器
                ReportToDBServer(insertStr, deviceInfo.DeviceName);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex.ToString());
                AppendUITextBox("	" + deviceInfo.DeviceName + " : 查询失败!");
            }
            finally
            {
                inquirer.Close();
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 单个电表查询线程的执行过程
        /// </summary>
        /// <param name="deviceInfo"></param>
        override protected void InquiryTask(ModbusDeviceInfo deviceInfo)
        {
            TcpSocketCommunicator inquirer = new TcpSocketCommunicator();

            try
            {
                // 与设备模块进行连接(Connect)
                // 设定Receive的接收超时时间为3000毫秒
                AppendUITextBox("	开始连接: "+ deviceInfo.DeviceName);
                inquirer.Connect(deviceInfo.HostName, deviceInfo.PortNum, 3000);
                AppendUITextBox("	"+ deviceInfo.DeviceName + "连接成功!");
                System.Threading.Thread.Sleep(100);
                // 向设备模块发送查询指令(Modbus协议)
                //  第一个字节是通信地址(设备号)
                //  第二个字节是功能码0x03(读数据)
                //  后面依次是读的起始地址0x0000和读长度0x004E(=78, 读到"正向(吸收)有功电能")
                //  最后两个字节是CRC16校验码
                byte[] tmpBytes = { (byte)deviceInfo.DeviceAddr, 0x03, 0x00, 0x00, 0x00, 0x4E };

                // 计算CRC校验码
                UInt16 crc16       = CRC16(tmpBytes, 6);
                byte   crcLowByte  = (byte)(crc16 & 0x00FF);
                byte   crcHighByte = (byte)((crc16 & 0xFF00) >> 8);

                byte[] sendBytes = { (byte)deviceInfo.DeviceAddr, 0x03, 0x00, 0x00, 0x00, 0x4E, crcLowByte, crcHighByte };

                // 向设备模块发送Modbus读数查询指令
                AppendUITextBox("	查询 "+ deviceInfo.DeviceName + " 指令发送!");
                inquirer.Send(sendBytes);

                // 接收设备模块返回的读数查询结果
                ReceiveData rd = inquirer.Receive();
                AppendUITextBox("	接收到 "+ deviceInfo.DeviceName + " 应答数据: " + rd.RcvLen.ToString() + " 字节.");
                // 对应答数据进行检查, 返回的第一个字节应该跟设备地址号一致
                if ((rd.RcvLen >= 1) &&
                    (deviceInfo.DeviceAddr != rd.RcvBytes[0]))
                {
                    AppendUITextBox("	"+ "收到的应答设备地址不一致: " + rd.RcvBytes[0].ToString());
                    return;
                }

                float  fValue;
                string insertStr = GetReportString(rd, deviceInfo, out fValue);
                if (null == insertStr)
                {
                    return;
                }
                AppendUITextBox("	"+ deviceInfo.DeviceName + " : 读数值 = "
                                + fValue.ToString() + " + " + deviceInfo.Adjustment.ToString() + " = "
                                + (fValue + deviceInfo.Adjustment).ToString());
                // 上报给服务器
                ReportToDBServer(insertStr, deviceInfo.DeviceName);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex.ToString());
                AppendUITextBox("	"+ deviceInfo.DeviceName + " : 查询失败!");
            }
            finally
            {
                inquirer.Close();
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 单个设备查询线程的执行过程
        /// </summary>
        /// <param name="deviceInfo"></param>
        protected override void InquiryTask(ModbusDeviceInfo deviceInfo)
        {
            string dateTimeStr = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            TcpSocketCommunicator inquirer = new TcpSocketCommunicator();
            try
            {
                // 与设备模块进行连接(Connect)
                // 设定Receive的接收超时时间为3000毫秒
                AppendUITextBox("	开始连接: " + deviceInfo.DeviceName);
                inquirer.Connect(deviceInfo.HostName, deviceInfo.PortNum, 3000);
                AppendUITextBox("	" + deviceInfo.DeviceName + "连接成功!");
                System.Threading.Thread.Sleep(100);

                // 发送查询命令内容
                // 帧头:0x10
                // 总字节数: 0x07
                // 目标地址: 4byte16进制数
                // IO功能编号: 0x0A PT100温度采集
                string deviceAddrStr = deviceInfo.DeviceAddr.ToString().PadLeft(8, '0');
                if (8 != deviceAddrStr.Length)
                {
                    return;
                }
                byte b1 = Convert.ToByte(deviceAddrStr.Substring(0, 2), 16);
                byte b2 = Convert.ToByte(deviceAddrStr.Substring(2, 2), 16);
                byte b3 = Convert.ToByte(deviceAddrStr.Substring(4, 2), 16);
                byte b4 = Convert.ToByte(deviceAddrStr.Substring(6, 2), 16);

                byte[] sendBytes = { 0x10, 0x07, b1, b2, b3, b4, 0x0a };

                // 向设备模块发送读数查询指令
                AppendUITextBox("	查询 " + deviceInfo.DeviceName + " 指令发送!");
                inquirer.Send(sendBytes);

                // 接收设备模块返回的读数查询结果
                ReceiveData ir = inquirer.Receive();
                AppendUITextBox("	接收到 " + deviceInfo.DeviceName + " 应答数据: " + ir.RcvLen.ToString() + " 字节.");
                // 室温返回的结果是字符串, 直接是以小数表示, 所以不用除以量纲
                string outStr = System.Text.Encoding.ASCII.GetString(ir.RcvBytes).Substring(0, ir.RcvLen);
                int idx = -1;
                string temperatureStr = "";
                // 加号表示零上温度,减号表示零下温度
                if (-1 != (idx = outStr.IndexOf('+')))
                {
                    temperatureStr = outStr.Substring(idx).Trim();
                }
                else if (-1 != (idx = outStr.IndexOf('-')))
                {
                    temperatureStr = outStr.Substring(idx).Trim();
                }
                else
                {
                    return;
                }
                float temperatureVal = 0;
                if (!float.TryParse(temperatureStr, out temperatureVal))
                {
                    return;
                }
                // 加上校正值进行校正调整得到最终的温度值
                float fValue = temperatureVal + deviceInfo.Adjustment;

                AppendUITextBox("	" + deviceInfo.DeviceName + " 返回值: " + temperatureStr + " + ("
                                    + deviceInfo.Adjustment.ToString() + ") = " + fValue.ToString());
                // 上报给服务器
                string insertStr = GetReportString(dateTimeStr, fValue, deviceInfo);
                AppendUITextBox("	" + deviceInfo.DeviceName + " : 读数值 = " + fValue.ToString());
                ReportToDBServer(insertStr, deviceInfo.DeviceName);

                // 判断温度值是否在正常区间内
                int alarmType = 0;
                if (0 != (alarmType = IsTemperatureOutOfRange(fValue)))
                {
                    AddTemperatureAlarmRecord(deviceInfo, alarmType, fValue);
                }
            }
            catch (Exception ex)
            {
                AppendUITextBox("	" + deviceInfo.DeviceName + ": 查询失败!");
                System.Diagnostics.Trace.WriteLine(ex.ToString());
            }
            finally
            {
                inquirer.Close();
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 单个设备查询线程的执行过程
        /// </summary>
        /// <param name="deviceInfo"></param>
        override protected void InquiryTask(ModbusDeviceInfo deviceInfo)
        {
            string dateTimeStr             = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            TcpSocketCommunicator inquirer = new TcpSocketCommunicator();

            try
            {
                // 与设备模块进行连接(Connect)
                // 设定Receive的接收超时时间为3000毫秒
                AppendUITextBox("	开始连接: "+ deviceInfo.DeviceName);
                inquirer.Connect(deviceInfo.HostName, deviceInfo.PortNum, 3000);
                AppendUITextBox("	"+ deviceInfo.DeviceName + "连接成功!");
                System.Threading.Thread.Sleep(100);

                // 发送查询命令内容
                // 帧头:0x10
                // 总字节数: 0x07
                // 目标地址: 4byte16进制数
                // IO功能编号: 0x0A PT100温度采集
                string deviceAddrStr = deviceInfo.DeviceAddr.ToString().PadLeft(8, '0');
                if (8 != deviceAddrStr.Length)
                {
                    return;
                }
                byte b1 = Convert.ToByte(deviceAddrStr.Substring(0, 2), 16);
                byte b2 = Convert.ToByte(deviceAddrStr.Substring(2, 2), 16);
                byte b3 = Convert.ToByte(deviceAddrStr.Substring(4, 2), 16);
                byte b4 = Convert.ToByte(deviceAddrStr.Substring(6, 2), 16);

                byte[] sendBytes = { 0x10, 0x07, b1, b2, b3, b4, 0x0a };

                // 向设备模块发送读数查询指令
                AppendUITextBox("	查询 "+ deviceInfo.DeviceName + " 指令发送!");
                inquirer.Send(sendBytes);

                // 接收设备模块返回的读数查询结果
                ReceiveData ir = inquirer.Receive();
                AppendUITextBox("	接收到 "+ deviceInfo.DeviceName + " 应答数据: " + ir.RcvLen.ToString() + " 字节.");
                // 室温返回的结果是字符串, 直接是以小数表示, 所以不用除以量纲
                string outStr         = System.Text.Encoding.ASCII.GetString(ir.RcvBytes).Substring(0, ir.RcvLen);
                int    idx            = -1;
                string temperatureStr = "";
                // 加号表示零上温度,减号表示零下温度
                if (-1 != (idx = outStr.IndexOf('+')))
                {
                    temperatureStr = outStr.Substring(idx).Trim();
                }
                else if (-1 != (idx = outStr.IndexOf('-')))
                {
                    temperatureStr = outStr.Substring(idx).Trim();
                }
                else
                {
                    return;
                }
                float temperatureVal = 0;
                if (!float.TryParse(temperatureStr, out temperatureVal))
                {
                    return;
                }
                // 加上校正值进行校正调整得到最终的温度值
                float fValue = temperatureVal + deviceInfo.Adjustment;

                AppendUITextBox("	"+ deviceInfo.DeviceName + " 返回值: " + temperatureStr + " + ("
                                + deviceInfo.Adjustment.ToString() + ") = " + fValue.ToString());
                // 上报给服务器
                string insertStr = GetReportString(dateTimeStr, fValue, deviceInfo);
                AppendUITextBox("	"+ deviceInfo.DeviceName + " : 读数值 = " + fValue.ToString());
                ReportToDBServer(insertStr, deviceInfo.DeviceName);

                // 判断温度值是否在正常区间内
                int alarmType = 0;
                if (0 != (alarmType = IsTemperatureOutOfRange(fValue)))
                {
                    AddTemperatureAlarmRecord(deviceInfo, alarmType, fValue);
                }
            }
            catch (Exception ex)
            {
                AppendUITextBox("	"+ deviceInfo.DeviceName + ": 查询失败!");
                System.Diagnostics.Trace.WriteLine(ex.ToString());
            }
            finally
            {
                inquirer.Close();
            }
        }