Hi All,
I needed to send hex data to epson printer as below. This command is to print barcode.
1D 6b 43 0c 36 31 32 33 34 35 33 37 37 30 39 31
For this example, the highlighted part are 12 digit I need to send.
I've prepared below program which works just fine.
static void Main(string[] args)
{
SendSampleData();
}
private static void SendSampleData()
{
SerialPort port = new SerialPort(
"COM1", 9600, Parity.None, 8, StopBits.One);
// Open the port for communications
port.Open();
//1D 6b 43 0c 36 31 32 33 34 35 33 37 37 30 39 31
port.Write(new byte[] { 0x1D, 0x6b, 0x43, 0x0c,
0x36, 0x31, 0x32, 0x33, 0x34, 0x35, 0x33, 0x37, 0x37, 0x30, 0x39, 0x31},
0, 16);
port.Write("612345377091");
port.Write(new byte[] { 0x0A }, 0, 1);
port.Close();
}
But now I wanted to simplify it to work as in code below... basically I imagine it so that I only need to adjust the barCodeString "6123453770918" value and the subsequent code will handle it to send the command like shown above.
I imagine that the code need to convert the string it to hex value and have each assigned to an array. The array will then be used to send a command to the printer.
I am still new to .NET and C#. I will appreciate any help to get a working code.
private static void SendSampleData()
{
string barCodeString = "6123453770918"; //12 digit but not fixed
byte[] bHex;
SerialPort port = new SerialPort(
"COM1", 9600, Parity.None, 8, StopBits.One);
// Open the port for communications
port.Open();
stringToHexArray(barCodeString); //I dont have the code //assign ascii string to bHex[] byte array
//1D 6b 43 0c 36 31 32 33 34 35 33 37 37 30 39 31
port.Write(new byte[] { 0x1D, 0x6b, 0x43, 0x0c,
bHex[0], bHex[1], bHex[2], bHex[3], bHex[4], bHex[5], bHex[6], bHex[7], bHex[8], bHex[9], bHex[10], bHex[11]},
0, 16);
port.Write("612345377091");
port.Write(new byte[] { 0x0A }, 0, 1);
port.Close();
}
I've searched the net for ascii to hex conversion, but main problem I have is that I dont know C# and .NET. I will appreciate your kind help in getting a code that works and I will try to work backward to understand the program back... :(
Loading
VulpesPosted Nov 21, 2011, 5:21 AM
aminPosted Nov 21, 2011, 8:33 PM
byte[] bHex = Encoding.ASCII.GetBytes(barCodeString);
aminPosted Nov 21, 2011, 5:16 AM
For example, 612345377091 will convert to:
bHex[0] = 36; //character 6 is hex 36
bHex[1] = 31; //character 1 is hex 31
bHex[2] = 32; //character 2 is hex 32
bHex[3] = 33; //character 3 is hex 33
bHex[4] = 34; //character 4 is hex 34
bHex[5] = 35; //character 5 is hex 35
bHex[6] = 33; //character 3 is hex 33
bHex[7] = 37; //character 7 is hex 37
bHex[8] = 37; //character 7 is hex 37
bHex[9] = 30; //character 0 is hex 30
bHex[10] = 39; //character 9 is hex 39
bHex[11] = 31; //character 1 is hex 31
I've tried to modify the code as below,
static byte[] StringToHexArray(string s)
{
// byte[] ba = new byte[s.Length / 2];
byte[] ba = new byte[s.Length];
for (int i = 0; i < ba.Length; i++)
{
// string temp = s.Substring(i * 2, 1);
string temp = s.Substring(i, 1);
ba[i] = Convert.ToByte(temp, 16);
}
return ba;
}
Output:
======
06 01 02 03 04 05 03 07 07 00 09 01
However, based on the program you provided I think I need the output to be a hex value as below:
36 31 32 33 34 35 33 37 37 30 39 31
I think there is a possibility that the input will not be 0-9 range of data, but maybe other ASCII character too. So I will be hoping for a generalize ascii to hex conversion. Each ascii character converted will need to be assigned to an array.
VulpesPosted Nov 21, 2011, 4:46 AM