Пример #1
0
        /// <summary>
        /// 获取数据行的设备目标位置参数
        /// 记录包含字段【访问方式】(TCP/UDP/SerialPort)、【端口】(60000/COM1)、【地址】(192.168.1.146)
        /// </summary>
        /// <param name="dr">数据记录</param>
        /// <returns></returns>
        private CommiTarget getTarget(DataRow dr)
        {
            if (null == dr || (DBNull.Value == dr["串口"] && DBNull.Value == dr["IP地址"]))
                return null;
            CommiTarget target = new CommiTarget();
            CommiType commiType = CommiType.UDP;
            string stype = Convert.ToString(dr["通讯类别"]);
            switch (stype)
            {
                case "TCP/IP(局域网)":
                    commiType = CommiType.TCP; break;
                case "UDP/IP(局域网)":
                    commiType = CommiType.UDP; break;
                default:
                    commiType = CommiType.SerialPort; break;
            }
            try
            {
                if (CommiType.SerialPort == commiType)
                {
                    string portname = Convert.ToString(dr["串口"]);
                    int baudRate = Convert.ToInt16(dr["波特率"]);
                    int dataBits = Convert.ToInt16(dr["数据位"]);
                    decimal s = Convert.ToDecimal(dr["停止位"]);
                    StopBits sb = StopBits.None;
                    if (1 == s) sb = StopBits.One;
                    else if (2 == s) sb = StopBits.Two;
                    else if (1 < s && s < 2) sb = StopBits.OnePointFive;

                    target.SetProtocolParam(portname, baudRate, Parity.None, dataBits, sb);
                }
                else
                {
                    string addr = Convert.ToString(dr["IP地址"]);
                    if (string.IsNullOrEmpty(addr) || DBNull.Value == dr["端口"])
                        return null;
                    int port = Convert.ToInt32(dr["端口"]);
                    target.SetProtocolParam(addr, port, commiType);
                }
            }
            catch (Exception ex)
            {
                NameValueCollection data = new NameValueCollection();
                data["操作"] = "创建通讯目标";
                data["设备ID"] = Convert.ToString(dr["ID"]);
                ServiceTool.LogMessage(ex, data, EventLogEntryType.Error);
                return null;
            }
            return target;
        }
Пример #2
0
 /// <summary>
 /// 获取数据行的设备目标位置参数
 /// 记录包含字段【访问方式】(TCP/UDP/SerialPort)、【端口】(60000/COM1)、【地址】(192.168.1.146)
 /// </summary>
 /// <param name="dr">数据记录</param>
 /// <returns></returns>
 private CommiTarget getTarget(DataRow dr)
 {
     if (null == dr) return null;
     CommiTarget target = new CommiTarget();
     CommiType commiType = CommiType.UDP;
     int typecommi = 0;
     if (dr["通讯类别"].ToString() == "485")
     {
         typecommi = 1;
     }
     else if (dr["通讯类别"].ToString() == @"TCP\IP")
     {
         typecommi = 2;
     }
     else
     {
         typecommi = 3;
     }
     if (DBNull.Value != dr["通讯类别"])
     {
         //int typecommi = Convert.ToInt16(dr["通讯类别"]);
         if (1 == typecommi) commiType = CommiType.SerialPort;
         if (2 == typecommi) commiType = CommiType.TCP;
     }
     if (CommiType.SerialPort == commiType)
     {
         string portname = "COM" + Convert.ToString(dr["端口"]);
         target.SetProtocolParam(portname, 19200);
     }
     else
     {
         string addr = Convert.ToString(dr["站址"]);
         int port = Convert.ToInt16(dr["端口"]);
         target.SetProtocolParam(addr, port, commiType);
     }
     target.setProtocol(Protocol.PTLPark);
     return target;
 }
Пример #3
0
        /// <summary>
        /// ��ȡ�����е��豸Ŀ��λ�ò���
        /// ��¼�����ֶΡ����ʷ�ʽ��(TCP/UDP/SerialPort)�����˿ڡ�(60000/COM1)������ַ��(192.168.1.146)
        /// </summary>
        /// <param name="dr">���ݼ�¼</param>
        /// <returns></returns>
        private CommiTarget getTarget(string[] devps)
        {
            string[] cols ={ "ͨѶ���", "����", "������", "����λ", "ֹͣλ", "IP��ַ", "�˿�", "վַ", "����" };

            if (null == devps || devps.Length < 7)
                return null;
            CommiTarget target = new CommiTarget();
            target.setProtocol(Protocol.PTLEatery);
            CommiType commiType = CommiType.UDP;
            string stype = Convert.ToString(devps[0]);
            switch (stype)
            {
                case "TCP/IP(������)":
                    commiType = CommiType.TCP; break;
                case "UDP/IP(������)":
                    commiType = CommiType.UDP; break;
                default:
                    commiType = CommiType.SerialPort; break;
            }
            try
            {
                if (CommiType.SerialPort == commiType)
                {
                    string portname = Convert.ToString(devps[1]);
                    int baudRate = Convert.ToInt16(devps[2]);
                    int dataBits = Convert.ToInt16(devps[3]);
                    decimal s = Convert.ToDecimal(devps[4]);
                    StopBits sb = StopBits.None;
                    if (1 == s) sb = StopBits.One;
                    else if (2 == s) sb = StopBits.Two;
                    else if (1 < s && s < 2) sb = StopBits.OnePointFive;
                    target.SetProtocolParam(portname, baudRate, Parity.None, dataBits, sb);
                }
                else
                {
                    string addr = Convert.ToString(devps[5]);
                    int port = Convert.ToInt32(devps[6]);
                    target.SetProtocolParam(addr, port, commiType);
                }
            }
            catch (Exception ex)
            {
                return null;
            }
            return target;
        }
Пример #4
0
 /// <summary>
 /// 获取数据行的设备目标位置参数
 /// 记录包含字段【访问方式】(TCP/UDP/SerialPort)、【端口】(60000/COM1)、【地址】(192.168.1.146)
 /// </summary>
 /// <param name="dr">数据记录</param>
 /// <returns></returns>
 private CommiTarget getTarget(DataGridViewCellCollection cells)
 {
     if (null == cells) return null;
     CommiTarget target = new CommiTarget();
     CommiType commiType = CommiType.UDP;
     string stype = Convert.ToString(cells["通讯类别"].Value);
     switch (stype)
     {
         case "TCP/IP(局域网)":
             commiType = CommiType.TCP; break;
         case "UDP/IP(局域网)":
             commiType = CommiType.UDP; break;
         default:
             commiType = CommiType.SerialPort; break;
     }
     try
     {
         if (CommiType.SerialPort == commiType)
         {
             string portname = Convert.ToString(cells["串口"].Value);
             int baudRate = Convert.ToInt16(cells["波特率"].Value);
             int dataBits = Convert.ToInt16(cells["数据位"].Value);
             decimal s = Convert.ToDecimal(cells["停止位"].Value);
             StopBits sb = StopBits.None;
             if (1 == s) sb = StopBits.One;
             else if (2 == s) sb = StopBits.Two;
             else if (1 < s && s < 2) sb = StopBits.OnePointFive;
             target.SetProtocolParam(portname, baudRate, Parity.None, dataBits, sb);
         }
         else
         {
             string addr = Convert.ToString(cells["IP地址"].Value);
             int port = Convert.ToInt32(cells["端口"].Value);
             target.SetProtocolParam(addr, port, commiType);
         }
         target.setProtocol(Protocol.PTLPark);
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message, "通讯设备提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
         return null;
     }
     return target;
 }