views:

251

answers:

3

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?

+3  A: 

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

Draemon
+1  A: 

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.

tommieb75
Thanks, Tom! In case of reading as a binary file, how I could turn "byte[] data" back to string, before sending to a printer?
rem
Regarding StreamReader reading only one line (do you think it's due to "\r" symbols?), I just have to note, that when showing string after reading to a MessageBox, it looks as entire string put to a .txt file, without any omittions.
rem
@rem: Please see the edited part of the answer... :) as for the '\r', the standard end of line is '\r\n', would be better to use System.Environment.Newline instead of the hard-coded version, to make it more robust and platform portable.
tommieb75
Reading the bytes by using BinaryReader wrapping a StreamReader using ASCII is a bad idea. If you want to read *binary* data, just use a Stream - don't use a StreamReader at all.
Jon Skeet
@Jon: Oh...well spotted...gosh...I'm a bad influence, now rem has probably taken my code on board and using it....<g>
tommieb75
@tommieb75 It's OK. No problem. Thank you for your help!
rem
+1  A: 

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.

Hans Passant
@nobugz Thanks! Do you mean, that I shouldn't edit this .txt file by simply hand typing? Is hand editing of this file the reason of the problem?
rem
Erm, that could well be. It is usually hard to stuff the \x02 control code into the file with a text editor. I wouldn't know how to do it with Notepad or Visual Studio's editor. No, typing "\x02" doesn't do it.
Hans Passant