Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- string data = m_tbMessage.Text;
- string a = ConvertstringToHex(data);
- 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 };
- 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 .
- And I've try this code.
- string command = "02";
- string command1 = ASCIITOHex("03");
- string command2 = ASCIITOHex("40");
- string terminalId = ASCIITOHex("00011");
- string command3 = ASCIITOHex(";");
- byte[] dataToSend = ASCIIEncoding.ASCII.GetBytes(command + terminalId + command2 + data + command3 + terminalId + command1);
- m_sock.BeginSend(dataToSend, 0, dataToSend.Length, SocketFlags.None, new AsyncCallback(this.OnSend), null);
- But I can't get the value that I want. T should get
- 0x02 0x30 0x30 0x30 0x31 0x31 0x40 0x53 0x68....
- but I get
- 0x02 0x33 0x30 0x33 0x30 0x33 0x33 0x30 0x33 0x31 0x33 0x31 0x33 0x40 0x33 0x53 0x33 0x68....
- 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 System.Net;
- using System.Net.Sockets;
- using System.IO.Ports;
- using System.Threading;
- using System.Runtime.InteropServices;
- delegate void AddMessage(string sNewMessage);
- namespace WindowsFormsApplication1
- {
- public partial class Form1 : Form
- {
- private Socket m_sock; // Server connection
- private byte[] m_byBuff = new byte[256]; // Recieved data buffer
- private event AddMessage m_AddMessage;
- public Form1()
- {
- InitializeComponent();
- m_AddMessage = new AddMessage(OnAddMessage);
- }
- private void button1_Click(object sender, EventArgs e)
- {
- Cursor cursor = Cursor.Current;
- Cursor.Current = Cursors.WaitCursor;
- try
- {
- // Close the socket if it is still open
- if (m_sock != null && m_sock.Connected)
- {
- m_sock.Shutdown(SocketShutdown.Both);
- System.Threading.Thread.Sleep(10);
- m_sock.Close();
- }
- // Create the socket object
- m_sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- // Define the Server address and port
- IPEndPoint epServer = new IPEndPoint(IPAddress.Parse(m_ServerAddress.Text), 155);
- // Connect to the server blocking method and setup callback for recieved data
- // m_sock.Connect( epServer );
- // SetupRecieveCallback( m_sock );
- // Connect to server non-Blocking method
- m_sock.Blocking = false;
- AsyncCallback onconnect = new AsyncCallback(OnConnect);
- m_sock.BeginConnect(epServer, onconnect, m_sock);
- }
- catch (Exception ex)
- {
- MessageBox.Show(this, ex.Message, "Server Connect failed!");
- }
- Cursor.Current = cursor;
- }
- public void OnConnect(IAsyncResult ar)
- {
- // Socket was the passed in object
- Socket sock = (Socket)ar.AsyncState;
- // Check if we were sucessfull
- try
- {
- //sock.EndConnect( ar );
- if (sock.Connected)
- SetupRecieveCallback(sock);
- else
- MessageBox.Show(this, "Unable to connect to remote machine", "Connect Failed!");
- }
- catch (Exception ex)
- {
- MessageBox.Show(this, ex.Message, "Unusual error during Connect!");
- }
- }
- public void OnRecievedData(IAsyncResult ar)
- {
- // Socket was the passed in object
- Socket sock = (Socket)ar.AsyncState;
- // Check if we got any data
- try
- {
- int nBytesRec = sock.EndReceive(ar);
- if (nBytesRec > 0)
- {
- // Wrote the data to the List
- string sRecieved = Encoding.ASCII.GetString(m_byBuff, 0, nBytesRec);
- // WARNING : The following line is NOT thread safe. Invoke is
- // m_lbRecievedData.Items.Add( sRecieved );
- Invoke(m_AddMessage, new string[] { sRecieved });
- enter code here
- // If the connection is still usable restablish the callback
- SetupRecieveCallback(sock);
- }
- else
- {
- // If no data was recieved then the connection is probably dead
- Console.WriteLine("Client {0}, disconnected", sock.RemoteEndPoint);
- sock.Shutdown(SocketShutdown.Both);
- sock.Close();
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(this, ex.Message, "Unusual error druing Recieve!");
- }
- }
- public void OnAddMessage(string sMessage)
- {
- m_lbRecievedData.Items.Add(sMessage);
- }
- public void SetupRecieveCallback(Socket sock)
- {
- try
- {
- AsyncCallback recieveData = new AsyncCallback(OnRecievedData);
- sock.BeginReceive(m_byBuff, 0, m_byBuff.Length, SocketFlags.None, recieveData, sock);
- }
- catch (Exception ex)
- {
- MessageBox.Show(this, ex.Message, "Setup Recieve Callback failed!");
- }
- }
- private void Form1_Closing(object sender, EventArgs e)
- {
- if (m_sock != null && m_sock.Connected)
- {
- m_sock.Shutdown(SocketShutdown.Both);
- m_sock.Close();
- }
- }
- private void button2_Click(object sender, EventArgs e)
- {
- if (m_sock == null || !m_sock.Connected)
- {
- MessageBox.Show(this, "Must be connected to Send a message");
- return;
- }
- // Read the message from the text box and send it
- try
- {
- // Convert to byte array and send.
- Byte[] byteDateLine = Encoding.ASCII.GetBytes(m_tbMessage.Text.ToCharArray());
- //m_sock.Send(byteDateLine, byteDateLine.Length, 0);
- if (m_tbMessage != null)
- {
- string dat = m_tbMessage.Text;
- string a = ConvertStringToHex(dat);
- string command = HEX2ASCII("02");
- string command1 = HEX2ASCII("03");
- string command2 = HEX2ASCII("40");
- string terminalId = ConvertStringToHex("00011");
- string command3 = ASCIITOHex(";");
- byte[] data = ASCIIEncoding.ASCII.GetBytes(command + terminalId + command2 + a + command3 + terminalId + command1);
- byte[] dataToSend = ASCIIEncoding.ASCII.GetBytes(command + terminalId + command2 + a + command3 + terminalId + command1);
- m_sock.BeginSend(dataToSend, 0, dataToSend.Length, SocketFlags.None, new AsyncCallback(this.OnSend), null);
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(this, ex.Message, "Send Message Failed!");
- }
- }
- private void OnSend(IAsyncResult ar)
- {
- try
- {
- m_sock.EndSend(ar);
- }
- catch
- {
- }
- }
- public static string ASCIITOHex(string ascii)
- {
- StringBuilder sb = new StringBuilder();
- byte[] inputBytes = Encoding.UTF8.GetBytes(ascii);
- foreach (byte b in inputBytes)
- {
- sb.Append(string.Format("0x{0:X2}", b));
- }
- return sb.ToString();
- }
- public static string HEX2ASCII(string hex)
- {
- string res = String.Empty;
- for (int a = 0; a < hex.Length; a = a + 2)
- {
- string Char2Convert = hex.Substring(a, 2);
- int n = Convert.ToInt32(Char2Convert, 16);
- char c = (char)n;
- res += c.ToString();
- }
- return res;
- }
- private byte[] HexStringToByteArray(string s)
- {
- s = s.Replace(" ", "");
- byte[] buffer = new byte[s.Length / 2];
- for (int i = 0; i < s.Length; i += 2)
- buffer[i / 2] = (byte)Convert.ToByte(s.Substring(i, 2), 16);
- return buffer;
- }
- public string ConvertStringToHex(string asciiString)
- {
- string hex = "";
- foreach (char c in asciiString)
- {
- int tmp = c;
- hex += String.Format("{0:x2}", (uint)System.Convert.ToUInt32(tmp.ToString()));
- }
- return hex;
- }
- public string ConvertHexToString(string HexValue)
- {
- string StrValue = "";
- while (HexValue.Length > 0)
- {
- StrValue += System.Convert.ToChar(System.Convert.ToUInt32(HexValue.Substring(0, 2), 16)).ToString();
- HexValue = HexValue.Substring(2, HexValue.Length - 2);
- }
- return StrValue;
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- m_tbMessage.Visible = !m_tbMessage.Visible;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement