/// <summary>
        /// TCP客户端以byte数组或16进制格式字符串发送数据
        /// </summary>
        /// <param name="data_origin">待发送数据,byte数组或16进制格式字符串</param>
        /// <param name="errorMessage">返回的错误信息,假如未报错则为空</param>
        /// <returns>返回发送结果</returns>
        public bool SendData(object data_origin, out string errorMessage)
        {
            errorMessage = string.Empty;
            if (!IsConnected || !IsSocketConnected())
            {
                errorMessage = string.Format("TCP服务端{0}未连接", Name);
                if (logging)
                {
                    FileClient.WriteFailureInfo(errorMessage);
                }
                return(false);
            }
            byte[] data;
            string typeName = data_origin.GetType().Name;

            if (typeName == "Byte[]")
            {
                data = (byte[])data_origin;
            }
            else if (typeName == "String")
            {
                data = HexHelper.HexString2Bytes((string)data_origin);
            }
            else
            {
                errorMessage = string.Format("数据格式不正确({0}),无法向TCP服务端{1}发送数据", typeName, Name);
                FileClient.WriteFailureInfo(errorMessage);
                return(false);
            }
            try { NetStream.Write(data, 0, data.Length); }
            catch (Exception ex)
            {
                errorMessage = string.Format("向TCP服务端{0}发送数据失败 {1}", Name, ex.Message);
                FileClient.WriteExceptionInfo(ex, errorMessage, false);
                //MessageBox.Show(selClient.Name + ":" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(false);
            }
            return(true);
        }
Beispiel #2
0
        /// <summary>
        /// UDP客户端以byte数组或16进制格式字符串发送数据
        /// </summary>
        /// <param name="data_origin">待发送数据,byte数组或16进制格式字符串</param>
        /// <param name="endPoint">远程IP终结点</param>
        /// <param name="errorMessage">返回的错误信息,假如未报错则为空</param>
        /// <returns>返回发送结果</returns>
        public bool SendData(object data_origin, IPEndPoint endPoint, out string errorMessage)
        {
            errorMessage = string.Empty;
            //if (!IsConnected || !IsSocketConnected())
            //{
            //    errorMessage = string.Format("UDP服务端{0}未连接", Name);
            //    if (logging)
            //        FileClient.WriteFailureInfo(errorMessage);
            //    return false;
            //}
            byte[] data;
            string typeName = data_origin.GetType().Name;

            if (typeName == "Byte[]")
            {
                data = (byte[])data_origin;
            }
            else if (typeName == "String")
            {
                data = HexHelper.HexString2Bytes((string)data_origin);
            }
            else
            {
                errorMessage = string.Format("数据格式不正确({0}),无法向UDP服务端{1}发送数据", typeName, Name);
                FileClient.WriteFailureInfo(errorMessage);
                return(false);
            }
            try { BaseClient.Send(data, data.Length, endPoint); }
            catch (Exception ex)
            {
                errorMessage = string.Format("向UDP服务端{0}发送数据失败 {1}", Name, ex.Message);
                FileClient.WriteExceptionInfo(ex, errorMessage, false);
                return(false);
            }
            return(true);
        }
Beispiel #3
0
 /// <summary>
 /// 解析MODBUS返回信息
 /// </summary>
 /// <param name="hex">返回的16进制字符串</param>
 public void ResolveCommand(string hex)
 {
     this.ResolveCommand(HexHelper.HexString2Bytes(hex));
 }