public static Image ConvertBase64StringToImage(string strBase64String) { try { Image img; if (string.IsNullOrEmpty(strBase64String)) { return(null); } byte[] byteImage = Convert.FromBase64String(strBase64String); using (MemoryStream ms = new MemoryStream(byteImage)) { img = Image.FromStream(ms); } return(img); } catch (Exception ex) { LogClass.WriteLog("ConvertBase64StringToImage Exception: " + ex.Message); return(null); } }
public void ComRead(byte[] dat, int rlen, int f) { int Comflag; double zl = 0; Comflag = f; int i = -1; string zlstr = "0"; string aa = ""; if (dat.Length > 0) { for (int p = 0; p < dat.Length; p++) { aa += dat[p].ToString() + ","; } zlstr = System.Text.Encoding.ASCII.GetString(dat, 0, dat.Length); LogClass.WriteLog("weight_log", "称重仪表读数据返回值,串口号:" + port + " zlstr 返回值: " + aa, EFlag.error); string pp = zlstr; // i = zlstr.IndexOf('\u0002'); i = zlstr.IndexOf(Convert.ToChar(2)); if (i >= 0) { zlstr = zlstr.Substring(i, zlstr.Length - i); } if (zlstr.Length > 17) { zlstr = zlstr.Substring(4, 6); if (zlstr.Trim().Length > 0) { //int m = Convert.ToInt32(dat[i + 2]); //int m1 = 4; //int m2 = m & m1; //if (m2 > 0) //{ // LogClass.WriteLog("weight_log", "称重仪表读数据(zlstr=-1)返回值,串口号:" + port + " zlstr 返回值: " + pp + "十六进制" + aa, EFlag.error); // zlstr = "-1"; // //触发事件 // myEvent(zlstr.ToString(), flag); // return; //} try { zl = Convert.ToInt32(zlstr); myEvent(zl.ToString(), flag); } catch (Exception ex) { flag = -13; LogClass.WriteLog("weight_log", "解析数据异常,串口号:" + port + " 错误信息: " + ex.ToString() + "字符串为: " + aa, EFlag.error); // myEvent(" ", flag); } } } } } //读数据
public int openT6 = -1;//0 社保卡是否正常打开 0正常 非0不正常++++++++++ #region 加载事件 private void MainForm_Load(object sender, EventArgs e) { AppContext.Session.openStatus = false; cmd.ShowOpaqueLayer(0f); String deptName = "无"; if (AppContext.Session.deptList.Count > 0) { deptName = AppContext.Session.deptList[0].name; } labBottomLeft.Text = deptName + " | " + AppContext.Session.name + " | " + System.DateTime.Now.ToString(); this.timer1.Start(); tmHeartbeat.Enabled = true; //获取父级为1的菜单 List <MenuEntity> menuList = new List <MenuEntity>(); for (int i = 0; i < AppContext.Session.menuList.Count(); i++) { MenuEntity menu = AppContext.Session.menuList[i]; if (menu.parentId.Equals("1")) { menuList.Add(menu); } } //排序菜单 menuList = menuList.OrderBy(x => x.sort).ToList(); //循环添加菜单 panMenuBar.Controls.Clear(); foreach (MenuEntity menu in menuList) { AddContextMenu(menu.id, menu.name, menu.href, panMenuBar); } cmd.ShowOpaqueLayer(0f, "初始化读卡器..."); this.DoWorkAsync(500, (o) => //耗时逻辑处理(此处不能操作UI控件,因为是在异步中) { //Thread.Sleep(1000); //初始化T6 有线程延迟 openT6 = HardwareInitialClass.OpenDevice(); if (openT6 != 0) { LogClass.WriteLog("社保读卡器初始化失败:"); } else { LogClass.WriteLog("社保读卡器初始化成功"); } return(null); }, null, (data) => //显示结果(此处用于对上面结果的处理,比如显示到界面上) { //显示欢迎页 WelcomeForm form = new WelcomeForm(); AaddUserControl(form, "Welcome", "欢迎页"); cmd.HideOpaqueLayer(); }); //AppContext.Session.waitControl = xtraTabControl1; }
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { var ex = e.ExceptionObject as Exception; if (ex != null) { LogClass.WriteLog("CurrentDomain_UnhandledException:" + ex); } MessageBox.Show("系统出现异常:" + (ex.Message + " " + (ex.InnerException != null && ex.InnerException.Message != null && ex.Message != ex.InnerException.Message ? ex.InnerException.Message : string.Empty))); }
private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e) { var ex = e.Exception; if (ex != null) { LogClass.WriteLog("Application_ThreadException:" + ex); } MessageBox.Show("系统出现异常:" + (ex.Message + " " + (ex.InnerException != null && ex.InnerException.Message != null && ex.Message != ex.InnerException.Message ? ex.InnerException.Message : string.Empty))); }
public static string CallRemote(string url, Dictionary <string, string> paramsList, HttpMethod method) { // System.GC.Collect();//强制回收,回收没有关闭的HTTP请求 MyWebClient wc = new MyWebClient(); if (url.StartsWith("https")) { ServicePointManager.ServerCertificateValidationCallback = ValidateServerCertificate; } if (method == HttpMethod.Get) { foreach (var pa in paramsList) { wc.QueryString.Add(pa.Key, pa.Value); } byte[] byRemoteInfo; string sRemoteInfo = ""; string result = ""; try { byRemoteInfo = wc.DownloadData(url); sRemoteInfo = Encoding.UTF8.GetString(byRemoteInfo); result = sRemoteInfo; } catch (Exception ex) { LogClass.WriteLog("HTTP Get请求错误信息:" + ex.Message); } return(result); } else { NameValueCollection par = new NameValueCollection(); foreach (var pa in paramsList) { par.Add(pa.Key, pa.Value); } byte[] byRemoteInfo; string sRemoteInfo = ""; string result = ""; try { byRemoteInfo = wc.UploadValues(url, "post", par); sRemoteInfo = Encoding.UTF8.GetString(byRemoteInfo); result = sRemoteInfo; } catch (Exception ex) { LogClass.WriteLog("HTTP Post请求错误信息:" + ex.Message); } return(result); } }
public void OpenPort() //打开串口并读数 { int mode; bool bol = false; while (bol == false) { ret = pComm.sio_open(port); if (ret < 0) { LogClass.WriteLog("weight_log", "称重仪表(sio_open)打开串口,串口号:" + port + " 返回值: " + ret, EFlag.error); } if (ret == 0) { bol = true; } System.Threading.Thread.Sleep(100); } while (thread_end) { mode = BaudRate | StopBits | ByteSize; // ret = pComm.sio_flush(port, 0); ret = pComm.sio_ioctl(port, BaudRate, mode); System.Threading.Thread.Sleep(100); int rlen; byte[] buf = new byte[127]; rlen = pComm.sio_read(port, ref buf[0], 127); System.Threading.Thread.Sleep(100); //rlen == 0 ||(20150218) if (ret != 0) { flag = ret; //中断 LogClass.WriteLog("weight_log", "称重仪表读数据(sio_read)返回值,串口号:" + port + " sio_read 返回值: " + rlen + "sio_ioctl返回值: " + ret, EFlag.error); myEvent("0", rlen); } else { LogClass.WriteLog("weight_log", "称重仪表读数据(sio_read)返回值,串口号:" + port + " sio_read 返回值: " + rlen + "sio_ioctl返回值: " + ret, EFlag.error); flag = ret; //正常 } ComRead(buf, rlen, flag); } //时间与波特率有关系 System.Threading.Thread.Sleep(1000); }
/// <summary> /// 修改 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnSave_Click(object sender, EventArgs e) { try { var selectedRow = gv_Message.GetFocusedRow() as MessageInfoEntity; if (selectedRow == null) { return; } groupBox1.Enabled = true; dcMessage.SetValue(selectedRow); } catch (Exception ex) { LogClass.WriteLog("消息模板列表修改错误信息:" + ex.Message); } }
} //读数据 public int clear() //清零 { int a = 0; byte[] b = new byte[1]; b[0] = (byte)(0x5a); try { a = pComm.sio_write(port, ref b[0], b.Length); if (a < 0) { LogClass.WriteLog("weight_log", "称重仪表发清零命令返回值" + port + " " + a, EFlag.error); } } catch (Exception ex) { Console.Write(ex.ToString()); LogClass.WriteLog("weight_log", "称重仪表发清零命令异常" + port + " " + ex.ToString(), EFlag.error); } return(a); }
public bool cexiao(string liushuihao) { StringBuilder tem = new StringBuilder(); tem.Append(liushuihao); int aa = HardwareInitialClass.SaleVoid(tem); if (aa != 0) { HardwareInitialClass.GetErrorMsg(tem); return_msg = tem + ""; LogClass.WriteLog("撤销失败:" + liushuihao); LogClass.WriteLog("撤销失败:" + tem); return(false); } else { return(true); } }
public static string ConvertImageToBase64String(string strImageFile) { try { byte[] byteImg; using (FileStream fs = File.OpenRead(strImageFile)) { byteImg = new byte[fs.Length]; fs.Read(byteImg, 0, byteImg.Length); fs.Close(); } string strRtn = Convert.ToBase64String(byteImg); return(strRtn); } catch (Exception ex) { LogClass.WriteLog("ConvertImageToBase64String Exception: " + ex.Message); return(string.Empty); } }
protected override void OnCalculate() { LogClass.WriteLog("Entro"); double zTemperature1In = InputPort1.Temperature; double zPressure1In = InputPort1.Pressure; double zEnthalpy1In = InputPort1.Enthalpy; LogClass.WriteLog("Dopo Enth"); double zTotalFlow1In = InputPort1.TotalFlow; double[] zFraction1In = InputPort1.Fraction; LogClass.WriteLog("Prima di porta 2"); double zTemperature2In = InputPort2.Temperature; double zPressure2In = InputPort2.Pressure; double zEnthalpy2In = InputPort2.Enthalpy; double zTotalFlow2In = InputPort2.TotalFlow; double[] zFraction2In = InputPort2.Fraction; //double zTemperature1Out; //double zPressure1Out; //double zEnthalpy1Out; //double zTotalFlow1Out; //double[] zFraction1Out; //double zTemperature2Out; //double zPressure2Out; //double zEnthalpy2Out; //double zTotalFlow2Out; //double[] zFraction2Out; double zDuty; LogClass.WriteLog("Temperatura T1= " + zTemperature1In.ToString() + " Temperatura T2= " + zTemperature2In.ToString()); if (zTemperature1In >= zTemperature2In) { OutputPort1.Pressure = zPressure1In; OutputPort1.TotalFlow = zTotalFlow1In; OutputPort1.Fraction = zFraction1In; OutputPort1.Temperature = 250; //OutputPort1.Temperature = InputPort1.Temperature; LogClass.WriteLog("Prima di calc equilibrium porta 1 x"); OutputPort1.CalculateEquilibrium(CapeOpenFlashType.TP); LogClass.WriteLog("DOPO di calc equilibrium porta 1"); LogClass.WriteLog("Enthalpy IN1 = " + zEnthalpy1In.ToString() + "Enthalpy OUT1 = " + OutputPort1.Enthalpy.ToString()); zDuty = zTotalFlow1In * (OutputPort1.Enthalpy - zEnthalpy1In); OutputPort2.Pressure = zPressure2In; OutputPort2.TotalFlow = zTotalFlow2In; OutputPort2.Fraction = zFraction2In; LogClass.WriteLog("Prima di set Enth"); OutputPort2.Enthalpy = zEnthalpy2In + zDuty; LogClass.WriteLog("DOPO di set Enth"); //OutputPort2.Enthalpy = 1199.8816; //OutputPort2.Temperature = 300; //OutputPort2.Temperature = InputPort2.Temperature; //OutputPort2.CalculateProperty(CapeOpenThermoMaterialPropertyType.Volume, CapeOpenPhaseType.Overall); //LogClass.WriteLog("Prima di calc equilibrium porta 2; Enthalpy IN2 = " + zEnthalpy2In.ToString() + " Duty = " + zDuty.ToString() + " EnthaplyOut2 = " + OutputPort2.Enthalpy); //OutputPort2.CalculateProperty(CapeOpenThermoMaterialPropertyType.Temperature, CapeOpenPhaseType.Overall, CapeOpenCalculationType.Nothing); //LogClass.WriteLog("Temperatura = " + OutputPort2.Temperature.ToString()); //string[] zProps = new string[1]; //zProps[0] = "Temperature"; //OutputPort2.CalculateEquilibrium(CapeOpenFlashType.PH,zProps); OutputPort2.CalculateEquilibrium(CapeOpenFlashType.PH); //LogClass.WriteLog("Temperatura = " + OutputPort2.Temperature.ToString() + " EnthaplyOut2 = " + OutputPort2.Enthalpy); //OutputPort2.Enthalpy = 1199.8816; LogClass.WriteLog("Dopo di calc equilibrium porta 2"); } }
/// <summary> /// post模拟表单提交(参数太长的时候用) /// </summary> /// <param name="url"></param> /// <param name="cookie"></param> /// <returns></returns> public static String httpPost(String url, String postDataStr) { string result = ""; HttpWebRequest request = null; HttpWebResponse response = null; Stream stream = null; StreamReader reader = null; try { GC.Collect(); // 请求之前 做一次垃圾回收 ServicePointManager.DefaultConnectionLimit = 20; // 打印 请求地址及参数 LogClass.WriteLog("请求数据:" + url); request = (HttpWebRequest)WebRequest.Create(url); byte[] requestBytes = Encoding.ASCII.GetBytes(postDataStr); //request.Accept = "*/*"; request.Method = "POST"; //request.UserAgent = "Mozilla/5.0"; request.CookieContainer = CookieEntity.cookie; request.KeepAlive = false; // 保持短链接 request.Timeout = 1 * 60 * 1000; // 1分钟,以防网络超时 request.ContentType = "application/x-www-form-urlencoded"; Encoding encoding = Encoding.UTF8; //根据网站的编码自定义 byte[] postData = encoding.GetBytes(postDataStr); //postDataStr即为发送的数据, request.ContentLength = postData.Length; Stream requestStream = request.GetRequestStream(); requestStream.Write(postData, 0, postData.Length); requestStream.Close(); response = (HttpWebResponse)request.GetResponse(); CookieEntity.cookie = request.CookieContainer; stream = response.GetResponseStream(); reader = new StreamReader(stream, Encoding.UTF8); result = reader.ReadToEnd().Trim(); // 打印响应结果 LogClass.WriteLog("响应结果:" + result); //if (result.Equals("远程服务器返回错误: (404) 未找到。")) //{ // throw new Exception(result); //} } catch (Exception e) { result = "{'state': false, 'message':'" + e.Message + "'}"; LogClass.WriteExceptionLog(e); } finally { if (reader != null) { reader.Close(); } if (stream != null) { stream.Close(); } if (response != null) { response.Close(); } if (request != null) { request.Abort(); } } return(result); }
/// <summary> /// 使用Post方法获取字符串结果 /// 主要用于文件上传(可携带表单数据) /// </summary> /// <param name="url">请求地址</param> /// <param name="formItems">Post表单文件、内容数据</param> /// <returns></returns> public static string PostForm(string url, List <FormItemModel> formItems) { string result = ""; HttpWebRequest request = null; HttpWebResponse response = null; Stream stream = null; StreamReader reader = null; try { GC.Collect(); // 请求之前 做一次垃圾回收 ServicePointManager.DefaultConnectionLimit = 20; // 打印 请求地址及参数 LogClass.WriteLog("请求数据:" + url); request = WebRequest.Create(url) as HttpWebRequest; #region 初始化请求对象 request.Accept = "*/*"; request.Method = "POST"; request.UserAgent = "Mozilla/5.0"; request.CookieContainer = CookieEntity.cookie; request.KeepAlive = false; // 保持短链接 request.Timeout = 2 * 60 * 1000; // 2分钟,以防网络超时 或 文件过大 #endregion string boundary = "----" + DateTime.Now.Ticks.ToString("x");//分隔符 request.ContentType = string.Format("multipart/form-data; boundary={0}", boundary); //请求流 var postStream = new MemoryStream(); #region 处理Form表单请求内容 //是否用Form上传文件 var formUploadFile = formItems != null && formItems.Count > 0; if (formUploadFile) { //文件数据模板 string fileFormdataTemplate = "\r\n--" + boundary + "\r\nContent-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"" + "\r\nContent-Type: multipart/form-data" + "\r\n\r\n"; //文本数据模板 string dataFormdataTemplate = "\r\n--" + boundary + "\r\nContent-Disposition: form-data; name=\"{0}\"" + "\r\n\r\n{1}"; foreach (var item in formItems) { string formdata = null; if (item.IsFile) { //上传文件 formdata = string.Format( fileFormdataTemplate, item.Key, //表单键 item.FileName); } else { //上传文本 formdata = string.Format( dataFormdataTemplate, item.Key, item.Value); } //统一处理 byte[] formdataBytes = null; //第一行不需要换行 if (postStream.Length == 0) { formdataBytes = Encoding.UTF8.GetBytes(formdata.Substring(2, formdata.Length - 2)); } else { formdataBytes = Encoding.UTF8.GetBytes(formdata); } postStream.Write(formdataBytes, 0, formdataBytes.Length); //写入文件内容 if (item.FileContent != null && item.FileContent.Length > 0) { using (var streams = item.FileContent) { byte[] buffer = new byte[1024]; int bytesRead = 0; while ((bytesRead = streams.Read(buffer, 0, buffer.Length)) != 0) { postStream.Write(buffer, 0, bytesRead); } } } } //结尾 var footer = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n"); postStream.Write(footer, 0, footer.Length); } else { request.ContentType = "application/json;charset=UTF-8"; } #endregion request.ContentLength = postStream.Length; #region 输入二进制流 if (postStream != null) { postStream.Position = 0; //直接写入流 Stream requestStream = request.GetRequestStream(); byte[] buffer = new byte[1024]; int bytesRead = 0; while ((bytesRead = postStream.Read(buffer, 0, buffer.Length)) != 0) { requestStream.Write(buffer, 0, bytesRead); } postStream.Close();//关闭文件访问 } #endregion response = (HttpWebResponse)request.GetResponse(); CookieEntity.cookie = request.CookieContainer; stream = response.GetResponseStream(); reader = new StreamReader(stream, Encoding.UTF8); result = reader.ReadToEnd().Trim(); // 打印响应结果 LogClass.WriteLog("响应结果:" + result); } catch (Exception e) { result = e.Message; LogClass.WriteExceptionLog(e); } finally { if (reader != null) { reader.Close(); } if (stream != null) { stream.Close(); } if (response != null) { response.Close(); } if (request != null) { request.Abort(); } } return(result);; }
private static void Main() { try { Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); Application.ThreadException += Application_ThreadException; AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; var aProcessName = Process.GetCurrentProcess().ProcessName; if (!System.Configuration.ConfigurationManager.AppSettings.AllKeys.Contains("Interval")) { SetAppSetting("Interval", "80"); } if (!System.Configuration.ConfigurationManager.AppSettings.AllKeys.Contains("Font")) { SetAppSetting("Font", "38"); } if (!System.Configuration.ConfigurationManager.AppSettings.AllKeys.Contains("PageSize")) { SetAppSetting("PageSize", "10"); } if (System.Configuration.ConfigurationManager.AppSettings.AllKeys.Contains("PageSize") && System.Configuration.ConfigurationManager.AppSettings["PageSize"] == "10") { SetAppSetting("PageSize", "8"); } if (!System.Configuration.ConfigurationManager.AppSettings.AllKeys.Contains("RollingTime")) { SetAppSetting("RollingTime", "6"); } if (!System.Configuration.ConfigurationManager.AppSettings.AllKeys.Contains("IsTextDate")) { SetAppSetting("IsTextDate", "false"); } if (!System.Configuration.ConfigurationManager.AppSettings.AllKeys.Contains("IsOpenVioce")) { SetAppSetting("IsOpenVioce", "false"); } if (!System.Configuration.ConfigurationManager.AppSettings.AllKeys.Contains("VoicePlayTime")) { SetAppSetting("VoicePlayTime", "20"); } if (!System.Configuration.ConfigurationManager.AppSettings.AllKeys.Contains("RefreshTimeWaitingDesc")) { SetAppSetting("RefreshTimeWaitingDesc", "60"); } else { if (System.Configuration.ConfigurationManager.AppSettings["RefreshTimeWaitingDesc"] != "60") { SetAppSetting("RefreshTimeWaitingDesc", "60"); } } System.Configuration.ConfigurationManager.RefreshSection("appSettings"); if ((Process.GetProcessesByName(aProcessName)).GetUpperBound(0) > 0) { MessageBox.Show(@"系统已经在运行中,如果要重新启动,请从进程中关闭...", @"系统警告", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } else { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Xr.RtScreen.Models.AppContext.Load(); Xr.Log4net.LogHelper.InitLog(); if (Xr.RtScreen.Models.AppContext.AppConfig.Setting == "1") { Application.Run(new Form1()); } else { Application.Run(new SettingFrm()); } } } catch (Exception ex) { LogClass.WriteLog("Main:" + ex); MessageBox.Show("系统出现异常:" + (ex.Message + " " + (ex.InnerException != null && ex.InnerException.Message != null && ex.Message != ex.InnerException.Message ? ex.InnerException.Message : string.Empty)) + ",请重启程序。"); } }
public bool kougezhang(PosTranClass posTran) { StringBuilder tem = new StringBuilder(); tem.Append(posTran.message_type); tem.Append(posTran.ill_code); tem.Append(posTran.q_amount); tem.Append(posTran.p_amount); tem.Append(posTran.f_amount); tem.Append(posTran.s_amount); tem.Append(posTran.c_amount); tem.Append(posTran.e_amount); tem.Append(posTran.yy_code); tem.Append(posTran.flag); tem.Append(posTran.datetime); LogClass.WriteLog("个帐参数:" + tem.ToString()); int aa = HardwareInitialClass.SendPosInfo(tem); LogClass.WriteLog("回参:" + tem); string[] resultValue = tem.ToString().Split('|'); string result = resultValue[0]; if (result != "30") { posTran = null; StringBuilder error = new StringBuilder(); aa = HardwareInitialClass.GetErrorMsg(error); LogClass.WriteLog("扣个账失败原因:" + error.ToString()); return_msg = "扣个账失败原因:" + error.ToString(); return(false); } else { string temm = "没有流水号"; if (resultValue.Length > 1 && resultValue[1] != null) { temm = resultValue[1]; } posTran.liushuihao = temm; posTran.GRZHKKQYE = sb_amount; posTran.GRZHZF = posTran.s_amount; posTran.GWYTCXEYE = posTran.e_amount; posTran.TSBZKFQYE = sp_amount; posTran.JFLB = posTran.flag; posTran.ZFLJ = temm; posTran.liushuihao = temm; return(true); } //StringBuilder tem = new StringBuilder(); //tem.Append("1"); //tem.Append(" "); //tem.Append("000000000001"); //tem.Append("000000000000"); //tem.Append("000000000001"); //tem.Append("000000000002"); //tem.Append("000000000000"); //tem.Append("000000000000"); //tem.Append(" "); //tem.Append("1"); //tem.Append("20170307152323"); //int aa = HardwareInitialClass.SendPosInfo(tem); //string result = (tem.ToString().Split('|'))[0]; ; //if (result != "30") // posTran = null; }