Click here to Skip to main content
15,887,908 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to print CPCL commands in toshiba printer using c# tcpclient socket programming.

this is my code

System.Net.Sockets.TcpClient client = new System.Net.Sockets.TcpClient();
      client.Connect(ipAddress, port)
      StreamWriter writer = new StreamWriter(client.GetStream())
      string printtemplate="! 0 200 200 300 1

                            PAGE-WIDTH 396

                            T 4 1 10 80 £75

                            T 4 1 140 80 £75

                            T 4 1 280 80 £75

                            FORM

                            PRINT" ;
                            byte[] unicodeBytes =   Encoding.Unicode.GetBytes(printtemplate);
                            byte[] win1252bytes =  Encoding.Convert(Encoding.Unicode, Encoding.GetEncoding(1252), unicodeBytes);
                            writer = new System.IO.StreamWriter(client.GetStream());
                           string win1252String =  Encoding.GetEncoding(1252).GetString(win1252bytes);
                           writer.Write(win1252String);
                         writer.Flush();
                       writer.Close();
                     client.Close();


What I have tried:

when i am trying to execute, it is printing u75 u75 u75 in toshiba printer. output should be £75 £75 £75 also it should support other country currency symbols like euro,doller and all.

pls hele me
Posted
Updated 17-Aug-17 23:00pm

1 solution

Use the BinaryWriter class instead of the StreamWriter class and write the win1252bytes byte array:
C#
byte[] unicodeBytes = Encoding.Unicode.GetBytes(printtemplate);
// EDIT: From comments, the printer support ISO-8859-15 to show the EURO symbol
//byte[] win1252bytes = Encoding.Convert(Encoding.Unicode, Encoding.GetEncoding(1252), unicodeBytes);
byte[] isoBytes = Encoding.Convert(Encoding.Unicode, Encoding.GetEncoding(28605), unicodeBytes);
writer = new System.IO.BinaryWriter(client.GetStream());
// The Write(Byte[]) method will write data as they are
//writer.Write(win1252bytes);
writer.Write(isoBytes);

If the output is then still wrong, ensure that the printer is using the 1252 encoding.
 
Share this answer
 
v2
Comments
Member 8235434 18-Aug-17 5:27am    
Thanks for your reply.

but euro is not printing. it is printing ?75 ?75 ?75. expected output is €75 €75 €75
Jochen Arndt 18-Aug-17 5:40am    
Euro? In your question you are using GBP.

If GBP is working but Euro not, check the manual of your printer. Is it really using Windows-1252 or ISO 8859-1? ISO 8859-1 does not contain all symbols (including Euro) from Windows-1252. If you need the Euro symbol, your printer must support ISO-8859-15.

See https://de.wikipedia.org/wiki/ISO_8859-1.
It shows the code points of the above encodings and explains the differences.

EDIT: Damn, I posted the German link and the English one does not explain the differences.
Member 8235434 18-Aug-17 6:00am    
printer is supporting ISO-8859-15.. but euro is not printing. is there any other encoding to get euro??
Jochen Arndt 18-Aug-17 6:07am    
Then you have to send him ISO-8859-15 encoded data. Just replace 1252 by 28605 (see https://msdn.microsoft.com/en-us/library/system.text.encoding(v=vs.110).aspx for code page numbers).
Member 8235434 18-Aug-17 6:15am    
no luck. same issue.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900