using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Configuration; using System.Threading; namespace ControlSystem { /// /// 所有进制之间的转换函数库 /// Creator:Richard.liu /// public static class CCarryConvert { /// /// 十进制转二进制 /// /// 十进制数 /// 二进制字符串 //public static string DecimalToBin(decimal dec) //{ // return Convert.ToString(dec, 2); //} /// /// 十进制转十六进制 /// /// 十进制数 /// 十六进制字符串 public static string DecimalToHex(byte dec) { return Convert.ToString(dec, 16); } /// /// 十进制转十六进制 /// /// /// public static string DecimalToHex(int dec) { return Convert.ToString(dec, 16); } /// /// 十进制转八进制 /// /// 十进制数 /// 八进制字符串 public static string DecimalToOct(byte dec) { return Convert.ToString(dec, 8); } /// /// 二进制转十进制 /// /// 二进制字符串 /// 十进制数 public static Int32 BinToDecimal(string bin) { return Convert.ToInt32(bin, 2); } /// /// 八进制转十进制 /// /// 八进制字符串 /// 十进制数 public static Int32 OctToDecimal(string Oct) { return Convert.ToInt32(Oct , 8); } /// /// 十六进制转十进制 /// /// 十六进制字符串 /// 十进制数 public static Int32 HexToDecimal(string hex) { return Convert.ToInt32(hex , 16); } /// /// 十六进制转字节型十进制 /// /// 十六进制字符串 /// 字节型十进制数 public static byte HexToByte(string hex) { return Convert.ToByte(hex, 16); } /// /// 在十进制数获得指定bit位的二进制字符:0/1 /// /// 十进制数 /// 比特位 /// public static byte GetBitFromInteger(int Int, int bitno) { if ((Int & Convert.ToInt32(Math.Pow(2, Convert.ToDouble(bitno)))) == Convert.ToInt32(Math.Pow(2, Convert.ToDouble(bitno)))) { return 1; } else { return 0; } } /// /// byte[0]存放高八位,byte[1]存放低八位 /// /// /// public static byte[] GetByteFromInt16(int _int16) { char[] cc = new char[1] { '.' }; string[] sp; byte[] _bytes = new byte[2]; sp = Convert.ToString(_int16 >> 8).Split(cc); _bytes[0] =Convert.ToByte( sp[0]); _bytes[1] =Convert.ToByte( _int16 & 255); return _bytes; } public static UInt16 GetInt16FromBytes(byte[] _bytes) { UInt16 _int16; _int16 =Convert.ToUInt16( _bytes[1] +( _bytes[0]*256)); return _int16; } /// /// 读取某一天的通讯日志,就是所谓的黑匣子 /// /// 指定的日期 /// 这一天的所有通讯记录 public static string ReadCommLog(string mydate) { string filePath = System.IO.Path.GetFullPath(System.Reflection.Assembly.GetExecutingAssembly().GetName().ToString()); filePath = System.IO.Path.GetDirectoryName(filePath); filePath = System.IO.Path.Combine(filePath, mydate+".log"); StreamReader sr = new StreamReader(filePath); if (sr.EndOfStream == false) { return sr.ReadToEnd(); } else { return ""; } } /// /// 写黑匣子 /// /// /// /// public static bool WriteCommLog(string mydate, string textline) { try { string filePath = System.IO.Path.GetFullPath(System.Reflection.Assembly.GetExecutingAssembly().GetName().ToString()); filePath = System.IO.Path.GetDirectoryName(filePath); filePath = System.IO.Path.Combine(filePath, "DarkCasket"); if (Directory.Exists(filePath) == false) { Directory.CreateDirectory(filePath); } //1--99循环 string strf = string.Empty; string filepath1 = string.Empty; for (int fcount = 1; fcount <= 99; fcount++) { if (fcount.ToString().Length < 2) { strf = "0" + fcount.ToString(); } else { strf = fcount.ToString(); } filepath1 = System.IO.Path.Combine(filePath, mydate + strf + ".log"); if (File.Exists(filepath1) == false) { using (StreamWriter sw = File.CreateText(filepath1)) { sw.WriteLine("通讯时间**********接口类型**********命令**********地址/端口**********报文内容"); sw.WriteLine(textline); //20091102 sw.Close(); sw.Dispose(); }// return true;//20081217 } else { FileInfo ff = new FileInfo(filepath1); AppSettingsReader asr = new AppSettingsReader(); long len = (long)asr.GetValue("DarkCasketSize", typeof(long)); if (ff.Length > len) { continue; } else { using (StreamWriter sw = File.AppendText(filepath1)) { sw.WriteLine(textline); //20091102 sw.Close(); sw.Dispose(); } return true;//20081217 } } } //20081217 //99个文件都写满了,删除今天的99个文件重新开始 for (int fcount = 1; fcount <= 99; fcount++) { if (fcount.ToString().Length < 2) { strf = "0" + fcount.ToString(); } else { strf = fcount.ToString(); } filepath1 = System.IO.Path.Combine(filePath, mydate + strf + ".log"); if (File.Exists(filepath1) == true) { File.Delete(filepath1); } } filepath1 = System.IO.Path.Combine(filePath, mydate + "01.log"); { using (StreamWriter sw = File.CreateText(filepath1)) { sw.WriteLine("通讯时间**********接口类型**********命令**********地址/端口**********报文内容"); sw.WriteLine(textline); //20091102 sw.Close(); sw.Dispose(); } } return true; } catch //(Exception ex) { return false; //throw ex; } } public static bool WriteCommLog(string DarkCasketFolder, string mydate, string textline) { try { string filePath = System.IO.Path.GetFullPath(System.Reflection.Assembly.GetExecutingAssembly().GetName().ToString()); filePath = System.IO.Path.GetDirectoryName(filePath); filePath = System.IO.Path.Combine(filePath, DarkCasketFolder+"DarkCasket"); if (Directory.Exists(filePath) == false) { Directory.CreateDirectory(filePath); } //1--99循环 string strf = string.Empty; string filepath1 = string.Empty; for (int fcount = 1; fcount <= 99; fcount++) { if (fcount.ToString().Length < 2) { strf = "0" + fcount.ToString(); } else { strf = fcount.ToString(); } filepath1 = System.IO.Path.Combine(filePath, mydate + strf + ".log"); if (File.Exists(filepath1) == false) { using (StreamWriter sw = File.CreateText(filepath1)) { sw.WriteLine("通讯时间**********接口类型**********命令**********地址/端口**********报文内容"); sw.WriteLine(textline); //20091102 sw.Close(); sw.Dispose(); }// return true;//20081217 } else { FileInfo ff = new FileInfo(filepath1); AppSettingsReader asr = new AppSettingsReader(); long len = (long)asr.GetValue("DarkCasketSize", typeof(long)); if (ff.Length > len) { continue; } else { using (StreamWriter sw = File.AppendText(filepath1)) { sw.WriteLine(textline); //20091102 sw.Close(); sw.Dispose(); } return true;//20081217 } } } //20081217 //99个文件都写满了,删除今天的99个文件重新开始 for (int fcount = 1; fcount <= 99; fcount++) { if (fcount.ToString().Length < 2) { strf = "0" + fcount.ToString(); } else { strf = fcount.ToString(); } filepath1 = System.IO.Path.Combine(filePath, mydate + strf + ".log"); if (File.Exists(filepath1) == true) { File.Delete(filepath1); } } filepath1 = System.IO.Path.Combine(filePath, mydate + "01.log"); { using (StreamWriter sw = File.CreateText(filepath1)) { sw.WriteLine("通讯时间**********接口类型**********命令**********地址/端口**********报文内容"); sw.WriteLine(textline); //20091102 sw.Close(); sw.Dispose(); } } return true; } catch //(Exception ex) { return false; //throw ex; } } /// /// 写黑匣子 /// /// /// /// /// public static void WriteDarkCasket(string interfacename,string ordername,string addr_port, byte[] message) { string data="", wtxt; if (message == null) { data = ""; } else { for (int i = 0; i < message.GetLength(0); i++) { data = data + "-"+ message[i].ToString(); } } wtxt = DateTime.Now.ToLongTimeString() + "**" + interfacename + "**" + ordername + "**" + addr_port + "**" + data; CCarryConvert.WriteCommLog(DateTime.Today.ToShortDateString(), wtxt); } public static void WriteDarkCasket(string DarkCasketFolder, string interfacename, string ordername, string addr_port, byte[] message) { string data = "", wtxt; if (message == null) { data = ""; } else { for (int i = 0; i < message.GetLength(0); i++) { data = data + "-" + message[i].ToString(); } } wtxt = DateTime.Now.ToLongTimeString() + "**" + interfacename + "**" + ordername + "**" + addr_port + "**" + data; CCarryConvert.WriteCommLog(DarkCasketFolder,DateTime.Today.ToShortDateString(), wtxt); } public static void WriteDarkCasket(string DarkCasketFolder, string interfacename, string ordername, string addr_port, ushort[] message) { string data = "", wtxt; if (message == null) { data = ""; } else { for (int i = 0; i < message.GetLength(0); i++) { data = data + "-" + message[i].ToString(); } } wtxt = DateTime.Now.ToLongTimeString() + "**" + interfacename + "**" + ordername + "**" + addr_port + "**" + data; CCarryConvert.WriteCommLog(DarkCasketFolder, DateTime.Today.ToShortDateString(), wtxt); } /// /// 写黑匣子 /// /// /// /// /// public static void WriteDarkCasket(string interfacename, string ordername, string addr_port, string message) { string wtxt; wtxt = DateTime.Now.ToLongTimeString() + "**" + interfacename + "**" + ordername + "**" + addr_port + "**" + message ; CCarryConvert.WriteCommLog(DateTime.Today.ToShortDateString(), wtxt); } public static void WriteDarkCasket(string DarkCasketFolder, string interfacename, string ordername, string addr_port, string message) { string wtxt; wtxt = DateTime.Now.ToLongTimeString() + "**" + interfacename + "**" + ordername + "**" + addr_port + "**" + message; CCarryConvert.WriteCommLog(DarkCasketFolder,DateTime.Today.ToShortDateString(), wtxt); } /// /// 执行整理黑匣子文件;在app.config文件中找到整理周期 /// public static void NeatenDarkCasket() { try { string filePath; string[] files; filePath = System.IO.Path.GetFullPath(System.Reflection.Assembly.GetExecutingAssembly().GetName().ToString()); filePath = System.IO.Path.GetDirectoryName(filePath); filePath = System.IO.Path.Combine(filePath, "DarkCasket"); AppSettingsReader asr = new AppSettingsReader(); long days = (long)asr.GetValue("DarkCasketDays", typeof(long)); string mydate, path1,pathnew; if (Directory.Exists(filePath) == false) return; files = Directory.GetFiles(filePath); int fc = files.GetLength(0); string strf = string.Empty; if (fc > (days*99)) { //20090910 for (long i = (days); i <= 100; i++) { path1 = filePath; mydate = DateTime.Today.AddDays(-i).ToShortDateString(); for (int fcount = 1; fcount <= 99; fcount++) { if (fcount.ToString().Length < 2) { strf = "0" + fcount.ToString(); } else { strf = fcount.ToString(); } pathnew = System.IO.Path.Combine(path1, mydate + strf + ".log"); if (File.Exists(pathnew) == true) { File.Delete(pathnew); } } } } } catch(Exception ex) { throw ex; } } } }