Form1.cs 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Text.RegularExpressions;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12. using GWSocketClient.util;
  13. using GWSocketClient.xml;
  14. using XuanJiSocket;
  15. namespace GWSocketClient
  16. {
  17. public partial class Form1 : Form
  18. {
  19. public Form1()
  20. {
  21. InitializeComponent();
  22. }
  23. private string ip;
  24. private string port;
  25. private SocketHelper.TcpClients sockeTcpClients;
  26. private VcarecityPD vcarecityProtocolBean = null;
  27. private void Form1_Load(object sender, EventArgs e)
  28. {
  29. XmlLoader xmlLoader = new XmlLoader();
  30. vcarecityProtocolBean = xmlLoader.loadXml();
  31. if (vcarecityProtocolBean != null)
  32. {
  33. this.Text = vcarecityProtocolBean.ProtocolName + " - " + vcarecityProtocolBean.ProtocolNo + " - " +
  34. vcarecityProtocolBean.ProtocolStartSymbol;
  35. }
  36. SocketHelper.pushSockets = new SocketHelper.PushSockets(receiveData); //注册推送器
  37. sockeTcpClients = new SocketHelper.TcpClients();
  38. ip = tbIpaddress.Text.Trim();
  39. port = tbPort.Text.Trim();
  40. }
  41. private bool showTopForNoLoadXML()
  42. {
  43. if (vcarecityProtocolBean == null)
  44. {
  45. MessageBox.Show("请先加载XML协议文件");
  46. return false;
  47. }
  48. return true;
  49. }
  50. private void receiveData(SocketHelper.Sockets sks)
  51. {
  52. this.Invoke(new ThreadStart(delegate
  53. {
  54. if (sks.ex != null)
  55. {
  56. switch (sks.ErrorCode)
  57. {
  58. case SocketHelper.Sockets.ErrorCodes.objectNull:
  59. break;
  60. case SocketHelper.Sockets.ErrorCodes.ConnectError:
  61. break;
  62. case SocketHelper.Sockets.ErrorCodes.ConnectSuccess:
  63. addToListboxItem("状态信息:连接成功.!");
  64. break;
  65. case SocketHelper.Sockets.ErrorCodes.TrySendData:
  66. break;
  67. default:
  68. break;
  69. }
  70. addToListboxItem(string.Format("客户端信息{0}", sks.ex));
  71. }
  72. else
  73. {
  74. byte[] buffer = new byte[sks.Offset];
  75. Array.Copy(sks.RecBuffer, buffer, sks.Offset);
  76. string byteToHexStr = HexUtil.byteToHexStr(buffer);
  77. if (byteToHexStr.Equals("ServerOff"))
  78. {
  79. addToListboxItem("状态信息:服务端主动关闭!");
  80. }
  81. else
  82. {
  83. if (!byteToHexStr.Equals(""))
  84. {
  85. addToListboxItem(string.Format("收到服务端{0}发来消息:[{1}]", sks.Ip, byteToHexStr));
  86. handleResponse(sks, byteToHexStr);
  87. }
  88. else
  89. {
  90. addToListboxItem("状态信息:服务端主动关闭!");
  91. sockeTcpClients.Stop();
  92. }
  93. // listBoxShow.Text += "\r\n";
  94. // listBoxShow.Text += string.Format("服务端{0}发来消息:{1}", sks.Ip, byteToHexStr);
  95. }
  96. }
  97. }));
  98. }
  99. //处理回复信息
  100. private void handleResponse(SocketHelper.Sockets sks, string reqData)
  101. {
  102. string resp = tfTmpSendData.Text.Trim();
  103. if (resp.Equals(""))
  104. {
  105. resp = "00";
  106. }
  107. string functionWord = reqData.Substring(18, 2);
  108. int funcode = Utils.hexToTen(functionWord);
  109. if (funcode == 16 || funcode == 32)
  110. {
  111. funcode = funcode + 128;
  112. functionWord = Utils.stringWith0(Utils.tenToHex(funcode), 2);
  113. string cmd = reqData.Substring(0, 18) + functionWord;
  114. string data = reqData.Substring(24, 10) + resp;
  115. cmd = cmd + Utils.stringWith0(Utils.tenToHex(data.Length / 2), 4) + data;
  116. cmd = Utils.calcCrcCode(cmd);
  117. addToListboxItem(string.Format("回复服务端{0}回应消息:[{1}]", sks.Ip, cmd));
  118. byte[] ts = HexUtil.hexToBytes(cmd);
  119. sockeTcpClients.SendData(ts);
  120. }
  121. }
  122. private void tbEquipmentAddress_TextChanged(object sender, EventArgs e)
  123. {
  124. string equipmentAddress = tbEquipmentAddress.Text.Trim();
  125. tsslEquipmentAddress.Text = equipmentAddress.Length / 2.0 + "";
  126. }
  127. private void btnLogin_Click(object sender, EventArgs e)
  128. {
  129. try
  130. {
  131. if (!showTopForNoLoadXML())
  132. return;
  133. ip = tbIpaddress.Text.Trim();
  134. port = tbPort.Text.Trim();
  135. sockeTcpClients.InitSocket(ip, int.Parse(port));
  136. sockeTcpClients.Start();
  137. // string loginCmd = "7e7e7e4442832e833c8200143839383630324231313931353530343739383737f7";
  138. string loginCmd = vcarecityProtocolBean.ProtocolStartSymbol;
  139. loginCmd += tbEquipmentAddress.Text.Trim() + "82" +
  140. Utils.stringWith0(Utils.tenToHex(tbCCID.Text.Trim().Length / 2), 4);
  141. loginCmd += tbCCID.Text.Trim();
  142. loginCmd = Utils.calcCrcCode(loginCmd);
  143. //发送登录命令
  144. byte[] ts = HexUtil.hexToBytes(loginCmd);
  145. addToListboxItem(string.Format("发送到服务端:[{0}]", loginCmd));
  146. sockeTcpClients.SendData(ts);
  147. }
  148. catch (Exception ex)
  149. {
  150. // statuslist.Items.Add(string.Format("连接失败!原因:{0}", ex.Message
  151. btnLogin.Enabled = true;
  152. Console.WriteLine(ex.Message);
  153. }
  154. }
  155. private void tbCCID_TextChanged(object sender, EventArgs e)
  156. {
  157. string ccid = tbCCID.Text.Trim();
  158. tsslCCIDLEN.Text = ccid.Length / 2.0 + "";
  159. }
  160. private void btnQuickLogout_Click(object sender, EventArgs e)
  161. {
  162. try
  163. {
  164. sockeTcpClients.Stop();
  165. }
  166. catch (Exception exception)
  167. {
  168. // addToListboxItem("你还没有登录!!" + exception.Message);
  169. }
  170. }
  171. private void tsmiLoadXml_Click(object sender, EventArgs e)
  172. {
  173. OpenFileDialog ofd = new OpenFileDialog();
  174. ofd.Filter = "XML文件|*.xml|所有文件|*.*";
  175. if (ofd.ShowDialog() == DialogResult.OK)
  176. {
  177. string xmlFilepath = ofd.FileName;
  178. vcarecityProtocolBean = new XmlLoader().load(xmlFilepath).getVcarecityPD();
  179. }
  180. }
  181. private void btnSendData_Click(object sender, EventArgs e)
  182. {
  183. string trim = tfTmpSendData.Text.Trim();
  184. trim = trim.Replace("\n", "").Replace("\r", "").Replace("\t", "").Replace(" ", "").Replace("-", "");
  185. if (!trim.Equals(""))
  186. {
  187. byte[] ts = HexUtil.hexToBytes(trim);
  188. addToListboxItem(string.Format("发送到服务端:[{0}]", trim));
  189. sockeTcpClients.SendData(ts);
  190. }
  191. }
  192. private void rightMenuCopyCmd_Click(object sender, EventArgs e)
  193. {
  194. int index = listBoxShow.SelectedIndex;
  195. if (index >= 0 && index < listBoxShow.Items.Count)
  196. {
  197. string info = listBoxShow.SelectedItem.ToString();
  198. if (!info.Equals(""))
  199. {
  200. if (info.Contains("[") || info.Contains("]"))
  201. {
  202. Regex reg = new Regex(@"\[\w+\]"); //注意里面的引号 要用双引号表示,而不是用反斜
  203. Match match = reg.Match(info);
  204. string ifs = match.ToString();
  205. ifs = ifs.Replace("[", "").Replace("]", "");
  206. if (!info.Equals(""))
  207. {
  208. Clipboard.SetDataObject(ifs);
  209. }
  210. else
  211. {
  212. Clipboard.SetDataObject(info);
  213. }
  214. }
  215. else
  216. {
  217. Clipboard.SetDataObject(info);
  218. }
  219. }
  220. }
  221. }
  222. private void addToListboxItem(object info)
  223. {
  224. if (listBoxShow.Items.Count > 1000)
  225. listBoxShow.Items.Clear();
  226. Console.WriteLine(info);
  227. listBoxShow.Items.Add(info);
  228. listBoxShow.TopIndex = listBoxShow.Items.Count - 1;
  229. }
  230. private void rightMenuClearInfo_Click(object sender, EventArgs e)
  231. {
  232. listBoxShow.Items.Clear();
  233. }
  234. private void listBoxShow_DrawItem(object sender, DrawItemEventArgs e)
  235. {
  236. if (e.Index >= 0 && e.Index < listBoxShow.Items.Count)
  237. {
  238. e.DrawBackground();
  239. //
  240. string res = listBoxShow.Items[e.Index].ToString().ToLower();
  241. if (res.Contains("收到服务端"))
  242. {
  243. Font f = new Font("微软雅黑", e.Font.Size, FontStyle.Bold);
  244. e.Graphics.DrawString(((ListBox) sender).Items[e.Index].ToString(), f, new SolidBrush(Color.Blue),
  245. e.Bounds);
  246. }
  247. else if (res.Contains("发送到服务端") || res.Contains("回复"))
  248. {
  249. Font f = new Font("微软雅黑", e.Font.Size, FontStyle.Italic);
  250. e.Graphics.DrawString(((ListBox) sender).Items[e.Index].ToString(), f,
  251. new SolidBrush(Color.BlueViolet),
  252. e.Bounds);
  253. }
  254. else
  255. {
  256. e.Graphics.DrawString(((ListBox) sender).Items[e.Index].ToString(), e.Font,
  257. new SolidBrush(e.ForeColor),
  258. e.Bounds);
  259. }
  260. e.DrawFocusRectangle();
  261. }
  262. }
  263. }
  264. }