private void button2_Click(object sender, EventArgs e) { string session = JsonWork.login(); if (session == "") { return; } WFHttpWebResponse web = new WFHttpWebResponse(); IDictionary <string, string> p = new Dictionary <string, string>(); p.Add("session", session); p.Add("device_id", DeviceID.ToString()); HttpWebResponse hr = web.CreatePostHttpResponse(JsonWork.strUrl + "get_device_list", p); JsonAddrListReturn AddrList = JsonWork.GetJsonObject <JsonAddrListReturn>(web.Content); if (AddrList.code != "200") { MessageBox.Show("获取数据失败:" + AddrList.message); return; } enBT(false); // ExcelImport el = new ExcelImport(true, "安装位置"); // DataTable dt = el.ExcelImportProc_OleDB(); // if (dt == null || dt.Rows.Count < 100 || dt.Columns.Count < 2) // { // MessageBox.Show("地址数据文件格式有问题,请检查!"); // return; // } byte[] tx; byte[] temp; byte[] temp1; progressBar1.Maximum = AddrList.obj.list.Count; progressBar1.Value = 0; byte id; for (int i = 0; i < AddrList.obj.list.Count; i++) { Debug.WriteLine(AddrList.obj.list[i].id + "," + AddrList.obj.list[i].name); temp = Encoding.Default.GetBytes(AddrList.obj.list[i].name); if (!byte.TryParse(AddrList.obj.list[i].id, out id)) { continue; } if (id == 0) { continue; } id--; if (temp.Length > 20) { temp1 = new byte[23]; temp1[0] = id; temp1[1] = 21; temp1[22] = 0; for (int j = 0; j < 20; j++) { temp1[j + 2] = temp[j]; } tx = CP1616Packet.MakeCP1616Packet(3, 1, temp1); } else { temp1 = new byte[3 + temp.Length]; temp1[0] = id; temp1[1] = (byte)(temp.Length + 1); temp1[2 + temp.Length] = 0; for (int j = 0; j < temp.Length; j++) { temp1[j + 2] = temp[j]; } tx = CP1616Packet.MakeCP1616Packet(3, 1, temp1); } int retry = 20; Rx1616 = new CP1616Packet(3, 1); bool bOK = false; while (retry != 0) { WaitMS(100); try { COMPort1.Write(tx, 0, tx.Length); } catch { retry--; continue; } while (true) { try { if (Rx1616.DataPacketed((byte)COMPort1.ReadByte())) { bOK = true; break; } } catch { break; } } if (bOK) { progressBar1.Value++; break; } retry--; } if (retry == 0) { MessageBox.Show("第" + (i + 1).ToString() + "个传感器地址设置出错!"); break; } } MessageBox.Show("地址数据导入完成!"); enBT(true); }
private void button1_Click_1(object sender, EventArgs e) { if (radioButton1.Checked)//当前数据 { Rx1616 = new CP1616Packet(4, 1); byte[] tx = CP1616Packet.MakeCP1616Packet(4, 1, 1); COMPort1.Write(tx, 0, tx.Length); while (true) { try { if (Rx1616.DataPacketed((byte)COMPort1.ReadByte())) { string session = JsonWork.login(); if (session == "") { return; } JsonTempDataList jsonTempDataList = new JsonTempDataList(); jsonTempDataList.list = new List <JsonTempData>(); for (int i = 0; i < 100; i++) { JsonTempData jtd = new JsonTempData(); jtd.cid = i.ToString(); if (Rx1616.Data[i * 27 + 21] == 0xff) { continue; } else { jtd.date = Rx1616.Data[i * 27 + 21].ToString("D02") + "-" + Rx1616.Data[i * 27 + 22].ToString("D02") + " " + Rx1616.Data[i * 27 + 23].ToString("D02") + ":" + Rx1616.Data[i * 27 + 24].ToString("D02"); jtd.temperature = ((BytesOP.MakeShort(Rx1616.Data[i * 27 + 26], Rx1616.Data[i * 27 + 25])) * 0.0625 + 0.5).ToString("f1"); } jsonTempDataList.list.Add(jtd); } WFHttpWebResponse web = new WFHttpWebResponse(); IDictionary <string, string> p = new Dictionary <string, string>(); p.Add("temperature_list", JsonWork.GetJsonString <JsonTempDataList>(jsonTempDataList)); p.Add("session", session); p.Add("device_id", Form1.DeviceID.ToString()); HttpWebResponse hr = web.CreatePostHttpResponse(JsonWork.strUrl + "post_temperature_list", p); JsonAddrListReturn AddrList = JsonWork.GetJsonObject <JsonAddrListReturn>(web.Content); if (AddrList.code != "200") { MessageBox.Show("数据导出失败:" + AddrList.message); return; } else { MessageBox.Show("数据导出成功"); return; } break; } } catch { MessageBox.Show("数据导出失败"); break; } } } else if (radioButton2.Checked) { hl = new List <byte[]>(); se = new List <byte>(); er = new List <byte>(); progressBar1.Value = 0; int retry; bool bOK; for (int i = 0; i < 100; i++) { retry = 20; bOK = false; while (retry != 0) { Rx1616 = new CP1616Packet(5, 1); byte[] tx = CP1616Packet.MakeCP1616Packet(5, 1, (byte)i); try { COMPort1.Write(tx, 0, tx.Length); } catch { retry--; continue; } while (true) { try { if (Rx1616.DataPacketed((byte)COMPort1.ReadByte())) { // byte[] temp = new byte[Rx1616.Data.Length - 2]; // for (int j = 0; j < Rx1616.Data.Length - 2; j++) // { // temp[j] // } hl.Add(Rx1616.Data); se.Add((byte)(i + 1)); progressBar1.Value++; Thread.Sleep(100); bOK = true; break; } } catch { break; } } if (bOK) { break; } retry--; Thread.Sleep(1000); } if (retry == 0) { er.Add((byte)(i + 1)); } } if (se.Count != 0) { if (er.Count != 0) { string str = "如下传感器数据读取失败:\r\n"; for (int i = 0; i < er.Count; i++) { str += "第" + er[i].ToString() + "号传感器" + "\r\n"; } str += "是否继续导出"; if (MessageBox.Show(str, "读取错误", MessageBoxButtons.YesNo) == DialogResult.No) { return; } } string session = JsonWork.login(); if (session == "") { return; } JsonTempDataList jsonTempDataList = new JsonTempDataList(); jsonTempDataList.list = new List <JsonTempData>(); for (int i = 0; i < se.Count; i++) { int ix1 = 21; while ((ix1 + 6) < hl[i].Length) { JsonTempData jtd = new JsonTempData(); jtd.cid = se[i].ToString(); jtd.date = hl[i][ix1].ToString("D02") + "-" + hl[i][ix1 + 1].ToString("D02") + " " + hl[i][ix1 + 2].ToString("D02") + ":" + hl[i][ix1 + 3].ToString("D02"); ushort t = BytesOP.MakeShort(hl[i][ix1 + 5], hl[i][ix1 + 4]); if (t == 0xffff) { jtd.temperature = "-100"; } else { jtd.temperature = ((t) * 0.0625 + 0.5).ToString("f1"); } jsonTempDataList.list.Add(jtd); ix1 += 6; } } WFHttpWebResponse web = new WFHttpWebResponse(); IDictionary <string, string> p = new Dictionary <string, string>(); p.Add("temperature_list", JsonWork.GetJsonString <JsonTempDataList>(jsonTempDataList)); p.Add("session", session); p.Add("device_id", Form1.DeviceID.ToString()); HttpWebResponse hr = web.CreatePostHttpResponse(JsonWork.strUrl + "post_temperature_list", p); JsonAddrListReturn AddrList = JsonWork.GetJsonObject <JsonAddrListReturn>(web.Content); if (AddrList.code != "200") { MessageBox.Show("数据导出失败:" + AddrList.message); return; } else { MessageBox.Show("数据导出成功"); return; } // ee = new ExcelExport(); // ee.SheetCount = se.Count; // ee.ExcelWorkbookCallbackProc = new ExcelExport.ExcelWorkbookCallback(ExcelWorkbookCallbackProc1); // ee.ExcelExportProc1(); } else { MessageBox.Show("读取历史数据失败"); } } else if (radioButton3.Checked) { if (numericUpDown2.Value < numericUpDown1.Value) { MessageBox.Show("范围有误"); return; } hl = new List <byte[]>(); se = new List <byte>(); er = new List <byte>(); progressBar1.Value = 0; int per = (int)(numericUpDown2.Value - numericUpDown1.Value + 1); per = 100 / per; int retry; bool bOK; for (int i = (int)(numericUpDown1.Value - 1); i < (int)numericUpDown2.Value; i++) { retry = 20; bOK = false; while (retry != 0) { Rx1616 = new CP1616Packet(5, 1); byte[] tx = CP1616Packet.MakeCP1616Packet(5, 1, (byte)i); try { COMPort1.Write(tx, 0, tx.Length); } catch { retry--; continue; } while (true) { try { if (Rx1616.DataPacketed((byte)COMPort1.ReadByte())) { // byte[] temp = new byte[Rx1616.Data.Length - 2]; // for (int j = 0; j < Rx1616.Data.Length - 2; j++) // { // temp[j] // } hl.Add(Rx1616.Data); se.Add((byte)(i + 1)); progressBar1.Value += per; Thread.Sleep(100); bOK = true; break; } } catch { break; } } if (bOK) { break; } retry--; Thread.Sleep(1000); } if (retry == 0) { er.Add((byte)(i + 1)); } } progressBar1.Value = 100; if (se.Count != 0) { if (er.Count != 0) { string str = "如下传感器数据读取失败:\r\n"; for (int i = 0; i < er.Count; i++) { str += "第" + er[i].ToString() + "号传感器" + "\r\n"; } str += "是否继续导出"; if (MessageBox.Show(str, "读取错误", MessageBoxButtons.YesNo) == DialogResult.No) { return; } } string session = JsonWork.login(); if (session == "") { return; } JsonTempDataList jsonTempDataList = new JsonTempDataList(); jsonTempDataList.list = new List <JsonTempData>(); for (int i = 0; i < se.Count; i++) { int ix1 = 21; while ((ix1 + 6) < hl[i].Length) { JsonTempData jtd = new JsonTempData(); jtd.cid = se[i].ToString(); jtd.date = hl[i][ix1].ToString("D02") + "-" + hl[i][ix1 + 1].ToString("D02") + " " + hl[i][ix1 + 2].ToString("D02") + ":" + hl[i][ix1 + 3].ToString("D02"); ushort t = BytesOP.MakeShort(hl[i][ix1 + 5], hl[i][ix1 + 4]); if (t == 0xffff) { jtd.temperature = "-100"; } else { jtd.temperature = ((t) * 0.0625 + 0.5).ToString("f1"); } jsonTempDataList.list.Add(jtd); ix1 += 6; } } WFHttpWebResponse web = new WFHttpWebResponse(); IDictionary <string, string> p = new Dictionary <string, string>(); p.Add("temperature_list", JsonWork.GetJsonString <JsonTempDataList>(jsonTempDataList)); p.Add("session", session); p.Add("device_id", Form1.DeviceID.ToString()); HttpWebResponse hr = web.CreatePostHttpResponse(JsonWork.strUrl + "post_temperature_list", p); JsonAddrListReturn AddrList = JsonWork.GetJsonObject <JsonAddrListReturn>(web.Content); if (AddrList.code != "200") { MessageBox.Show("数据导出失败:" + AddrList.message); return; } else { MessageBox.Show("数据导出成功"); return; } // ee = new ExcelExport(); // ee.SheetCount = se.Count; // ee.ExcelWorkbookCallbackProc = new ExcelExport.ExcelWorkbookCallback(ExcelWorkbookCallbackProc1); // ee.ExcelExportProc1(); } else { MessageBox.Show("读取历史数据失败"); } } }