// <summary> /// 写收到的数据和回发的数据 /// </summary> /// <param name="strdata">要写的数据</param> public void Writelog(string strdata) { try { //写日志到文件 StreamWriter sw = new StreamWriter(@getFilePath("data_" + System.DateTime.Now.ToString("yyMMddhh") + ".log"), true);//getDateString() sw.Write(strdata); sw.Write("\r\n"); sw.Close(); sw.Dispose(); } catch (Exception e) { try { //写日志到文件 StreamWriter sw = new StreamWriter(@getFilePath("error.log"), true); sw.Write(strdata); sw.Write("\r\n"); sw.Write("Time: {0}", getNowTimeString()); sw.Write("\r\n"); sw.Write(e.ToString()); sw.Write("\r\n"); sw.Close(); sw.Dispose(); } catch { } } } // <summary> /// 写日志 /// </summary> /// <param name="content">日志内容</param> public void RecLog(string content) { try { //写日志到文件 StreamWriter sw = new StreamWriter(@getFilePath("data_" + getDateString() + ".log"), true); sw.Write(string.Format("[{0}] {1}", getNowTimeString(), content));//格式:[时间] 日志内容 sw.Write("\r\n"); sw.Close(); sw.Dispose(); } catch (Exception e) { try { //写日志到文件 StreamWriter sw = new StreamWriter(@getFilePath("error.log"), true); sw.Write(string.Format("[{0}] {1}", getNowTimeString(), content)); sw.Write("\r\n"); sw.Write(string.Format("[{0}] {1}", getNowTimeString(), e.ToString())); sw.Close(); sw.Dispose(); } catch { } } } /// <summary> /// 获取文件完整路径 /// </summary> /// <param name="filename">文件名</param> /// <returns>文件的完整路径</returns> public string getFilePath(string filename) { string path; path = Application.StartupPath + "\\" + filename; return path; } /// <summary> /// 十六进制转成字节 /// </summary> /// <param name="aHex"></param> /// <returns></returns> public string HexToBytes(string aHex) { aHex = aHex.ToUpper(); int len = aHex.Length; string ss = ""; string sss = ""; for (int i = 1; i <= len; i++) { string s = ""; s = aHex.Substring(i - 1, 1); switch (s) { case "0": ss = "0000"; break; case "1": ss = "0001"; break; case "2": ss = "0010"; break; case "3": ss = "0011"; break; case "4": ss = "0100"; break; case "5": ss = "0101"; break; case "6": ss = "0110"; break; case "7": ss = "0111"; break; case "8": ss = "1000"; break; case "9": ss = "1001"; break; case "A": ss = "1010"; break; case "B": ss = "1011"; break; case "C": ss = "1100"; break; case "D": ss = "1101"; break; case "E": ss = "1110"; break; case "F": ss = "1111"; break; default: break; } sss += ss; } return sss; } /// <summary> /// 汉字转国标码 /// </summary> /// <param name="hz"></param> /// <returns></returns> public string Hz2ASC(string hz) { string st = string.Empty; byte[] array = System.Text.Encoding.Default.GetBytes(hz); for (int i = 0; i < array.Length; i++) { st += array[i].ToString("X2"); } return st; } /// <summary> /// 国标码转汉字 /// </summary> /// <param name="asc"></param> /// <returns></returns> public string ASC2Hz(string asc) { string st = string.Empty; byte[] array = new byte[asc.Length / 2]; for (int i = 0; i < array.Length; i++) array[i] = Convert.ToByte(asc.Substring(i * 2, 2).Trim(), 16); for (int i = 0; i < array.Length; i++) { if (array[i] >= 161 && array[i] <= 247) { st = st + System.Text.Encoding.Default.GetString(array, i, 2); i++; } else { st = st + System.Text.Encoding.Default.GetString(array, i, 1); } } return st; } /// <summary> /// 从字符串头位置取指定长度的字符串, /// 并从源字符串删除取出的字符串 /// </summary> /// <param name="str">源字符串</param> /// <param name="getLen">要取的长度</param> /// <returns>返回取出的字符串</returns> public string getString(ref string str, int getLen) { string backStr = ""; try { if ((!str.Equals("")) && (str.Length >= getLen)) { backStr = str.Substring(0, getLen); str = str.Substring(getLen); } } catch { } return backStr; } /// <summary> /// 从字符串右边位置取指定长度的字符串, /// 并从源字符串删除取出的字符串 /// </summary> /// <param name="str"></param> /// <param name="getLen"></param> /// <returns></returns> public string getStringR(ref string str, int getLen) { string backStr = ""; try { if ((!str.Equals("")) && (str.Length >= getLen)) { backStr = str.Substring(str.Length - getLen); str = str.Substring(0, str.Length - getLen); } } catch { } return backStr; } /// <summary> /// 十六进制转换成十进制 /// </summary> /// <param name="hexStr">十六进制字符串</param> /// <returns>十进制数</returns> public int Hex2Int(string hexStr) { int intValue; try { intValue = int.Parse(hexStr, System.Globalization.NumberStyles.HexNumber); } catch { intValue = 0; } return intValue; } /// <summary> /// 字符串(必须是BCD码串)转十六进制 /// </summary> /// <param name="str">字串</param> /// <param name="len">转换后长度,为字符长度</param> /// <returns></returns> public string Str2Hex(string str, int len) { string hex = ""; try { int strInt = Convert.ToInt32(str); hex = strInt.ToString("X" + len); } catch { for (int i = 0; i < len; i++) hex += "0"; } return hex; } /// <summary> /// string类型的ASCII字符转换为string类型的十六进制(如“AB426EPC11”-> “41423432364550433131”) /// 注意是大写的 /// </summary> /// <param name="StringASCII">要转换的ASCII字符</param> /// <returns></returns> public string ASCII2string(string StringASCII) { string Data = StringASCII; int i; int intData; string hexData; string changeData = ""; for (i = 0; i < Data.Length; i++) { string stringData = Data.Substring(i, 1).ToUpper(); char charData = Convert.ToChar(stringData); intData = Convert.ToInt32(charData); hexData = intData.ToString("X"); changeData += hexData; } return changeData; } /// <summary> /// string类型的ASCII字符转换为string类型的十六进制(如“AB426EPC11”-> “41423432364550433131”) /// 注意是小写的 /// </summary> /// <param name="StringASCII">要转换的ASCII字符</param> /// <returns></returns> public string ASCII2stringLower(string StringASCII) { string Data = StringASCII; int i; int intData; string hexData; string changeData = ""; for (i = 0; i < Data.Length; i++) { string stringData = Data.Substring(i, 1).ToLower(); char charData = Convert.ToChar(stringData); intData = Convert.ToInt32(charData); hexData = intData.ToString("X"); changeData += hexData; } return changeData; } /// <summary> /// string类型的ASCII字符转换为string类型的十六进制(如“AB426EPC11”-> “41423432364550433131”) /// 注意是区分大小写的 /// </summary> /// <param name="StringASCII">要转换的ASCII字符</param> /// <returns></returns> public string ASCII2stringCase(string StringASCII) { string Data = StringASCII; int i; int intData; string hexData; string changeData = ""; for (i = 0; i < Data.Length; i++) { string stringData = Data.Substring(i, 1); char charData = Convert.ToChar(stringData); intData = Convert.ToInt32(charData); hexData = intData.ToString("X"); changeData += hexData; } return changeData; } /// <summary> /// string类型的HEX转换为string类型的ASCII字符(如“41423432364550433131”-> “AB426EPC11”) /// </summary> /// <param name="stringhex">要转换的string类型的HEX</param> /// <returns>ASCII字符</returns> public string string2ASCII(string stringhex) { string changeData = ""; try { string hexData = stringhex; int i; char stringData; int intData; for (i = 0; i < hexData.Length; i = i + 2) { string tempData = hexData.Substring(i, 2); intData = Convert.ToInt32(tempData, 16); stringData = (char)intData; changeData += stringData.ToString(); } return changeData; } catch (Exception e) { return changeData = e.Message; } } /// <summary> /// BCD转成固定长度的String /// </summary> /// <param name="str"></param> /// <param name="len"></param> /// <returns></returns> public string BCD2String(string str, int len) { int ll = str.Length; for (int i = 0; i < len - ll; i++) { str = "0" + str; } return str.Substring(0, len); } /// <summary> /// yyyyMMddHHmmss字符串转换成日期时间格式 /// </summary> /// <param name="strTime">日期时间字符串</param> /// <returns>yyyy-MM-dd HH:mm:ss格式字符串</returns> public string Str2Datetime(string strTime) { try { return strTime.Substring(0, 4) + "-" + strTime.Substring(4, 2) + "-" + strTime.Substring(6, 2) + " " + strTime.Substring(8, 2) + ":" + strTime.Substring(10, 2) + ":" + strTime.Substring(12, 2); } catch { return ""; } } /// <summary> /// yyMMddHHmmss字符串转换成日期时间格式 /// </summary> /// <param name="strTime">日期时间字符串</param> /// <returns>yyyy-MM-dd HH:mm:ss格式字符串</returns> public string Str2Datetime1(string strTime) { try { return "20" + strTime.Substring(0, 2) + "-" + strTime.Substring(2, 2) + "-" + strTime.Substring(4, 2) + " " + strTime.Substring(6, 2) + ":" + strTime.Substring(8, 2) + ":" + strTime.Substring(10, 2); } catch { return ""; } } /// <summary> /// yyMMdd字符串转换成日期格式 /// </summary> /// <param name="strDate">日期字符串</param> /// <returns>yyyy-MM-dd格式字符串</returns> public string Str2Date(string strDate) { try { return strDate.Substring(0, 4) + "-" + strDate.Substring(4, 2) + "-" + strDate.Substring(6, 2); } catch { return ""; } } /// <summary> /// HHmmss字符串转换成时间格式 /// </summary> /// <param name="strTime">时间字符串</param> /// <returns>HH:mm:ss格式字符串</returns> public string Str2Time(string strTime) { try { return strTime.Substring(0, 2) + ":" + strTime.Substring(2, 2) + ":" + strTime.Substring(4, 2); } catch { return ""; } } /// <summary> /// 取得日期 /// </summary> /// <returns></returns> public string getDateString() { return System.DateTime.Now.ToString("yyyyMMdd"); } /// <summary> /// 取得当前时间 /// </summary> /// <returns></returns> public string getNowTimeString() { return System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); } /// <summary> /// 显示像十六进制数据 /// </summary> /// <param name="datagram"></param> /// <returns></returns> public string ShowSpace(string datagram) { StringBuilder sb = new StringBuilder(datagram.Length); for (int j = 0; j < (datagram.Length / 2); j++) { sb.Append(String.Format("{0}", datagram.Substring(j * 2, 2))); sb.Append(" "); } return sb.ToString(); } /// <summary> /// DDDFF.FFF,0DDFF.FFF转换成度 /// </summary> /// <param name="coordinate"></param> /// <returns></returns> public decimal Coord2Degree(string coordinate) { //DDDFF.FFF,0DDFF.FFF decimal _decData = 0; decimal d_Degree = 0; decimal d_Minute = 0; try { string s_Degree = coordinate.Substring(0, 3);//取度 string s_Minute = coordinate.Substring(3, 5);//取分 d_Degree = Convert.ToDecimal(s_Degree); d_Minute = Math.Round(Convert.ToDecimal(s_Minute) / 60000, 6); _decData = d_Degree + d_Minute; } catch { _decData = -1; } return _decData; } /// <summary> /// 读取.exe.config的值 /// </summary> /// <param name="path">.exe.config文件的路径</param> /// <param name="appKey">"key"的值</param> /// <returns>返回"value"的值</returns> public string GetConfigValue(string path, string appKey) { XmlDocument xDoc = new XmlDocument(); XmlNode xNode; XmlElement xElem = null; try { xDoc.Load(path); xNode = xDoc.SelectSingleNode("//appSettings"); xElem = (XmlElement)xNode.SelectSingleNode("//add[@key=\"" + appKey + "\"]"); } catch (XmlException ex) { MeBox(ex.Message); } if (xElem != null) return xElem.GetAttribute("value"); else return ""; } /// <summary> /// 设置.exe.config的值 /// </summary> /// <param name="path">.exe.config文件的路径</param> /// <param name="appKey">"key"的值</param> /// <param name="appValue">"value"的值</param> public void SetConfigValue(string path, string appKey, string appValue) { XmlDocument xDoc = new XmlDocument(); try { xDoc.Load(path); XmlNode xNode; XmlElement xElem1; XmlElement xElem2; xNode = xDoc.SelectSingleNode("//appSettings"); xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key=\"" + appKey + "\"]"); if (xElem1 != null) xElem1.SetAttribute("value", appValue); else { xElem2 = xDoc.CreateElement("add"); xElem2.SetAttribute("key", appKey); xElem2.SetAttribute("value", appValue); xNode.AppendChild(xElem2); } xDoc.Save(Application.StartupPath + "\\" + path); } catch (XmlException ex) { MeBox(ex.Message); } } /// <summary> /// 弹出提示框 /// </summary> /// <param name="txt">输入提示信息</param> private void MeBox(string txt) { MessageBox.Show( txt, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly); }