using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using BatCharging.Model; using BatCharging.Service; using Monitor.LogService; using Newtonsoft.Json; namespace Monitor.ChargeTest { public partial class FrmChargeResult : Form { #region 处理结果消息委托 //ListBox文本框类型委托 private delegate void SetListBoxCallBack(string str); private SetListBoxCallBack MsgConnectLogCallBack; //服务端或客户端连接状态信息委托 //RichTextBox文本框类型委托 private delegate void SetRTxtCallBack(string str); private SetRTxtCallBack RTxtSendFramesInfoCallBack; //客户端发送数据帧委托 private SetRTxtCallBack RTxtRecvFramesInfoCallBack; //客户端接收数据帧委托 #endregion 处理结果消息委托 public FrmChargeResult() { InitializeComponent(); MsgConnectLogCallBack = new SetListBoxCallBack(PrintConnectLogMsg); RTxtSendFramesInfoCallBack = new SetRTxtCallBack(PrintSendFramesInfo); RTxtRecvFramesInfoCallBack = new SetRTxtCallBack(PrintRecvFramesInfo); } /// /// 窗体加载事件 /// /// /// private void FrmRwTcpCanResult_Load(object sender, EventArgs e) { } /// /// 字节数组转16进制字符串:空格分隔 /// /// /// public string BytesToHexStr(byte[] byteDatas) { StringBuilder builder = new StringBuilder(); for (int i = 0; i < byteDatas.Length; i++) { builder.Append(string.Format("{0:X2} ", byteDatas[i])); } return builder.ToString().Trim(); } #region 委托事件方法 /// /// 连接状态日志 /// /// private void PrintConnectLogMsg(string message) { if (lstBoxConnect.Items.Count <= 12) { lstBoxConnect.Items.Add(message); lstBoxConnect.SelectedIndex = lstBoxConnect.Items.Count - 1; lstBoxConnect.ClearSelected(); } else { lstBoxConnect.Items.Clear(); lstBoxConnect.Items.Add(message); lstBoxConnect.SelectedIndex = lstBoxConnect.Items.Count - 1; lstBoxConnect.ClearSelected(); } } /// /// 显示发送的数据帧信息 /// /// 发送的数据帧信息 private void PrintSendFramesInfo(string msg) { if (lstSendFrameInfo.Items.Count <= 12) { lstSendFrameInfo.Items.Add(msg); lstSendFrameInfo.SelectedIndex = lstSendFrameInfo.Items.Count - 1; lstSendFrameInfo.ClearSelected(); } else { lstSendFrameInfo.Items.Clear(); lstSendFrameInfo.Items.Add(msg); lstSendFrameInfo.SelectedIndex = lstSendFrameInfo.Items.Count - 1; lstSendFrameInfo.ClearSelected(); } } /// /// 显示接收到的数据帧信息 /// /// 接收到的数据帧信息 private void PrintRecvFramesInfo(string msg) { if (lstRecvFrameInfo.Items.Count <= 12) { lstRecvFrameInfo.Items.Add(msg); lstRecvFrameInfo.SelectedIndex = lstRecvFrameInfo.Items.Count - 1; lstSendFrameInfo.ClearSelected(); } else { lstRecvFrameInfo.Items.Clear(); lstRecvFrameInfo.Items.Add(msg); lstRecvFrameInfo.SelectedIndex = lstRecvFrameInfo.Items.Count - 1; lstRecvFrameInfo.ClearSelected(); } } #endregion 委托事件方法 /// /// 设置textbox文本 /// /// /// private static void SetTextBoxSafe(TextBox tb, string text) { if (tb.InvokeRequired) tb.Invoke(new Action(() => tb.Text = text)); else tb.Text = text; } /// /// 客户端关闭 /// /// /// private void BtnNetClose_Click(object sender, EventArgs e) { CmnChargerParam._CHR00TCPCLIENT.ChargerDisConnect(); } /// /// 充电机网络连接 /// /// /// private void BtnChrg01Connect_Click(object sender, EventArgs e) { string strIp = txtChargeIp.Text.Trim(); int iPort = Convert.ToInt32(txtChargePort.Text.Trim()); int destaddr = Convert.ToInt32(txtDestAddr.Text.Trim()); CmnChargerParam._CHR00TCPCLIENT.ConnectedStatusChanged += ChargerMgrTool_ConnectedStatusChanged; CmnChargerParam._CHR00TCPCLIENT.ChargerConnect(strIp, iPort, destaddr); CmnChargerParam._CHR00TCPCLIENT.DataSended += ChargerMgrTool_DataSended; CmnChargerParam._CHR00TCPCLIENT.DataReceived += ChargerMgrTool_DataReceived; } /// /// 数据接收解析 /// /// /// private void ChargerMgrTool_DataReceived(object sender, DataRecvedEventArgs e) { string result = ""; byte[] data = e.Data; if (data != null) { result += "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "]" + "RECV HEX:"; result += BytesToHexStr(data); lstRecvFrameInfo.Invoke(RTxtRecvFramesInfoCallBack, result); } } /// /// 数据发送 /// /// /// private void ChargerMgrTool_DataSended(object sender, DataSendedEventArgs e) { string result = ""; byte[] data = e.Data; if (data != null) { result += "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "]" + "SEND HEX:"; result += BytesToHexStr(data); lstSendFrameInfo.Invoke(RTxtSendFramesInfoCallBack, result); } } /// /// 连接状态改变 /// /// /// private void ChargerMgrTool_ConnectedStatusChanged(object sender, LogEventArgs e) { lstBoxConnect.Invoke(MsgConnectLogCallBack, e.IsContent); } #region 协议格式 /// /// (CMD=1)后台服务器下发充电桩整形工作参数 /// /// /// private void button14_Click(object sender, EventArgs e) { byte type = 1;//类型 0-查询/1-设置 uint startAddress = 2;//设置/查询参数起始地址 byte num = 2;//设置/查询个数 byte[] setData = new byte[8];//设置数据 setData[0] = 0; setData[1] = 1; setData[2] = 2; setData[3] = 3; setData[4] = 4; setData[5] = 5; setData[6] = 6; setData[7] = 7; CmnChargerParam._CHR00TCPCLIENT.Send1(type, startAddress, num, setData); } /// /// (CMD=3)后台服务器下发充电桩字符型参数 /// /// /// private void button15_Click(object sender, EventArgs e) { byte type = 1;//0-查询 1-设置 uint startAddress = 1;//设置/查询参数启始地址 byte[] setData = new byte[8];//设置数据 setData[0] = 0; setData[1] = 1; setData[2] = 2; setData[3] = 3; setData[4] = 4; setData[5] = 5; setData[6] = 6; setData[7] = 7; CmnChargerParam._CHR00TCPCLIENT.Send3(type, startAddress, setData); } /// /// (CMD=5)后台服务器下发充电桩控制命令 /// /// /// private void button16_Click(object sender, EventArgs e) { byte type = 1;//充电枪口 uint startAddress = 1;//启始命令地址 byte num = 2;//命令个数 byte[] setData = new byte[8];//命令参数 setData[0] = 0; setData[1] = 1; setData[2] = 2; setData[3] = 3; setData[4] = 4; setData[5] = 5; setData[6] = 6; setData[7] = 7; CmnChargerParam._CHR00TCPCLIENT.Send5(type, startAddress, num, setData); } #endregion #region 充电信息数据 /// /// 201/221 /// /// /// private void button13_Click(object sender, EventArgs e) { //CmnChargerParam._CHR00TCPCLIENT.Send201(); } /// /// 203 /// /// /// private void button25_Click(object sender, EventArgs e) { //CmnChargerParam._CHR00TCPCLIENT.Send203(); } /// /// 205 /// /// /// private void button11_Click(object sender, EventArgs e) { uint value03 = 0; uint value04 = 0; CmnChargerParam._CHR00TCPCLIENT.Send205(value03, value04); } /// /// 207 /// /// /// private void button31_Click(object sender, EventArgs e) { // CmnChargerParam._CHR00TCPCLIENT.Send207(); } /// /// 209 /// /// /// private void button32_Click(object sender, EventArgs e) { CMD210 CMD210 = new CMD210(); string value05 = ";"; CmnChargerParam._CHR00TCPCLIENT.Send209(CMD210, value05); } #endregion #region 直流充电桩 BMS 信息数据(预留) /// /// 301 /// /// /// private void button33_Click(object sender, EventArgs e) { CmnChargerParam._CHR00TCPCLIENT.Send301(); } /// /// 303 /// /// /// private void button34_Click(object sender, EventArgs e) { CmnChargerParam._CHR00TCPCLIENT.Send303(); } #endregion #region 历史记录 /// /// 401 /// /// /// private void button36_Click(object sender, EventArgs e) { string chargingSerialNum = ""; uint startIndex = 0; CmnChargerParam._CHR00TCPCLIENT.Send401(chargingSerialNum, startIndex); } #endregion #region 升级命令 /// /// 1001 /// /// /// private void button38_Click(object sender, EventArgs e) { int instruct = 0; CmnChargerParam._CHR00TCPCLIENT.Send1001(instruct); } /// /// 1003 /// /// /// private void button39_Click(object sender, EventArgs e) { string fileName = ""; CmnChargerParam._CHR00TCPCLIENT.Send1003(fileName); } /// /// 1005 /// /// /// private void button37_Click(object sender, EventArgs e) { uint fileLen = 0; CmnChargerParam._CHR00TCPCLIENT.Send1005(fileLen); } /// /// 1007 /// /// /// private void button40_Click(object sender, EventArgs e) { string updata = ""; CmnChargerParam._CHR00TCPCLIENT.Send1007(updata); } /// /// 1009 /// /// /// private void button35_Click(object sender, EventArgs e) { CmnChargerParam._CHR00TCPCLIENT.Send1009(); } /// /// 1011 /// /// /// private void button41_Click(object sender, EventArgs e) { CmnChargerParam._CHR00TCPCLIENT.Send1011(); } /// /// 1021 /// /// /// private void button42_Click(object sender, EventArgs e) { uint results = 0; string description = ""; CmnChargerParam._CHR00TCPCLIENT.Send1021(results, description); } #endregion #region 与计费策略相关的指令 /// /// 1101 /// /// /// private void button1_Click(object sender, EventArgs e) { CmnChargerParam._CHR00TCPCLIENT.Send1101(); } /// /// 1103 /// /// /// private void button2_Click(object sender, EventArgs e) { CMD1103 cMD1103 = new CMD1103(); cMD1103.startHour1 = 00; cMD1103.startMinute1 = 00; cMD1103.endHour1 = 10; cMD1103.endMinute1 = 30; cMD1103.rate1 = 120; cMD1103.startHour2 = 10; cMD1103.startMinute2 = 30; cMD1103.endHour2 = 20; cMD1103.endMinute2 = 00; cMD1103.rate2 = 100; cMD1103.startHour3 = 20; cMD1103.startMinute3 = 00; cMD1103.endHour3 = 24; cMD1103.endMinute3 = 00; cMD1103.rate3 = 160; CmnChargerParam._CHR00TCPCLIENT.Send1103(cMD1103); } /// /// 1105 /// /// /// private void button3_Click(object sender, EventArgs e) { CMD1105 cMD1105 = new CMD1105(); cMD1105.startHour1 = 00; cMD1105.startMinute1 = 00; cMD1105.endHour1 = 10; cMD1105.endMinute1 = 30; cMD1105.rate1 = 1000; cMD1105.reserveRate1 = 1000; cMD1105.startHour2 = 10; cMD1105.startMinute2 = 30; cMD1105.endHour2 = 20; cMD1105.endMinute2 = 30; cMD1105.rate2 = 2000; cMD1105.reserveRate2 = 3000; cMD1105.startHour3 = 20; cMD1105.startMinute3 = 30; cMD1105.endHour3 = 24; cMD1105.endMinute3 = 00; cMD1105.rate3 = 1000; cMD1105.reserveRate3 = 1000; CmnChargerParam._CHR00TCPCLIENT.Send1105(cMD1105); } /// /// 1107 /// /// /// private void button4_Click(object sender, EventArgs e) { CmnChargerParam._CHR00TCPCLIENT.Send1107(); } #endregion #region 白名单报文 /// /// 1301 /// /// /// private void button5_Click(object sender, EventArgs e) { CMD1301 cMD1301 = new CMD1301(); cMD1301.chargingStationCode = "0001"; //cMD1301.whitelistType = 1;//充电卡号 cMD1301.whitelistType = 2;//VIN码 cMD1301.operation =4;//查询 //cMD1301.operation =0;//增加 cMD1301.whitelistVersionNum = 10; //每次固定下发10条 cMD1301.carVinOrChargCardNum01 = "11101"; cMD1301.carVinOrChargCardNum02 = "11102"; cMD1301.carVinOrChargCardNum03 = "11103"; cMD1301.carVinOrChargCardNum04 = "11104"; cMD1301.carVinOrChargCardNum05 = "11105"; cMD1301.carVinOrChargCardNum06 = "11106"; cMD1301.carVinOrChargCardNum07 = "11107"; cMD1301.carVinOrChargCardNum08 = "11108"; cMD1301.carVinOrChargCardNum09 = "11109"; cMD1301.carVinOrChargCardNum10 = "11110"; CmnChargerParam._CHR00TCPCLIENT.Send1301(cMD1301); } #endregion #region FTP 远程升级和 FTP 方式日志上传 /// /// 1401 /// /// /// private void button6_Click(object sender, EventArgs e) { CMD1401 cMD1401 = new CMD1401(); cMD1401.UpgradeSoftwareFileVersionNum = "209.02"; cMD1401.ftpServerIPaddress = "16.80.1.20"; cMD1401.ftpUserName = "ftpuserName"; cMD1401.ftpPassWord = "123456"; cMD1401.ftpDownloadPath = "ftp://172.16.4.254/SINEXCEL/dcharg.bin"; cMD1401.executeWay = 1; cMD1401.nowUpgradeDeviceType = 1; CmnChargerParam._CHR00TCPCLIENT.Send1401(cMD1401); } /// /// 1406 /// /// /// private void button7_Click(object sender, EventArgs e) { CMD1406 cMD1406 = new CMD1406(); cMD1406.getLogType = 1; cMD1406.ftpServerIPaddress = "16.80.1.20"; cMD1406.ftpUserName = "ftpuserName"; cMD1406.ftpPassWord = "123456"; cMD1406.ftpSavePath = ""; cMD1406.executeWay = 1; CmnChargerParam._CHR00TCPCLIENT.Send1406(cMD1406); } #endregion #region 协议方式日志上传 /// /// 1502 /// /// /// private void button8_Click(object sender, EventArgs e) { ushort responseCode = 0; CmnChargerParam._CHR00TCPCLIENT.Send1502(responseCode); } /// /// 1504 /// /// /// private void button9_Click(object sender, EventArgs e) { uint logo = 0; uint successReservedMessageNum = 0; CmnChargerParam._CHR00TCPCLIENT.Send1504(logo, successReservedMessageNum); } /// /// 1506 /// /// /// private void button10_Click(object sender, EventArgs e) { CmnChargerParam._CHR00TCPCLIENT.Send1506(); } #endregion private void button12_Click(object sender, EventArgs e) { CMD7 CMD = new CMD7(); CMD.value01 = 0; CMD.value02 = 0; CMD.value03 = 1; CMD.value04 = 2; CMD.value05 = 3; CMD.value06 = 4; CMD.value07 = 5; CMD.value08 = 6; CMD.value09 = 7; CMD.value10 = "123456789"; CMD.value11 = 9; CMD.value12 = 10; CMD.value13 = "12345678"; CMD.value14 = 11; CMD.value15 = 12; CMD.value16 = 13; CmnChargerParam._CHR00TCPCLIENT.Send7(CMD); } private void button17_Click(object sender, EventArgs e) { byte chargeNum = 0; byte sign = 0; CmnChargerParam._CHR00TCPCLIENT.Send113(chargeNum, sign); } } }