开发者

ASCII raw symbols to control a printer from a .txt file

A label printer is controled by sending a string of raw ASCII characters (which formats a label). Like this:

 string s = "\x02L\r" + "D11\r" + "ySWR\r" + "421100001100096" + date + "\r" + "421100002150096" + time + "\r" + "421100001200160" + price +开发者_运维百科 "\r" + "E\r";

 RawPrinterHelper.SendStringToPrinter(printerName, s);

This hardcoded variant works well.

Now I want to put the control string to a .txt file and read it during runtime. Like this:

        string printstr;
        TextReader tr = new StreamReader("print.txt");
        printstr = tr.ReadLine();
        tr.Close();

But in this case printer prints nothing.

It seems, that StreamReader adds something else to this string

(If I put the read string to a MessageBox.Show(printstr); everything looks OK. Though, this way we can not see control characters added).

What could be a solution to this problem?


Your code calls tr.ReadLine() once, but it looks like you have multiple lines in that string.


Looks like a Zebra label printer, I've had the displeasure. The first thing you need to fix is the way you generate the print.txt file. You'll need to write one line for each section of the command string that's terminated with \r. For example, your command string should be written like this:

printFile.WriteLine("\x02L");
printFile.WriteLine("D11");
printFile.WriteLine("ySWR");
printFile.WriteLine("421100001100096" + date);
printFile.WriteLine("421100002150096" + time);
printFile.WriteLine("421100001200160" + price);
printFile.WriteLine("E");
printFile.WriteLine();

Now you can use ReadLine() when you read the label from print.txt. You'll need to read multiple lines to get the complete label. I added a blank line at the end, you could use that when you read the file to detect that you got all the lines that creates the label. Don't forget to append "\r" again when you send it to the printer.


It could be that the StreamReader is reading it in an Unicode format. By the way, you are reading in only just one line...you need to iterate the lines instead...Your best bet would be to do it this way:

string printstr;
TextReader tr = new StreamReader("print.txt",System.Text.Encoding.ASCII);
printstr = tr.ReadToEnd();
tr.Close();

Or read it as a binary file and read the whole chunk into a series of bytes instead, error checking is omitted.

System.IO.BinaryReader br = new System.IO.BinaryReader(new StreamReader("print.txt", System.Text.Encoding.ASCII));
byte[] data = br.ReadBytes(br.BaseStream.Length);
br.Close();

Edit: After rem's comment I thought it best to include this additional snippet here...this follows on from the previous snippet where the variable data is referenced...

string sData = System.Text.Encoding.ASCII.GetString(data);

Hope this helps, Best regards, Tom.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜