private void Uart_DataReceived(object sender, SerialDataReceivedEventArgs e) { int irx; byte rx; while (true) { try { irx = Uart.ReadByte(); } catch { return; } if (!Uart.IsOpen) { return; } if (irx == -1) { return; } rx = (byte)irx; if (RxPacket.DataPacketed(rx)) { if (uartRxProc != null) { uartRxProc(RxPacket); } RxPacket = new CP1616Packet(); } } }
private void button1_Click(object sender, EventArgs e) { CP1616Packet Rx1616 = new CP1616Packet(100, 1); bool bOK = false; for (int i = 0; i < 3; i++) { try { byte[] deviceID = new byte[2]; deviceID[0] = BytesOP.GetHighByte((ushort)numericUpDown1.Value); deviceID[1] = BytesOP.GetLowByte((ushort)numericUpDown1.Value); byte[] tx = CP1616Packet.MakeCP1616Packet(100, 1, deviceID); try { COMPort1.Write(tx, 0, tx.Length); } catch { continue; } while (true) { try { if (Rx1616.DataPacketed((byte)COMPort1.ReadByte())) { MessageBox.Show("设定成功"); bOK = true; return; } } catch { break; } } if (bOK) { break; } Thread.Sleep(100); } catch { break; } } MessageBox.Show("设定失败"); }
private void btDisconnect_Click(object sender, EventArgs e) { serialPort1.ReadTimeout = 500; Rx1616 = new CP1616Packet(2, 1); byte[] tx = CP1616Packet.MakeCP1616Packet(2, 1, 1); try { serialPort1.Write(tx, 0, tx.Length); } catch { PortName = ""; progressBar1.Value = progressBar1.Maximum; serialPort1.Close(); bConnect.BackColor = Color.Red; lConnect.Text = "已断开"; MessageBox.Show("成功断开连接!!!"); } while (true) { try { if (Rx1616.DataPacketed((byte)serialPort1.ReadByte())) { PortName = ""; progressBar1.Value = progressBar1.Maximum; bConnect.BackColor = Color.Red; lConnect.Text = "已断开"; MessageBox.Show("成功与主机断开连接!!!"); break; } } catch { break; } } serialPort1.Close(); btDisconnect.Enabled = false; btScan.Enabled = false; btConnect.Enabled = true; }
private void button1_Click(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())) { ee = new ExcelExport(100); ee.ExcelWorkbookCallbackProc = new ExcelExport.ExcelWorkbookCallback(ExcelWorkbookCallbackProc); ee.ExcelExportProc(); 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; } } 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; } } ee = new ExcelExport(); ee.SheetCount = se.Count; ee.ExcelWorkbookCallbackProc = new ExcelExport.ExcelWorkbookCallback(ExcelWorkbookCallbackProc1); ee.ExcelExportProc1(); } else { MessageBox.Show("读取历史数据失败"); } } }
//连接设备 private void button1_Click(object sender, EventArgs e) { //bool bOK = false; if (button1.Text == "连接设备")//启动串口扫描,连接设备 { Rx1616 = new CP1616Packet(1, 1); string[] ports = SerialPort.GetPortNames(); PortName = ""; progressBar1.Value = 0; int perCount, per; perCount = ports.Length * 3; per = 100 / perCount; foreach (string com in ports) { for (int i = 0; i < 3; i++) { progressBar1.Value += per; try { COMPort1.Close(); COMPort1.PortName = com; COMPort1.Open(); byte[] tx = CP1616Packet.MakeCP1616Packet(1, 1, 1); try { COMPort1.Write(tx, 0, tx.Length); } catch { continue; } while (true) { try { if (Rx1616.DataPacketed((byte)COMPort1.ReadByte())) { DeviceID = BytesOP.MakeShort(Rx1616.Data[0], Rx1616.Data[1]); label3.Text = "设备ID:" + DeviceID.ToString(); label2.Text = com;//.Show(com); PortName = com; progressBar1.Value = 100; button1.Text = "断开连接"; enBT(true); //bOK = true; break; } } catch { break; } } if (PortName != "") { break; } COMPort1.Close(); Thread.Sleep(100); } catch { break; } } if (PortName != "") { break; } } if (PortName == "") { progressBar1.Value = 100; MessageBox.Show("未找到设备"); } } else//已经连接,则断开 { Rx1616 = new CP1616Packet(2, 1); byte[] tx = CP1616Packet.MakeCP1616Packet(2, 1, 1); try { COMPort1.Write(tx, 0, tx.Length); } catch { PortName = ""; progressBar1.Value = 100; button1.Text = "连接设备"; label3.Text = ""; enBT(false); button1.Enabled = true; COMPort1.Close(); } while (true) { try { if (Rx1616.DataPacketed((byte)COMPort1.ReadByte())) { PortName = ""; progressBar1.Value = 100; button1.Text = "连接设备"; enBT(false); button1.Enabled = true; label3.Text = ""; break; } } catch { break; } } COMPort1.Close(); } }
//同步地址信息 private void ImportAddrInfo_Click(object sender, EventArgs e) { ExcelImport el = new ExcelImport(true, "安装位置"); DataTable dt = el.ExcelImportProc_OleDB(); if (dt == null || dt.Rows.Count != 104) { MessageBox.Show("地址数据文件格式有问题,请检查!"); return; } enBT(false); int areaLen = 64; int addrInfoLen = areaLen + 100 * 2; byte[] addrInfo = new byte[addrInfoLen]; for (int i = 0; i < addrInfoLen; i++) { addrInfo[i] = 0; } //设备安装小区 string strInstallArea = dt.Rows[2][1].ToString(); Debug.WriteLine("设备安装小区:" + strInstallArea); byte[] byteInstallArea = Encoding.Default.GetBytes(strInstallArea); if (byteInstallArea.Length >= areaLen) { for (int i = 0; i < areaLen; i++) { addrInfo[i] = byteInstallArea[i]; } } else { for (int i = 0; i < byteInstallArea.Length; i++) { addrInfo[i] = byteInstallArea[i]; } } //栋单元信息处理 byte dong, danyuan; for (int i = 0; i < 100; i++) { Debug.WriteLine("传感器" + (i + 1).ToString() + ":" + dt.Rows[4 + i][1].ToString() + "-" + dt.Rows[4 + i][2].ToString()); if (!byte.TryParse(dt.Rows[4 + i][1].ToString(), out dong)) { MessageBox.Show("单元格B" + (i + 5).ToString() + "内容输入有问题,只能输入0-200的数字"); return; } if (!byte.TryParse(dt.Rows[4 + i][2].ToString(), out danyuan)) { MessageBox.Show("单元格C" + (i + 5).ToString() + "内容输入有问题,只能输入0-200的数字"); return; } addrInfo[areaLen + i * 2] = dong; addrInfo[areaLen + i * 2 + 1] = danyuan; } byte[] tx = CP1616Packet.MakeCP1616Packet(3, 1, addrInfo); 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) { break; } retry--; } if (retry == 0) { MessageBox.Show("地址数据导入失败,请重试!"); enBT(true); } else { MessageBox.Show("地址数据导入完成!"); enBT(true); } }
private void button1_Click(object sender, EventArgs e) { Rx1616 = new CP1616Packet(100, 1); string[] ports = SerialPort.GetPortNames(); PortName = ""; progressBar1.Value = 0; int perCount, per; perCount = ports.Length * 3; per = 100 / perCount; bool bOK = false; foreach (string com in ports) { COMPort1.PortName = com; for (int i = 0; i < 3; i++) { progressBar1.Value += per; try { COMPort1.Open(); byte[] deviceID = new byte[2]; deviceID[0] = BytesOP.GetHighByte((ushort)numericUpDown1.Value); deviceID[1] = BytesOP.GetLowByte((ushort)numericUpDown1.Value); byte[] tx = CP1616Packet.MakeCP1616Packet(100, 1, deviceID); try { COMPort1.Write(tx, 0, tx.Length); } catch { continue; } while (true) { try { if (Rx1616.DataPacketed((byte)COMPort1.ReadByte())) { MessageBox.Show("设定成功"); bOK = true; break; } } catch { break; } } if (bOK) { break; } COMPort1.Close(); Thread.Sleep(100); } catch { break; } } if (bOK) { break; } } if (!bOK) { progressBar1.Value = 100; MessageBox.Show("未找到设备"); } COMPort1.Close(); }
void Uart_DataReceived(object sender, SerialDataReceivedEventArgs e) { int irx; byte rx; while (true) { try { irx = Uart.ReadByte(); } catch { return; } if (!Uart.IsOpen) { return; } if (irx == -1) { return; } rx = (byte)irx; if (RxPacket.DataPacketed(rx)) { byte[] data = new byte[2]; data[0] = RxPacket.Header.Command; if (waitCommand == RxPacket.Header.Command) { WaitSometingForm.bGenerateSometing = true; } if (bStopNet) { data[1] = 0x02;//要求关闭网络 CP1616Packet.CP1616ComSend(ref Uart, 3, 0, data); } else if (RxPacket.Header.Command == 1)//标定网络启动确认 { startResult = RxPacket.Data[0]; } else if (RxPacket.Header.Command == 0x22)//标定网络,请求转换为测量网络 { this.Invoke((EventHandler)(delegate { textBox1.AppendText(DateTime.Now.ToString("HH:mm:ss:ffff") + ":标定网络转换为测量网络\r\n"); })); startResult = 3; data[1] = 0x03; CP1616Packet.CP1616ComSend(ref Uart, 3, 0, data); } else if (RxPacket.Header.Command == 0x10) { data[1] = 0x00; CP1616Packet.CP1616ComSend(ref Uart, 3, 0, data); SignInProc(); } else if (RxPacket.Header.Command == 0x20) { startResult = 2; if (listView1.Items.Count == 0) { data[1] = 0x01;//请求传感器名单 CP1616Packet.CP1616ComSend(ref Uart, 3, 0, data); } else { data[1] = 0x00; CP1616Packet.CP1616ComSend(ref Uart, 3, 0, data); ADCDataProc(); } this.Invoke((EventHandler)(delegate { if (btNetStart.Text == "启动网络") { btNetStart.Text = "停止网络"; } })); } RxPacket = new CP1616Packet(); } } }
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 btConnect_Click(object sender, EventArgs e) { serialPort1.ReadTimeout = 500; bool bOK = false; string[] ports = SerialPort.GetPortNames(); Rx1616 = new CP1616Packet(1, 1); PortName = ""; progressBar1.Value = 0; progressBar1.Maximum = ports.Length * 3; foreach (string com in ports) { serialPort1.PortName = com; for (int i = 0; i < 3; i++) { progressBar1.PerformStep(); try { serialPort1.Open(); byte[] tx = CP1616Packet.MakeCP1616Packet(1, 1, null); try { serialPort1.Write(tx, 0, tx.Length); } catch { continue; } while (true) { try { if (Rx1616.DataPacketed((byte)serialPort1.ReadByte())) { if (Rx1616.Data[0] == 0x01)//成功 { PortName = com; progressBar1.Value = progressBar1.Maximum; bOK = true; bConnect.BackColor = Color.Green; lConnect.Text = "已连接"; MessageBox.Show("成功连接到主机!!!"); btDisconnect.Enabled = true; btScan.Enabled = true; btConnect.Enabled = false; break; } else { serialPort1.Close(); if (Rx1616.Data[0] == 0x10) { MessageBox.Show("主机设备自动模式无法与计算机进行连接!"); } else { MessageBox.Show("主机设备当前无法与计算机进行连接!" + Rx1616.Data[0].ToString("x02")); } return; } } } catch { break; } } if (PortName != "") { break; } serialPort1.Close(); Thread.Sleep(100); } catch { break; } } if (PortName != "") { break; } } if (PortName == "") { progressBar1.Value = progressBar1.Maximum; MessageBox.Show("未找到设备"); } }
void PickOne() { serialPort1.ReadTimeout = 2000; int retry = 3; Rx1616 = new CP1616Packet(3, 1); byte[] bv = new byte[2]; bv[0] = BytesOP.GetHighByte((ushort)daV); bv[1] = BytesOP.GetLowByte((ushort)daV); byte[] tx = CP1616Packet.MakeCP1616Packet(3, 1, bv); while (retry != 0) { try { serialPort1.Write(tx, 0, tx.Length); } catch { PortName = ""; serialPort1.Close(); PickFinish(); MessageBox.Show("发送错误,与主机断开连接"); btDisconnect.Enabled = false; btConnect.Enabled = true; return; } while (true) { try { if (Rx1616.DataPacketed((byte)serialPort1.ReadByte())) { if (Rx1616.Data[0] == 0x01) { chart1.Series[0].Points.AddXY(daV * 0.08, BytesOP.MakeShort(Rx1616.Data[1], Rx1616.Data[2])); lX.Text = (daV * 0.08).ToString("f01"); lY.Text = BytesOP.MakeShort(Rx1616.Data[1], Rx1616.Data[2]).ToString(); progressBar1.PerformStep(); if (daV >= 1023) { tScan.Enabled = false; MessageBox.Show("采集完成"); PickFinish(); } else { tScan.Enabled = true; daV += stepV; if (daV >= 1023) { daV = 1023; } } } else { PortName = ""; serialPort1.Close(); PickFinish(); btDisconnect.Enabled = false; btConnect.Enabled = true; bConnect.BackColor = Color.Red; lConnect.Text = "已断开"; MessageBox.Show("发送错误,与主机断开连接"); } return; } } catch { break; } } retry--; } if (retry == 0)//重试失败 { PortName = ""; serialPort1.Close(); PickFinish(); btDisconnect.Enabled = false; btConnect.Enabled = true; bConnect.BackColor = Color.Red; lConnect.Text = "已断开"; MessageBox.Show("发送错误,与主机断开连接"); } }
private void button1_Click(object sender, EventArgs e) { if (radioButton1.Checked) { int retry; bool bOK; retry = 20; bOK = false; while (retry != 0) { Rx1616 = new CP1616Packet(6, 1); byte[] tx = CP1616Packet.MakeCP1616Packet(6, 1, new byte[2] { 0, 100 }); try { COMPort1.Write(tx, 0, tx.Length); } catch { retry--; continue; } while (true) { try { if (Rx1616.DataPacketed((byte)COMPort1.ReadByte())) { bOK = true; MessageBox.Show("清空数据成功"); break; } } catch { break; } } if (bOK) { break; } retry--; } if (retry == 0) { MessageBox.Show("清空数据失败"); } } else if (radioButton2.Checked) { int retry; bool bOK; retry = 20; bOK = false; while (retry != 0) { Rx1616 = new CP1616Packet(6, 1); byte[] tx = CP1616Packet.MakeCP1616Packet(6, 1, new byte[2] { (byte)(numericUpDown1.Value - 1), (byte)(numericUpDown2.Value) }); try { COMPort1.Write(tx, 0, tx.Length); } catch { retry--; continue; } while (true) { try { if (Rx1616.DataPacketed((byte)COMPort1.ReadByte())) { bOK = true; MessageBox.Show("清空数据成功"); break; } } catch { break; } } if (bOK) { break; } retry--; } if (retry == 0) { MessageBox.Show("清空数据失败"); } } }
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("读取历史数据失败"); } } }
private void button2_Click(object sender, EventArgs e) { ExcelImport el = new ExcelImport(true, "安装位置"); DataTable dt = el.ExcelImportProc_OleDB(); if (dt == null || dt.Rows.Count < 100 || dt.Columns.Count < 2) { MessageBox.Show("地址数据文件格式有问题,请检查!"); return; } enBT(false); byte[] tx; byte[] temp; byte[] temp1; progressBar1.Value = 0; for (int i = 0; i < 100; i++) { temp = Encoding.Default.GetBytes(dt.Rows[i][1].ToString()); Debug.WriteLine(dt.Rows[i][1].ToString()); if (temp.Length > 20) { temp1 = new byte[23]; temp1[0] = (byte)(i); 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] = (byte)(i); 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); }