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 = 0;
uint startAddress = 2;
byte num = 3;
byte[] setData = null;
CmnChargerParam._CHR00TCPCLIENT.Send1(type, startAddress, num, setData);
}
///
/// (CMD=3)后台服务器下发充电桩字符型参数
///
///
///
private void button15_Click(object sender, EventArgs e)
{
byte type = 0;
uint startAddress = 1;
byte[] setData = null;
CmnChargerParam._CHR00TCPCLIENT.Send3(type, startAddress, setData);
}
///
/// (CMD=5)后台服务器下发充电桩控制命令
///
///
///
private void button16_Click(object sender, EventArgs e)
{
byte type = 0;
uint startAddress = 9;
byte num = 10;
byte[] setData = null;
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();
CmnChargerParam._CHR00TCPCLIENT.Send1103(cMD1103);
}
///
/// 1105
///
///
///
private void button3_Click(object sender, EventArgs e)
{
CMD1105 cMD1105 = new CMD1105();
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();
CmnChargerParam._CHR00TCPCLIENT.Send1301(cMD1301);
}
#endregion
#region FTP 远程升级和 FTP 方式日志上传
///
/// 1401
///
///
///
private void button6_Click(object sender, EventArgs e)
{
CMD1401 cMD1401 = new CMD1401();
CmnChargerParam._CHR00TCPCLIENT.Send1401(cMD1401);
}
///
/// 1406
///
///
///
private void button7_Click(object sender, EventArgs e)
{
CMD1406 cMD1406 = new CMD1406();
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 = 8;
CMD.value11 = 9;
CMD.value12 = 10;
CMD.value13 = "12345678";
CMD.value14 = 11;
CMD.value15 = 12;
CMD.value16 = 13;
CmnChargerParam._CHR00TCPCLIENT.Send7(CMD);
}
}
}