I have a POS-80C thermal printer, printing to it using PrinterUtility Library from NuGet. This library converts a string to ByteArray and sends it to the printer directly, so it's RAW printing. it works really well with Latin characters, but not with Hebrew or Arabic. it Prints "???" when the characters are not in English. I know that I must use ESC/POS commands to switch to for example Arabic character sets when my text is in Arabic but I tried a lot and figure out how to do so.
PrinterUtility.EscPosEpsonCommands.EscPosEpson obj = new PrinterUtility.EscPosEpsonCommands.EscPosEpson();
var BytesValue = Encoding.ASCII.GetBytes(string.Empty);
BytesValue = PrintExtensions.AddBytes(BytesValue, Encoding.ASCII.GetBytes("English Text")); //works fine
BytesValue = PrintExtensions.AddBytes(BytesValue, Encoding.ASCII.GetBytes(String.Format("{0,-25}", "تيست"))); //Arabic Charecter, prints as ????
I know that somewhere in between the English and Arabic text I have to switch to Arabic Character Set
BytesValue = PrintExtensions.AddBytes(BytesValue, Encoding.ASCII.GetBytes("English Text"));
//Here i have to switch To Arabic Charecter set
BytesValue = PrintExtensions.AddBytes(BytesValue, Encoding.Unicode.GetBytes(String.Format("{0,-25}", "تيست")));
//Here I have to switch back To English Character set
And then send the file containing byte array to the printer
if (File.Exists("Receipt")
File.Delete("Receipt");
File.WriteAllBytes(txtPrinterName.Text, BytesValue);
RawPrinterHelper.SendFileToPrinter("PrinterName", "Receipt");
My printer does support Arabic, but it's a matter for sending command to switch to it or to any other language.
If you could teach me how to do this, I will appreciate it.
I haven't been able to find the command reference for your exact printer model, so some of this is going to be guess work from experience. The closest I could find was this command reference from POS-X.
You are correct that special commands are prefixed with ESC. In this particular case, the command you want is ESC t - Select character code table to change the code page. The code page you want is PC864 - Arabic which is 37 decimal (25 hexidecimal). The code page for English is simply 0 for PC437 - English.
I'm not exactly sure how the PrinterUtility package functions and I couldn't find any documentation for it either.
Essentially, you need to send the following:
byte[] CodePageArabic = new byte[] { 0x1B, 0x74, 0x25 };
byte[] CodePageEnglish = new byte[] { 0x1B, 0x74, 0x00 };
Related
I would like to send an SMS containing Emojis from my GSM Modem with AT Commands.
However, no matter which encoding I try, it never works (Encoding.BigEndianUnicode, Encoding.Unicode or Default) make the SMS unreadable.
My code looks a bit like this:
// [..]
// send message with UCS2
command = "AT+CSCS=\"UCS2\"" + char.ConvertFromUtf32(13);
send(command);
// [..]
// convert my message (string from a WPF TextBox) to a unicode hex-string
byte[] ba = Encoding.BigEndianUnicode.GetBytes(message);
var hexString = BitConverter.ToString(ba);
hexString = hexString.Replace("-", "");
// send the converted string
command = hexString + char.ConvertFromUtf32(26);
send(command);
// [..]
The SMS successfully reaches its destination but the message is just some unreadable stuff.
Is this even possible to do? My GSM Modem would also support "HEX" as encoding.
Update:
It kinda works if i replace this line:
command = hexString + char.ConvertFromUtf32(26);
With this:
command = "80 " + hexString + char.ConvertFromUtf32(26);
But then i get this 㣩 letter at the start of the message...
Make sure that your modem uses the correct data coding scheme for the SMS. See this answer and
Set the modem to text mode using AT+CMGF=1
Set the coding scheme to "UCS2" using AT+CSCS="UCS2"
Send AT+CSMP=1,167,0,8 to the modem.
Now in UCS2 mode, the modem needs the recipient number in UCS2 form as well, i.e. for +123456890 send
AT+CGMS="002B003100320033003400350036003800390030",145 (145 is type-of-address for numbers with country code, use 129 otherwise.
The modem should respond with a > prompt. Then, send the message in the right encoding i.e. sending the message as what I would describe as an ASCII hex sequence of UCS2/UTF-16 bytes, i.e. encoding the string to UTF-16BE, then taking each byte and formatting it as ASCII HEX characters, i.e. 🐈 which is U+1F408 becomes D83DDC08 that is sent to the modem, 012ABC becomes 003000310032 004100420043.
Completely stuck on a problem related to the inbound parse webhook functionality offered by SendGrid: https://sendgrid.com/docs/for-developers/parsing-email/setting-up-the-inbound-parse-webhook/
First off everything is working just fine with retrieving the mail sent to my application endpoint. Using Request.Form I'm able to retrieve the data and work with it.
The problem is that we started noticing question mark symbols instead of letters when recieving some mails (written in swedish using Å Ä and Ö). This occured both when sending plaintext mails, and mails with an HTML-body.
However, this only happens every now and then. After a lot of searching I found out that if the mail is sent from e.g. Postbox or Outlook (or the like), and the application has the charset set to iso-8859-1 that's when Å Ä Ö is replaced by question marks.
To replicate the error and be able to debug it I set up a HTML page with a form using the iso-8859-1 encoding, sending a similar payload as the one seen in the link above (the default one). And after that been through testing a multitude of things trying to get it to work.
As of now I'm trying to recode the input, without success. Code I'm testing:
Encoding wind1252 = Encoding.GetEncoding(1252);
Encoding utf8 = Encoding.UTF8;
byte[] wind1252Bytes = wind1252.GetBytes(Request.Form.["html"]);
byte[] utf8Bytes = Encoding.Convert(wind1252, utf8,wind1252Bytes);
string utf8String = Encoding.UTF8.GetString(utf8Bytes);
This only results in the utf8String producing the same result with "???" where Å Ä Ö should be. My guess here is that perhaps it's due to the Request.Form["html"] returning a UTF-16 string, of the content that is encoded already in the wrong encoding iso-8859-1.
The method for fetching the POST is as follows
public async Task<InboundParseModel> FetchMail(IFormCollection form)
{
InboundParseModel _em = new InboundParseModel
{
To = form["to"].SingleOrDefault(),
From = form["from"].SingleOrDefault(),
Subject = form["subject"].SingleOrDefault(),
Html = form["html"].SingleOrDefault(),
Text = System.Net.WebUtility.HtmlEncode(form["text"].SingleOrDefault()),
Envelope = form["envelope"].SingleOrDefault()
};
}
Called from another method that the POST is done to by FetchMail(Request.Form);
Project info: ASP.NET Core 2.2, C#
So as stated earlier, I am completely stuck and don't really have any ideas on how to solve this. Any help would be much appreciated!
Using the simple serial program created awhile back(http://csharp.simpleserial.com/) but I am having trouble sending ASCII control commands to my serial device. I need to send: 01P00104##. Is there a certain way to modify the code so that it sends out these “ASCII unprintable character commands” for C#?
I tried:
private void linkLabel_HC1_100_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
if serialPort1.IsOpen()
{
serialPort1.Write( (char)2 + “01P00104##” + (char)3);
}
}
Where (char)2 and (char)3 are supposedly the character representations for the start and end of text commands. I also tried using escape codes such as \u0002 or even sendKeys.Send(“^(b)”) or ^(c) but it wont seem to work. When I use Hyperterminal or PuTTy software, I can copy and past the full command in from a notepad.txt file and it sends correctly but when I copy and past the same code into this application, I don’t get a response from the serial device. This app uses .net 2.0. The stx and etx commands in the notepad look like a small 7 and small L
You could use the SerialPort.Write(Byte[], Int32, Int32) method.
var content = new List<byte>();
content.Add(2); // ASCII STX
content.AddRange(Encoding.ASCII.GetBytes("01P00104##"));
content.Add(3); // ASCII ETX
byte[] buffer = content.ToArray();
serialPort1.Write(buffer, 0, buffer.Length);
I saw similar topics but could not find a solution. My problem is that I have a .txt file in which the symbols are in Bulgarian language / which is Cyrillic /, but after trying to read them, there is no sucess. I tried to read with this code:
StreamReader reader = new StreamReader(fileName,Encoding.UTF8);
if (File.Exists(fileName))
{
while ((line = reader.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
And I also changed the Encoding value to all possible , as I tried with GetEncoding(1251), which I wrote is for cyrillic. And when I save the .txt file I tried to save it with each different encoding which was there / UNICODE,UTF-8,BigEndianUnicode,ANSI / in each combination with the Encoding I am settin through the code, but again no success.
Any ideas for how to read the cyrillic symbols in the right way will be appriciated.
And here is sample text for this: "Ето примерен текст."
Thanks in advance! :)
Your problem is that the console can't show cyrillic characters. Try putting a breakpoint on the Console.WriteLine and inspect the line variable. Clearly you'll need to know the correct encoding first! :-)
If you don't trust me, try this: make a console program that does this:
string line = "Ето примерен текст";
Console.WriteLine(line);
return 0;
put a breakpoint on the return 0;, watch the console and watch the line variable.
I'll add that unicode consoles should be one of the "new" things in .NET 4.5
And you can try to read this page: c# unicode string output
The problem you are having is not reading the text, but displaying it.
If your real intention is to display Unicode text in a console window, then you'll have to make a few changes. If however, you will be displaying the text in a WinForms or WPF app for instance, then you will not have problems - they work with Unicode by default.
By default, the console will not handle unicode, or use a font which has unicode glyphs. You need to do the following:
Save your text file as UTF8.
Start a console which is unicode enabled: cmd \u
Change the font to "Lucida Sans Unicode": console window menu -> properties -> font
Change the codepage to Unicode: chcp 65001
Run your app.
Your characters will now be displayed correctly:
I have a question regarding encoding for text email messages using C# .net because I have mine as simple ASCII but when doing padding for formatting a recipt to the user the data is not lining up although when I check the lines in say NotePad++ they are exactly the same No. of character. Below is some code, can anyone tell me what I'm doing wrong?
StringBuilder oSB = new StringBuilder();
oSB.AppendLine(EmailLine("Amount", oTrans.PaymentAmount.ToString()));
oSB.AppendLine(EmailLine("Payment Method", oTrans.CardType));
private static string EmailLine(string FieldLabel, string FieldVal)
{
return PadLabel(FieldLabel) + FieldVal ;
}
private static string PadLabel(string FieldLabel)
{
return FieldLabel.PadRight(40, char.Parse(" ")) + ": ";
}
My output looks like this:
Amount : 100.00
Payment Method : VISA
Whether or not they line up will depend on the font being used to display the email. That's a setting on the email client. For example, here is where I would set it in Outlook:
Try changing the font in Notepad++ to Times new Roman and Courier and you'll see that they line up differently.
You have no control over the user's font if you're sending it as a plan text mail. The best you can do it indicate "Best read with ___ font" or format it as HTML where you have some control.
Other options would include outputting this to a PDF file, or an image (again where you have more control).