/// <summary> /// 设参数 /// </summary> /// <param name="tbObj">要输入数据的文本框控件name</param> /// <param name="dataId"></param> private void SetParameter(object tbObj, string dataId) { if (!(tbObj is TextBox)) { return; } TextBox tb = (TextBox)tbObj; string strDate = tb.Text; if (strDate.Length != tb.MaxLength) { MessageBox.Show("数据长度过长或不足!"); return; } if (dataId == "04000101" || strDate.Length == 6)//如果是日期,则要判断星期 { strDate = Method.JustWeek(strDate); } if (!Protocol645.WriteData(dataId, strDate)) { tb.ForeColor = Color.Red; tb.Text = "Err"; } }
/// <summary> /// 设数据 /// </summary> /// <param name="obj"></param> private void Set(object obj) { //_ret2 = "test"; Ret2 = "test"; Protocol645 p645 = new Protocol645(); p645.WriteData(obj.ToString(), ""); }
private void btn二类数据发送_Click(object sender, EventArgs e)//200次发送 { string orginalPsw = Protocol645.Psw; if (tbMacErlei.Text == "" || tbEndataErlei.Text == "") { MessageBox.Show("请先获取密文及MAC!"); return; } Protocol645.Psw = "00000098";//临时改为98级密码 for (int i = 0; i < Convert.ToInt16(this.tbSendTimes.Text); i++) { this.lblTimes.Text = (i + 1).ToString(); Protocol645 p645 = new Protocol645(); p645.WriteData(this.tbErleishujvID.Text, this.tbMacErlei.Text + this.tbEndataErlei); Functions.Delay(100); } Protocol645.Psw = orginalPsw;//用完后要改回界面上的密码,否则影响其他功能使用 }
/// <summary> /// 99级权限写数据 /// </summary> /// <param name="putApdu">5个字节命令头</param> /// <param name="dataID">数据ID</param> /// <param name="putData">数据</param> private void Level99PramSet(string putApdu, string dataID, string putData) { if (Functions.IsNum(putData)) { if (tbRand2.Text != "" && tbEsamNo.Text != "") { string orginalPsw = Protocol645.Psw; Esam645Service.EsamOptionClient ESAMproxy = new Esam645Service.EsamOptionClient();//要操作时才new try { string macStr = ESAMproxy.Meter_Formal_ParameterUpdate(0, tbRand2.Text, tbDiv.Text, putApdu, putData).MAC; Protocol645.Psw = "00000099";//临时改为99级密码 Protocol645 p645 = new Protocol645(); p645.WriteData(dataID, macStr + putData); } catch (Exception ex) { MessageBox.Show(ex.Message); return; } finally { ESAMproxy.Close(); Protocol645.Psw = orginalPsw;//用完后要改回界面上的密码,否则影响其他功能使用 } } else { MessageBox.Show("请先身份认证!"); return; } } else { MessageBox.Show("请输入数字!"); return; } }
private void btn二类数据ESAM设_Click(object sender, EventArgs e) { string putData = this.tbErleishujv.Text; if (putData.Length > 1) { if (tbRand2.Text != "" && tbEsamNo.Text != "") { string orginalPsw = Protocol645.Psw; Esam645Service.EsamOptionClient ESAMproxy = new Esam645Service.EsamOptionClient();//要操作时才new try { string dataID = this.tbErleishujvID.Text; putData = dataID + putData; int dataID_DI2 = Convert.ToInt16(dataID.Substring(2, 2), 16); string putApdu = null; if (dataID_DI2 % 5 == 0) { putApdu = "04D68900" + (putData.Length / 2 + 0x0C).ToString("X2"); } if (dataID_DI2 % 5 == 1) { putApdu = "04D69000" + (putData.Length / 2 + 0x0C).ToString("X2"); } if (dataID_DI2 % 5 == 2) { putApdu = "04D69100" + (putData.Length / 2 + 0x0C).ToString("X2"); } if (dataID_DI2 % 5 == 3) { putApdu = "04D69200" + (putData.Length / 2 + 0x0C).ToString("X2"); } if (dataID_DI2 % 5 == 4) { putApdu = "04D69300" + (putData.Length / 2 + 0x0C).ToString("X2"); } string macStr = ESAMproxy.Meter_Formal_ParameterElseUpdate(0, tbRand2.Text, tbDiv.Text, putApdu, putData).MAC; string enData = ESAMproxy.Meter_Formal_ParameterElseUpdate(0, tbRand2.Text, tbDiv.Text, putApdu, putData).DataValue; this.tbEndataErlei.Text = enData; this.tbMacErlei.Text = macStr; Protocol645.Psw = "00000098";//临时改为98级密码 Protocol645 p645 = new Protocol645(); p645.WriteData(dataID, macStr + enData); } catch (Exception ex) { MessageBox.Show(ex.Message); return; } finally { ESAMproxy.Close(); Protocol645.Psw = orginalPsw;//用完后要改回界面上的密码,否则影响其他功能使用 } } else { MessageBox.Show("请先身份认证!"); return; } } else { MessageBox.Show("请输入要设的数据!"); return; } }
private void btn费率ESAM设_Click(object sender, EventArgs e) { int n = Convert.ToInt16(numUpdownDianjia.Value); string putData = null; string putDataReverse = null; for (int i = 0; i < n; i++) { string s = null; if (this.dgRates[0, i].Value == null || this.dgRates[0, i].Value.ToString() == "") { putData += "00000000"; } else { s = this.dgRates[0, i].Value.ToString(); if (s.Contains(".")) { string[] ss = s.Split('.'); putData += (ss[0].PadLeft(4, '0') + ss[1].PadRight(4, '0')); } else { putData += (s.PadLeft(4, '0') + "0000"); } } } for (int i = 0; i < n; i++) { putDataReverse += putData.Substring(8 * (n - i - 1), 8); } string putApdu = "04D68404" + (n * 4 + 0x0C).ToString("X2"); if (Functions.IsNum(putData)) { if (tbRand2.Text != "" && tbEsamNo.Text != "") { string orginalPsw = Protocol645.Psw; Esam645Service.EsamOptionClient ESAMproxy = new Esam645Service.EsamOptionClient();//要操作时才new try { string macStr = ESAMproxy.Meter_Formal_ParameterUpdate2(0, tbRand2.Text, tbDiv.Text, putApdu, putData).MAC; Protocol645.Psw = "00000099";//临时改为99级密码 Protocol645 p645 = new Protocol645(); p645.WriteData("040502FF", macStr + putDataReverse); } catch (Exception ex) { MessageBox.Show(ex.Message); return; } finally { ESAMproxy.Close(); Protocol645.Psw = orginalPsw;//用完后要改回界面上的密码,否则影响其他功能使用 } } else { MessageBox.Show("请先身份认证!"); return; } } else { MessageBox.Show("请输入数字!"); return; } }
public void SetTimeEventTest() { txtBoxTimeSetEvent.Clear(); Protocol645 p645 = new Protocol645(); //校时记录清零 txtBoxTimeSetEvent.AppendText("校时记录清零。。。"); if (p645.EventClear("033004")) { txtBoxTimeSetEvent.AppendText(" 成功!\r\n"); } else { txtBoxTimeSetEvent.AppendText(" 失败!\r\n"); } //校时记录测试 Functions.Delay(100);//等待清零动作完成 bool setTimeEventIsOk = false; string[] _10timesSetTimeEvent = new string[10];//将10次校时记录写进一个数组,以便比较 for (int m = 0; m < 10; m++) { _10timesSetTimeEvent[m] = ReadLastNtimesRec("033004", m + 1); if (_10timesSetTimeEvent[m] == "00000000000000000000000000000000")//判断是否已经清零 { setTimeEventIsOk = true; } else { setTimeEventIsOk = false; break; } } if (setTimeEventIsOk) { for (int i = 0; i < int.Parse(textBoxTestTimes.Text); i++) { //********************随机设时间********************************* string setTime = Functions.GenerateRandomTime(); if (p645.WriteData("04000102", setTime)) { txtBoxTimeSetEvent.AppendText((i + 1).ToString("D5") + ":随机设电表时间:" + setTime.Substring(0, 2) + ":" + setTime.Substring(2, 2) + ":" + setTime.Substring(4, 2) + "..........................成功!\r\n"); } else { txtBoxTimeSetEvent.AppendText((i + 1).ToString("D5") + ":设电表时间:设电表时间失败!\r\n"); } //********************读校时记录总次数,检查是否记录正确********************************* Functions.Delay(50); string timesOfSetTime = null; if (p645.ReadData("03300400", out timesOfSetTime)) { txtBoxTimeSetEvent.AppendText(" 读校时次数:" + timesOfSetTime.Substring(0, 6)); if (timesOfSetTime == (i + 1).ToString("D6")) { txtBoxTimeSetEvent.AppendText(" 校时次数符合实际设的次数,成功!\r\n"); } else { txtBoxTimeSetEvent.AppendText(" 校时次数不符合实际设的次数,失败!\r\n"); } } else//如果失败就读多一次 if (p645.ReadData("03300400", out timesOfSetTime)) { txtBoxTimeSetEvent.AppendText(" 读校时次数:" + timesOfSetTime.Substring(0, 6)); if (timesOfSetTime == (i + 1).ToString("D6")) { txtBoxTimeSetEvent.AppendText(" 校时次数符合实际设的次数,成功!\r\n"); } else { txtBoxTimeSetEvent.AppendText(" 校时次数不符合实际设的次数,失败!\r\n"); } } else { txtBoxTimeSetEvent.AppendText(" 读校时次数失败!\r\n"); } //********************读上10次校时记录,检查是否记录正确********************************* //读10次记录 for (int j = 0; j < 10; j++) { if (ReadLastNtimesRec("033004", j + 1) != null) { txtBoxTimeSetEvent.AppendText(" 上" + (j + 1).ToString("D2") + "次校时记录:" + ReadLastNtimesRec("033004", j + 1) + " 成功!\r\n"); } else { txtBoxTimeSetEvent.AppendText(" 上" + (j + 1).ToString("D2") + "次校时记录:读上" + (j + 1).ToString() + "次校时记录失败!\r\n"); } } //判断10次记录 //this.txtBoxTimeSetEvent.AppendText(" 判断10次校时事件是否正确记录(判断上1次事件记录的校时后时间是否为所设时间、判断10次记录是否轮换正确)。。。\r\n"); string[] _10timesSetTimeEventTemp = new string[10]; for (int m = 0; m < 10; m++) { _10timesSetTimeEventTemp[m] = ReadLastNtimesRec("033004", m + 1); } if (ReadLastNtimesRec("033004", 1) != null && ReadLastNtimesRec("033004", 1).Substring(6, 6) == setTime)//判断第一次是否等于所设时间 { for (int m = 1; m < 10; m++) { if (_10timesSetTimeEventTemp[m] == _10timesSetTimeEvent[m - 1])//新的上2~10次等于旧的上1~9次 { setTimeEventIsOk = true; } else { setTimeEventIsOk = false; break; } } //_10timesSetTimeEvent = _10timesSetTimeEventTemp; System.Array.Copy(_10timesSetTimeEventTemp, _10timesSetTimeEvent, _10timesSetTimeEvent.Length);//新的写入旧的,以便下次比较 } else { setTimeEventIsOk = false; } if (setTimeEventIsOk) { txtBoxTimeSetEvent.AppendText(" 10次校时事件记录正确!\r\n"); } else { txtBoxTimeSetEvent.AppendText(" 10次校时事件记录失败!\r\n"); } } } else { txtBoxTimeSetEvent.AppendText("10校时记录不为空,请检查是否清空!失败!\r\n"); } txtBoxTimeSetEvent.AppendText("\r\n校时记录自动测试结束!\r\n"); //判断测试结果是否合格 string testRec = txtBoxTimeSetEvent.Text; if (testRec.Contains("失败")) { labelSetTimeEventTestResult.Text = "测试结果:不合格"; } else { labelSetTimeEventTestResult.Text = "测试结果:合格"; } btnTimeSetEventTest.Text = "开始测试"; btnDataSave.Enabled = true; }
/// <summary> /// 设参数 /// </summary> /// <param name="dataId">数据ID</param> /// <param name="data">数据</param> private void SetParameter(string dataId, string data) { data = data.Replace(" ", "").Replace("\r\n", ""); Protocol645.WriteData(dataId, data); }
private void ZhengdianFreezeTest() { this.tBZhengdianFreeze.Clear(); //整点冻结测试 bool zhengdianFreezeIsOk = false; int timesOfZhengdianFreeze = Convert.ToInt16(this.tBTimesOfZhengdianRec.Text); string[] _NtimesZhengdianFreezeOld = new string[timesOfZhengdianFreeze]; //测之前先记录整点冻结数据 string[] _NtimesZhengdianFreezeNew = new string[timesOfZhengdianFreeze]; //测之后记录整点冻结数据,判断新的上2~N次记录是否等于旧的上1~(N-1)次记录 int startTestHour = DateTime.Now.Hour; //****************************读整点冻结数据********************************************* for (int m = 0; m < timesOfZhengdianFreeze; m++) { _NtimesZhengdianFreezeOld[m] = ReadLastNtimesZhengdianFreeze(m + 1); if (_NtimesZhengdianFreezeOld[m].Length == 58) { tBZhengdianFreeze.AppendText(" 上" + (m + 1).ToString("D3") + "次整点冻结:" + _NtimesZhengdianFreezeOld[m] + " 读取成功!\r\n"); } else { tBZhengdianFreeze.AppendText(" 上" + (m + 1).ToString("D3") + "次整点冻结:" + _NtimesZhengdianFreezeOld[m] + " 读取失败!\r\n"); } } for (int i = 0; i < int.Parse(tBTimesOfZhengdianFreezeTest.Text); i++) { //****************************设时间在整点(半整点)前5秒********************************* string setTime = "000000"; string _1stZhengdianTime = null;//先定义上1次过整点冻结的时间,方便后面比较 if (rBZhengdian.Checked) { setTime = startTestHour.ToString("D2") + "5955"; if (startTestHour < 23) { _1stZhengdianTime = (startTestHour + 1).ToString("D2") + "00"; } else { _1stZhengdianTime = "0000"; } } if (rBBanZhengdian.Checked && (DateTime.Now.Minute < 30)) { setTime = startTestHour.ToString("D2") + "2955"; _1stZhengdianTime = startTestHour.ToString("D2") + "30"; } if (rBBanZhengdian.Checked && (DateTime.Now.Minute >= 30)) { setTime = startTestHour.ToString("D2") + "5955"; if (startTestHour < 23) { _1stZhengdianTime = (startTestHour + 1).ToString("D2") + "00"; } else { _1stZhengdianTime = "0000"; } } startTestHour++; if (startTestHour == 24) { startTestHour = 0; } Protocol645 p645 = new Protocol645(); if (p645.WriteData("04000102", setTime)) { tBZhengdianFreeze.AppendText((i + 1).ToString("D5") + ":设电表时间:" + setTime.Substring(0, 2) + ":" + setTime.Substring(2, 2) + ":" + setTime.Substring(4, 2) + " 设置成功!\r\n"); } else { tBZhengdianFreeze.AppendText((i + 1).ToString("D5") + ":设电表时间:设电表时间失败! 失败原因:\r\n"); } //****************************等待时间过整点(半整点)************************************ tBZhengdianFreeze.AppendText(" 等待10秒...\r\n"); Functions.Delay(10000);//等待10秒 //****************************读整点冻结数据********************************************* for (int m = 0; m < timesOfZhengdianFreeze; m++) { _NtimesZhengdianFreezeNew[m] = ReadLastNtimesZhengdianFreeze(m + 1); if (_NtimesZhengdianFreezeNew[m].Length == 58) { tBZhengdianFreeze.AppendText(" 上" + (m + 1).ToString("D3") + "次整点冻结:" + _NtimesZhengdianFreezeNew[m] + " 读取成功!\r\n"); } else { tBZhengdianFreeze.AppendText(" 上" + (m + 1).ToString("D3") + "次整点冻结:" + _NtimesZhengdianFreezeNew[m] + " 读取失败!\r\n"); } } //****************************判断新的上1次整点冻结时间是否正确*************************** if (_NtimesZhengdianFreezeNew[0] != null && _NtimesZhengdianFreezeNew[0].Substring(6, 4) == _1stZhengdianTime) { //****************************判断新的上2~N次记录是否等于旧的上1~(N-1)次记录************** for (int m = 1; m < timesOfZhengdianFreeze; m++) { if (_NtimesZhengdianFreezeNew[m] == _NtimesZhengdianFreezeOld[m - 1])//新的上2~N次等于旧的上1~(N-1)次 { zhengdianFreezeIsOk = true; } else { zhengdianFreezeIsOk = false; break; } } //_NtimesZhengdianFreezeOld = _NtimesZhengdianFreezeNew; System.Array.Copy(_NtimesZhengdianFreezeNew, _NtimesZhengdianFreezeOld, _NtimesZhengdianFreezeNew.Length);//新的终究要变成旧的,好吧。。。 } else { zhengdianFreezeIsOk = false; } if (zhengdianFreezeIsOk) { this.tBZhengdianFreeze.AppendText("整点冻结记录正确!\r\n"); } else { this.tBZhengdianFreeze.AppendText("整点冻结记录失败!\r\n"); } } this.tBZhengdianFreeze.AppendText("整点冻结测试结束!"); //判断测试结果是否合格 string testRec = this.tBZhengdianFreeze.Text; if (testRec.Contains("失败")) { this.labelZhengdianTestResult.Text = "测试结果:不合格"; } else { this.labelZhengdianTestResult.Text = "测试结果:合格"; } this.btnZhengdianFreezeTest.Text = "开始测试"; this.btnZhengdianTestDataSave.Enabled = true; }
private void RidongjieTest() { this.tBRidongjieRec.Clear(); bool riFreezeIsOk = false; int timesOfRiFreeze = Convert.ToInt16(this.tBRidongjieRecTimes.Text); string[] _NtimesRiFreezeOld = new string[timesOfRiFreeze]; //测之前先记录日冻结数据 string[] _NtimesRiFreezeNew = new string[timesOfRiFreeze]; //测之后记录日冻结数据,判断新的上2~N次记录是否等于旧的上1~(N-1)次记录 string ridongjieDate = null; Protocol645 p645 = new Protocol645(); p645.ReadData("04001203", out ridongjieDate); //****************************读日冻结数据********************************************* for (int m = 0; m < timesOfRiFreeze; m++) { _NtimesRiFreezeOld[m] = ReadLastNtimesRiFreeze(m + 1); if (_NtimesRiFreezeOld[m].Length == 138) { tBRidongjieRec.AppendText(" 上" + (m + 1).ToString("D3") + "次日冻结:" + _NtimesRiFreezeOld[m] + " 读取成功!\r\n"); } else { tBRidongjieRec.AppendText(" 上" + (m + 1).ToString("D3") + "次日冻结:" + _NtimesRiFreezeOld[m] + " 读取失败!\r\n"); } } for (int i = 0; i < int.Parse(tBRidongjieTestTimes.Text); i++) { //****************************设时间在日冻结时间前5秒********************************* string setTime = "000000"; //日冻结时间有4种可能:00:00、00:XX、XX:00、XX:XX if (ridongjieDate == "0000") { setTime = "235955"; } else if (ridongjieDate.Substring(0, 2) == "00" && ridongjieDate.Substring(2, 2) != "00") { setTime = "00" + (Convert.ToInt16(ridongjieDate.Substring(2, 2)) - 1).ToString("D2") + "55"; } else if (ridongjieDate.Substring(0, 2) != "00" && ridongjieDate.Substring(2, 2) == "00") { setTime = (Convert.ToInt16(ridongjieDate.Substring(0, 2)) - 1).ToString("D2") + "5955"; } else if (ridongjieDate.Substring(0, 2) != "00" && ridongjieDate.Substring(2, 2) != "00") { setTime = ridongjieDate.Substring(0, 2) + (Convert.ToInt16(ridongjieDate.Substring(2, 2)) - 1).ToString("D2") + "55"; } if (p645.WriteData("04000102", setTime)) { this.tBRidongjieRec.AppendText((i + 1).ToString("D5") + ":设电表时间:" + setTime.Substring(0, 2) + ":" + setTime.Substring(2, 2) + ":" + setTime.Substring(4, 2) + " 设置成功!\r\n"); } else { this.tBRidongjieRec.AppendText((i + 1).ToString("D5") + ":设电表时间:设电表时间失败!\r\n"); } //****************************等待时间过日冻结时间************************************ tBRidongjieRec.AppendText(" 等待10秒...\r\n"); Functions.Delay(10000);//等待10秒 //****************************读日冻结数据********************************************* for (int m = 0; m < timesOfRiFreeze; m++) { _NtimesRiFreezeNew[m] = ReadLastNtimesRiFreeze(m + 1); if (_NtimesRiFreezeNew[m].Length == 138) { tBRidongjieRec.AppendText(" 上" + (m + 1).ToString("D3") + "次日冻结:" + _NtimesRiFreezeNew[m] + " 读取成功!\r\n"); } else { tBRidongjieRec.AppendText(" 上" + (m + 1).ToString("D3") + "次日冻结:" + _NtimesRiFreezeNew[m] + " 读取失败!\r\n"); } } //****************************判断新的上1次日冻结时间是否正确*************************** if (_NtimesRiFreezeNew[0].Substring(6, 4) == ridongjieDate) { //****************************判断新的上2~N次记录是否等于旧的上1~(N-1)次记录************** for (int m = 1; m < timesOfRiFreeze; m++) { if (_NtimesRiFreezeNew[m] == _NtimesRiFreezeOld[m - 1])//新的上2~N次等于旧的上1~(N-1)次 { riFreezeIsOk = true; } else { riFreezeIsOk = false; break; } } System.Array.Copy(_NtimesRiFreezeNew, _NtimesRiFreezeOld, _NtimesRiFreezeNew.Length);//新的终究要变成旧的,好吧。。。 } else { riFreezeIsOk = false; } if (riFreezeIsOk) { this.tBRidongjieRec.AppendText("日冻结记录正确!\r\n"); } else { this.tBRidongjieRec.AppendText("日冻结记录失败!\r\n"); } } this.tBRidongjieRec.AppendText("日冻结测试结束!"); //判断测试结果是否合格 string testRec = this.tBRidongjieRec.Text; if (testRec.Contains("失败")) { this.lblRidongjieResult.Text = "测试结果:不合格"; } else { this.lblRidongjieResult.Text = "测试结果:合格"; } this.btnRidongjieTest.Text = "开始测试"; this.btnRidongjieDataSave.Enabled = true; }