Advertisement
Guest User

Untitled

a guest
Sep 30th, 2014
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.79 KB | None | 0 0
  1. string data = m_tbMessage.Text;
  2. string a = ConvertstringToHex(data);
  3.  
  4. byte[] cmd_setshop = { 0x02, 0x30, 0x30, 0x30, 0x31, 0x31, 0x40, 0x53, 0x68, 0x6f, 0x70, 0x20, 0x4B, 0x4D, 0x55, 0x54, 0x54, 0x3B, 0x30, 0x30, 0x30, 0x31, 0x31, 0x03 };
  5.  
  6. I don't know how to add string a to this command. I have to send the command like this but for the m_tbMessage.Text I have to convert it first before add to this byte. So If it possible to send byte that contain string and hex .
  7.  
  8.  
  9.  
  10.  
  11. And I've try this code.
  12.  
  13. string command = "02";
  14. string command1 = ASCIITOHex("03");
  15. string command2 = ASCIITOHex("40");
  16. string terminalId = ASCIITOHex("00011");
  17. string command3 = ASCIITOHex(";");
  18.  
  19. byte[] dataToSend = ASCIIEncoding.ASCII.GetBytes(command + terminalId + command2 + data + command3 + terminalId + command1);
  20.  
  21. m_sock.BeginSend(dataToSend, 0, dataToSend.Length, SocketFlags.None, new AsyncCallback(this.OnSend), null);
  22.  
  23. But I can't get the value that I want. T should get
  24. 0x02 0x30 0x30 0x30 0x31 0x31 0x40 0x53 0x68....
  25. but I get
  26. 0x02 0x33 0x30 0x33 0x30 0x33 0x33 0x30 0x33 0x31 0x33 0x31 0x33 0x40 0x33 0x53 0x33 0x68....
  27.  
  28. using System;
  29. using System.Collections.Generic;
  30. using System.ComponentModel;
  31. using System.Data;
  32. using System.Drawing;
  33. using System.Linq;
  34. using System.Text;
  35. using System.Threading.Tasks;
  36. using System.Windows.Forms;
  37. using System.Net;
  38. using System.Net.Sockets;
  39. using System.IO.Ports;
  40. using System.Threading;
  41. using System.Runtime.InteropServices;
  42.  
  43. delegate void AddMessage(string sNewMessage);
  44.  
  45.  
  46. namespace WindowsFormsApplication1
  47. {
  48. public partial class Form1 : Form
  49. {
  50. private Socket m_sock; // Server connection
  51. private byte[] m_byBuff = new byte[256]; // Recieved data buffer
  52. private event AddMessage m_AddMessage;
  53.  
  54.  
  55. public Form1()
  56. {
  57. InitializeComponent();
  58. m_AddMessage = new AddMessage(OnAddMessage);
  59.  
  60.  
  61. }
  62.  
  63. private void button1_Click(object sender, EventArgs e)
  64. {
  65. Cursor cursor = Cursor.Current;
  66. Cursor.Current = Cursors.WaitCursor;
  67. try
  68. {
  69. // Close the socket if it is still open
  70. if (m_sock != null && m_sock.Connected)
  71. {
  72. m_sock.Shutdown(SocketShutdown.Both);
  73. System.Threading.Thread.Sleep(10);
  74. m_sock.Close();
  75. }
  76.  
  77. // Create the socket object
  78. m_sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  79.  
  80. // Define the Server address and port
  81. IPEndPoint epServer = new IPEndPoint(IPAddress.Parse(m_ServerAddress.Text), 155);
  82.  
  83. // Connect to the server blocking method and setup callback for recieved data
  84. // m_sock.Connect( epServer );
  85. // SetupRecieveCallback( m_sock );
  86.  
  87. // Connect to server non-Blocking method
  88. m_sock.Blocking = false;
  89. AsyncCallback onconnect = new AsyncCallback(OnConnect);
  90. m_sock.BeginConnect(epServer, onconnect, m_sock);
  91. }
  92. catch (Exception ex)
  93. {
  94. MessageBox.Show(this, ex.Message, "Server Connect failed!");
  95. }
  96. Cursor.Current = cursor;
  97. }
  98.  
  99. public void OnConnect(IAsyncResult ar)
  100. {
  101. // Socket was the passed in object
  102. Socket sock = (Socket)ar.AsyncState;
  103.  
  104. // Check if we were sucessfull
  105. try
  106. {
  107. //sock.EndConnect( ar );
  108. if (sock.Connected)
  109. SetupRecieveCallback(sock);
  110. else
  111. MessageBox.Show(this, "Unable to connect to remote machine", "Connect Failed!");
  112. }
  113. catch (Exception ex)
  114. {
  115. MessageBox.Show(this, ex.Message, "Unusual error during Connect!");
  116. }
  117. }
  118.  
  119. public void OnRecievedData(IAsyncResult ar)
  120. {
  121. // Socket was the passed in object
  122. Socket sock = (Socket)ar.AsyncState;
  123.  
  124. // Check if we got any data
  125. try
  126. {
  127. int nBytesRec = sock.EndReceive(ar);
  128. if (nBytesRec > 0)
  129. {
  130. // Wrote the data to the List
  131. string sRecieved = Encoding.ASCII.GetString(m_byBuff, 0, nBytesRec);
  132.  
  133.  
  134. // WARNING : The following line is NOT thread safe. Invoke is
  135. // m_lbRecievedData.Items.Add( sRecieved );
  136. Invoke(m_AddMessage, new string[] { sRecieved });
  137. enter code here
  138.  
  139. // If the connection is still usable restablish the callback
  140. SetupRecieveCallback(sock);
  141. }
  142. else
  143. {
  144. // If no data was recieved then the connection is probably dead
  145. Console.WriteLine("Client {0}, disconnected", sock.RemoteEndPoint);
  146. sock.Shutdown(SocketShutdown.Both);
  147. sock.Close();
  148. }
  149. }
  150. catch (Exception ex)
  151. {
  152. MessageBox.Show(this, ex.Message, "Unusual error druing Recieve!");
  153. }
  154. }
  155.  
  156. public void OnAddMessage(string sMessage)
  157. {
  158. m_lbRecievedData.Items.Add(sMessage);
  159.  
  160.  
  161. }
  162.  
  163. public void SetupRecieveCallback(Socket sock)
  164. {
  165. try
  166. {
  167. AsyncCallback recieveData = new AsyncCallback(OnRecievedData);
  168. sock.BeginReceive(m_byBuff, 0, m_byBuff.Length, SocketFlags.None, recieveData, sock);
  169. }
  170. catch (Exception ex)
  171. {
  172. MessageBox.Show(this, ex.Message, "Setup Recieve Callback failed!");
  173. }
  174. }
  175.  
  176. private void Form1_Closing(object sender, EventArgs e)
  177. {
  178. if (m_sock != null && m_sock.Connected)
  179. {
  180. m_sock.Shutdown(SocketShutdown.Both);
  181. m_sock.Close();
  182. }
  183. }
  184.  
  185. private void button2_Click(object sender, EventArgs e)
  186. {
  187. if (m_sock == null || !m_sock.Connected)
  188. {
  189. MessageBox.Show(this, "Must be connected to Send a message");
  190. return;
  191. }
  192.  
  193. // Read the message from the text box and send it
  194. try
  195. {
  196. // Convert to byte array and send.
  197. Byte[] byteDateLine = Encoding.ASCII.GetBytes(m_tbMessage.Text.ToCharArray());
  198. //m_sock.Send(byteDateLine, byteDateLine.Length, 0);
  199.  
  200.  
  201. if (m_tbMessage != null)
  202. {
  203.  
  204. string dat = m_tbMessage.Text;
  205.  
  206. string a = ConvertStringToHex(dat);
  207.  
  208. string command = HEX2ASCII("02");
  209.  
  210. string command1 = HEX2ASCII("03");
  211. string command2 = HEX2ASCII("40");
  212. string terminalId = ConvertStringToHex("00011");
  213. string command3 = ASCIITOHex(";");
  214.  
  215. byte[] data = ASCIIEncoding.ASCII.GetBytes(command + terminalId + command2 + a + command3 + terminalId + command1);
  216.  
  217. byte[] dataToSend = ASCIIEncoding.ASCII.GetBytes(command + terminalId + command2 + a + command3 + terminalId + command1);
  218. m_sock.BeginSend(dataToSend, 0, dataToSend.Length, SocketFlags.None, new AsyncCallback(this.OnSend), null);
  219. }
  220. }
  221. catch (Exception ex)
  222. {
  223. MessageBox.Show(this, ex.Message, "Send Message Failed!");
  224. }
  225. }
  226.  
  227. private void OnSend(IAsyncResult ar)
  228. {
  229. try
  230. {
  231. m_sock.EndSend(ar);
  232. }
  233. catch
  234. {
  235.  
  236. }
  237.  
  238. }
  239.  
  240. public static string ASCIITOHex(string ascii)
  241. {
  242.  
  243. StringBuilder sb = new StringBuilder();
  244.  
  245. byte[] inputBytes = Encoding.UTF8.GetBytes(ascii);
  246.  
  247. foreach (byte b in inputBytes)
  248. {
  249.  
  250. sb.Append(string.Format("0x{0:X2}", b));
  251.  
  252. }
  253.  
  254. return sb.ToString();
  255.  
  256. }
  257.  
  258. public static string HEX2ASCII(string hex)
  259. {
  260.  
  261. string res = String.Empty;
  262.  
  263. for (int a = 0; a < hex.Length; a = a + 2)
  264. {
  265.  
  266. string Char2Convert = hex.Substring(a, 2);
  267.  
  268. int n = Convert.ToInt32(Char2Convert, 16);
  269.  
  270. char c = (char)n;
  271.  
  272. res += c.ToString();
  273.  
  274. }
  275.  
  276. return res;
  277.  
  278. }
  279.  
  280. private byte[] HexStringToByteArray(string s)
  281. {
  282. s = s.Replace(" ", "");
  283. byte[] buffer = new byte[s.Length / 2];
  284. for (int i = 0; i < s.Length; i += 2)
  285. buffer[i / 2] = (byte)Convert.ToByte(s.Substring(i, 2), 16);
  286. return buffer;
  287. }
  288. public string ConvertStringToHex(string asciiString)
  289. {
  290. string hex = "";
  291. foreach (char c in asciiString)
  292. {
  293. int tmp = c;
  294. hex += String.Format("{0:x2}", (uint)System.Convert.ToUInt32(tmp.ToString()));
  295. }
  296. return hex;
  297. }
  298.  
  299.  
  300.  
  301. public string ConvertHexToString(string HexValue)
  302. {
  303. string StrValue = "";
  304. while (HexValue.Length > 0)
  305. {
  306. StrValue += System.Convert.ToChar(System.Convert.ToUInt32(HexValue.Substring(0, 2), 16)).ToString();
  307. HexValue = HexValue.Substring(2, HexValue.Length - 2);
  308. }
  309. return StrValue;
  310. }
  311.  
  312. private void Form1_Load(object sender, EventArgs e)
  313. {
  314. m_tbMessage.Visible = !m_tbMessage.Visible;
  315. }
  316.  
  317.  
  318.  
  319.  
  320. }
  321. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement