Problem with custom encoding

Hi

I'm writing my own Encoder which I will use on the SerialPort class. The small tests I've made work just fine, but when assigning the encoder to the SerialPort.Encoder, I get the exception

System.NotSupportedException : No data is available for encoding 0
at System.Text.Encoding.GetDataItem()
at System.Text.Encoding.get_WebName() at System.IO.Ports.SerialPort.set_Encoding(Encoding value)

Does anyone know why this might be Have I failed to implement some overload, or incorrect overload I'm sorry I can't provide with any more information than this. Hopefully someone have had this problem before, and figured out what it was.

Best regards

Bjornar Sundsbo



Answer this question

Problem with custom encoding

  • Coder82

    Hi,

    False Alarm!

    Problem is not with SerialPort
    but with new way Chr() works

    before the program I try to modify was widely using String instead of Byte array as it is handy
    to pass to the functions and already can carry concatenations
    so in VB6 (b =asc(chr(b))) is TRUE
    now it can be FALSE
    as all strings are now 2byte per symbol in unicode

    so I'm modifying all parts to use byte array and index for size of data inside
    sW = "" becomes now abW_Len = 0

    Ted

  • SanJoseBoston

    Lucian is correct; we only support ASCII, UTF8, UTF32, Unicode, single byte, and double byte encodings.

    When developing the SerialPort class we ran into a lot of problems from some of the stranger encodings like UTF7 when reading bytes from the SerialPort and converting them to characters. With enough time we of course could have fixed all of the problems however we felt that the encodings that we support would be sufficient for the vast majority of our customers so we decided not to support additional encodings.

    I would like to understand why you need to use a custom encoding so we can explore supporting this in a future release. Can you explain your scenario a little

    Thanks,

    Ryan Byington [MS]



  • Mike5809

    Thanks for the feedback. We made a feature request to possibly support this in an upcoming release.

    Though it is a bit of a pain in the meantime you use the Read/Write methods that deal with byte arrays and do the decoding and encoding yourself.

    Ryan Byington [MS]



  • CaravanBill

    Using Reflector you can see that you cannot use just any Encoding :). There are other threads about SerialPort encoding, maybe those will help.

    public void set_Encoding(Encoding value)
    {
       if (value == null)
       {
          throw new ArgumentNullException("Encoding");
       }
       if (((!(value is ASCIIEncoding) && !(value is UTF8Encoding)) && (!(value is UnicodeEncoding) && !(value is UTF32Encoding))) && (((value.CodePage >= 0xc350) && (value.CodePage != 0xd698)) || (value.GetType().Assembly != typeof(string).Assembly)))
       {
          throw new ArgumentException(SR.GetString("NotSupportedEncoding", new object[] { value.WebName }), "value");
       }
       this.encoding = value;
       this.decoder = this.encoding.GetDecoder();
       this.maxByteCountForSingleChar = this.encoding.GetMaxByteCount(1);
       this.singleCharBuffer = null;
    }
    


  • Craig Berntson


    There is a problem with SerialPort class in .net
    .encoding is used always - there is no way at the moment to use it in binary mode.
    What happens is that in my case sequence 1B 83 is always replaced by 1B 3F
    while 1B 82 is Ok - given from SerialPort by 1B 82.
    This is very annoying!
    I have a code made in VB6 and in the new tools (.net) I had to use SerialPort class

    Any suggestion on how to build encode class for serial port with the logic:
    ByteOut = ByteIn
    This can help a lot.

    I see what the driver receives by using PortMon from sysinternals.
    I tried using
    1. SerialPort.Read(buf,0,Len(SerialPort.BytesToRead))
    2. SerialPort.ReadByte

    And all of them gave the same result.
    Help please.

    Ted

  • Dave512

    Well a user scenario is very simple: there exist numerous legacy devices that do not support your restricted range of encodings. For example, there have been almost 10 differerent single-byte codepages for Polish language, out of which you support 2. Those other are pretty simple to implement (eg. Mazovia took me about 80 LOC, this includes unit tests), but cannot be used in communication in a simple way because of this "clever solution".

  • Problem with custom encoding