0

Language: c#

Im struggling to convert some data My device sends to PC the humble byte 0xbc(188 decimal) The serial port sees it as the char "?" or 0x3f in hexadecimal i think I have tried all conversion functions. I don't see how 0xbc translates to 0x3f ;

After seeing this I set the encoding of serial port to

ComPort.Encoding = Encoding.ASCII;

but no result; the same thing happens

I have tried

byte[] b=System.Text.Encoding.UTF8.GetBytes(recievedData);

where recievedData is recievedData = ComPort.ReadExisting(); ( recievedData is a string)

Im sure it is a conversion problem but I'm stuck

int  c=char.ConvertToUtf32(recievedData,0) //does not work

What to do in order to have 0xbc' string equivalent in recievedData ?

Riad
  • 3,822
  • 5
  • 28
  • 39
fjohn
  • 33
  • 5
  • make sure the baud rate of receiving and sending ports are equal – Uthistran Selvaraj Aug 17 '16 at 07:28
  • What is the result you expect? The number 188 to be in the `c` variable? Because in that case you shouldn't use Text conversions – Bojan B Aug 17 '16 at 07:29
  • If you want something else as the result, the extended ascii value for `0xBC` is ¼ - to get this you can look at http://stackoverflow.com/questions/666385/how-can-i-convert-extended-ascii-to-a-system-string – Bojan B Aug 17 '16 at 07:37
  • @Bojan Where is "extended ASCII" defined? Do you mean the "ISO/IEC 8859-1" encoding (aka "Latin 1")? – Matthew Watson Aug 17 '16 at 07:46
  • @MatthewWatson thats the encoding I meant, yes – Bojan B Aug 17 '16 at 07:48
  • @fionut Try using `Encoding.Default` rather than `Encoding.ASCII`. – Matthew Watson Aug 17 '16 at 07:48
  • You are doing it wrong. Do **not** use the SerialPort methods that read a string. It is not a string, the device is sending binary data. Use the Read() method instead. Or you got the configuration wrong and the data is getting corrupted, that's possible as well. Look in the device's programming manual to make sure. – Hans Passant Aug 17 '16 at 08:57
  • the device is sending mixed data; strings; and some other not printable values like 0xbc; the communication is fine; when the device sends strings to pc ; the pc sees exactly what the device is sending. the problem is for other values; with no printable meaning – fjohn Aug 17 '16 at 10:08
  • @MatthewWatson i used the default encoding the same thing happened; after that i changed to ASCII – fjohn Aug 17 '16 at 10:11

0 Answers0