I want to send code to the mobile label printer via mobile application.
I have to do this with TCP connection.
While tcp connection and printing is OK with the windows desktop application,
It just does nothing with the mobile application.
Once I click the connect button it connects to the printer and sends the text without problem
but the printer does not respond!
Appreciate any suggestion
Here is the code
private static NetworkStream stream;
private static TcpClient client;
if (client == null)
{
client = new TcpClient();
int port = int.Parse(strPort);
client.Connect(server, port);
}
stream = client.GetStream();
StreamWriter writer = new StreamWriter(stream, Encoding.GetEncoding("Windows-1251"));
writer.AutoFlush = false;
writer.Write(Encoding.GetEncoding("Windows-1251").GetBytes(message).Length);
writer.Write(message);
writer.Flush();
First thought:
Is it possible that the printer requires a delimiter between the messages? How does the printer know where the expected length ends and where the actual message begins?
For example, if the string to print is
12345, how does the printer know from the TCP message512345whether to expect 5 characters or 51 or 512, etc.?Also I think that it is possible that
Encoding.GetEncoding("Windows-1251").GetBytes(message).Lengthdelivers a number different from the number of characters withinmessage, ifmessageis not in encodingWindows-1251.What you’d have to do is:
messageto a byte arraySample:
Finally (!), you should really look into closing the resources you open up!