using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; using GWSocketClient.util; using GWSocketClient.xml; using XuanJiSocket; namespace GWSocketClient { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private string ip; private string port; private SocketHelper.TcpClients sockeTcpClients; private VcarecityPD vcarecityProtocolBean = null; private void Form1_Load(object sender, EventArgs e) { XmlLoader xmlLoader = new XmlLoader(); vcarecityProtocolBean = xmlLoader.loadXml(); if (vcarecityProtocolBean != null) { this.Text = vcarecityProtocolBean.ProtocolName + " - " + vcarecityProtocolBean.ProtocolNo + " - " + vcarecityProtocolBean.ProtocolStartSymbol; } SocketHelper.pushSockets = new SocketHelper.PushSockets(receiveData); //注册推送器 sockeTcpClients = new SocketHelper.TcpClients(); ip = tbIpaddress.Text.Trim(); port = tbPort.Text.Trim(); } private bool showTopForNoLoadXML() { if (vcarecityProtocolBean == null) { MessageBox.Show("请先加载XML协议文件"); return false; } return true; } private void receiveData(SocketHelper.Sockets sks) { this.Invoke(new ThreadStart(delegate { if (sks.ex != null) { switch (sks.ErrorCode) { case SocketHelper.Sockets.ErrorCodes.objectNull: break; case SocketHelper.Sockets.ErrorCodes.ConnectError: break; case SocketHelper.Sockets.ErrorCodes.ConnectSuccess: addToListboxItem("状态信息:连接成功.!"); break; case SocketHelper.Sockets.ErrorCodes.TrySendData: break; default: break; } addToListboxItem(string.Format("客户端信息{0}", sks.ex)); } else { byte[] buffer = new byte[sks.Offset]; Array.Copy(sks.RecBuffer, buffer, sks.Offset); string byteToHexStr = HexUtil.byteToHexStr(buffer); if (byteToHexStr.Equals("ServerOff")) { addToListboxItem("状态信息:服务端主动关闭!"); } else { if (!byteToHexStr.Equals("")) { addToListboxItem(string.Format("收到服务端{0}发来消息:[{1}]", sks.Ip, byteToHexStr)); handleResponse(sks, byteToHexStr); } else { addToListboxItem("状态信息:服务端主动关闭!"); sockeTcpClients.Stop(); } // listBoxShow.Text += "\r\n"; // listBoxShow.Text += string.Format("服务端{0}发来消息:{1}", sks.Ip, byteToHexStr); } } })); } //处理回复信息 private void handleResponse(SocketHelper.Sockets sks, string reqData) { string resp = tfTmpSendData.Text.Trim(); if (resp.Equals("")) { resp = "00"; } string functionWord = reqData.Substring(18, 2); int funcode = Utils.hexToTen(functionWord); if (funcode == 16 || funcode == 32) { funcode = funcode + 128; functionWord = Utils.stringWith0(Utils.tenToHex(funcode), 2); string cmd = reqData.Substring(0, 18) + functionWord; string data = reqData.Substring(24, 10) + resp; cmd = cmd + Utils.stringWith0(Utils.tenToHex(data.Length / 2), 4) + data; cmd = Utils.calcCrcCode(cmd); addToListboxItem(string.Format("回复服务端{0}回应消息:[{1}]", sks.Ip, cmd)); byte[] ts = HexUtil.hexToBytes(cmd); sockeTcpClients.SendData(ts); } } private void tbEquipmentAddress_TextChanged(object sender, EventArgs e) { string equipmentAddress = tbEquipmentAddress.Text.Trim(); tsslEquipmentAddress.Text = equipmentAddress.Length / 2.0 + ""; } private void btnLogin_Click(object sender, EventArgs e) { try { if (!showTopForNoLoadXML()) return; ip = tbIpaddress.Text.Trim(); port = tbPort.Text.Trim(); sockeTcpClients.InitSocket(ip, int.Parse(port)); sockeTcpClients.Start(); // string loginCmd = "7e7e7e4442832e833c8200143839383630324231313931353530343739383737f7"; string loginCmd = vcarecityProtocolBean.ProtocolStartSymbol; loginCmd += tbEquipmentAddress.Text.Trim() + "82" + Utils.stringWith0(Utils.tenToHex(tbCCID.Text.Trim().Length / 2), 4); loginCmd += tbCCID.Text.Trim(); loginCmd = Utils.calcCrcCode(loginCmd); //发送登录命令 byte[] ts = HexUtil.hexToBytes(loginCmd); addToListboxItem(string.Format("发送到服务端:[{0}]", loginCmd)); sockeTcpClients.SendData(ts); } catch (Exception ex) { // statuslist.Items.Add(string.Format("连接失败!原因:{0}", ex.Message btnLogin.Enabled = true; Console.WriteLine(ex.Message); } } private void tbCCID_TextChanged(object sender, EventArgs e) { string ccid = tbCCID.Text.Trim(); tsslCCIDLEN.Text = ccid.Length / 2.0 + ""; } private void btnQuickLogout_Click(object sender, EventArgs e) { try { sockeTcpClients.Stop(); } catch (Exception exception) { // addToListboxItem("你还没有登录!!" + exception.Message); } } private void tsmiLoadXml_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "XML文件|*.xml|所有文件|*.*"; if (ofd.ShowDialog() == DialogResult.OK) { string xmlFilepath = ofd.FileName; vcarecityProtocolBean = new XmlLoader().load(xmlFilepath).getVcarecityPD(); } } private void btnSendData_Click(object sender, EventArgs e) { string trim = tfTmpSendData.Text.Trim(); trim = trim.Replace("\n", "").Replace("\r", "").Replace("\t", "").Replace(" ", "").Replace("-", ""); if (!trim.Equals("")) { byte[] ts = HexUtil.hexToBytes(trim); addToListboxItem(string.Format("发送到服务端:[{0}]", trim)); sockeTcpClients.SendData(ts); } } private void rightMenuCopyCmd_Click(object sender, EventArgs e) { int index = listBoxShow.SelectedIndex; if (index >= 0 && index < listBoxShow.Items.Count) { string info = listBoxShow.SelectedItem.ToString(); if (!info.Equals("")) { if (info.Contains("[") || info.Contains("]")) { Regex reg = new Regex(@"\[\w+\]"); //注意里面的引号 要用双引号表示,而不是用反斜 Match match = reg.Match(info); string ifs = match.ToString(); ifs = ifs.Replace("[", "").Replace("]", ""); if (!info.Equals("")) { Clipboard.SetDataObject(ifs); } else { Clipboard.SetDataObject(info); } } else { Clipboard.SetDataObject(info); } } } } private void addToListboxItem(object info) { if (listBoxShow.Items.Count > 1000) listBoxShow.Items.Clear(); Console.WriteLine(info); listBoxShow.Items.Add(info); listBoxShow.TopIndex = listBoxShow.Items.Count - 1; } private void rightMenuClearInfo_Click(object sender, EventArgs e) { listBoxShow.Items.Clear(); } private void listBoxShow_DrawItem(object sender, DrawItemEventArgs e) { if (e.Index >= 0 && e.Index < listBoxShow.Items.Count) { e.DrawBackground(); // string res = listBoxShow.Items[e.Index].ToString().ToLower(); if (res.Contains("收到服务端")) { Font f = new Font("微软雅黑", e.Font.Size, FontStyle.Bold); e.Graphics.DrawString(((ListBox) sender).Items[e.Index].ToString(), f, new SolidBrush(Color.Blue), e.Bounds); } else if (res.Contains("发送到服务端") || res.Contains("回复")) { Font f = new Font("微软雅黑", e.Font.Size, FontStyle.Italic); e.Graphics.DrawString(((ListBox) sender).Items[e.Index].ToString(), f, new SolidBrush(Color.BlueViolet), e.Bounds); } else { e.Graphics.DrawString(((ListBox) sender).Items[e.Index].ToString(), e.Font, new SolidBrush(e.ForeColor), e.Bounds); } e.DrawFocusRectangle(); } } } }