?
Solved

System.IO.Ports.SerialPort with raw encoding?

Posted on 2006-05-18
7
Medium Priority
?
2,116 Views
Last Modified: 2008-01-09
I use System.IO.Ports.SerialPort and my code is up and running, but SerialPort.Encoding appears to be getting in my way.

How can I disable encoding, or switch to a raw encoding? I need to transfer bytes with the entire 0x00..0xFF value range, unaltered by encoding.
0
Comment
Question by:bgauweiler
  • 4
  • 3
7 Comments
 
LVL 11

Expert Comment

by:vo1d
ID: 16711894
doesn't Encoding.UTF8 work?
0
 
LVL 2

Author Comment

by:bgauweiler
ID: 16712336
To tell the truth, I don't know. I can not yet look into the downstream receiver (an embedded device without debugging capabilities), I can only inspect some results (resulting from my application downstreaming some data), which suggest that some code translation has occurred. I may be on the wrong track with the encoding inquiry alltogether, this is the only straw that I've got at the moment.

Are you saying that UTF8 will pass 8-bit values without processing?
0
 
LVL 2

Author Comment

by:bgauweiler
ID: 16712380
According to this (http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8) and similar web resources, UTF-8 only leaves values 0x00-0x7F untouched. Hmm.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 11

Accepted Solution

by:
vo1d earned 1000 total points
ID: 16715520
good question, maybe a little test app will make that clear.
try something like this with your bytes to check out, how they will be encoded:


Encoding ascii = Encoding.UTF8; //try it for all

// Encode the string.
Byte[] encodedBytes = encoder.GetBytes(yourbyteshere); <-- here you load your bytes
Console.WriteLine();
Console.WriteLine("Encoded bytes:");
foreach (Byte b in encodedBytes)
{
  Console.Write("[{0}]", b);
}
Console.WriteLine();

hope this will help you.
0
 
LVL 11

Expert Comment

by:vo1d
ID: 16715527
sorry, wrong fieldname.
has to be:
Encoding encoder= Encoding.UTF8;
0
 
LVL 2

Author Comment

by:bgauweiler
ID: 16732067
OK I followed you suggestion and wrote a simple console application that tests all encodings for quaility:

    class Program {
        static void Main(string[] args) {
            char[] raw = new char[256];

            Console.WriteLine("Encodings tester\n");

            // build raw data
            for(int i = 0; i <256; ++i) {
                raw[i] = (char)i;
            }

            // get all encodings
            foreach(EncodingInfo encoderInfo in Encoding.GetEncodings()) {
                Encoding encoder = encoderInfo.GetEncoding();
                Console.Write("Encoding '{0}': ", encoderInfo.Name);

                byte [] encoded = encoder.GetBytes(raw);

                if (encoded.Length != raw.Length) {
                    Console.WriteLine("failed with different length.");
                } else {
                    bool match = true;
                    for (int i=0; (i < encoded.Length) && match; ++i) {
                        if (encoded[i] != i) {
                            match = false;
                            break;
                        }
                    }
                    if (match) {
                        Console.WriteLine("****** MATCH. ******");
                    } else {
                        Console.WriteLine("failed.");
                    }
                }
            }
           
            Console.WriteLine("\nDone.");
        }
    }

Turns out there is exactly one match: iso-8859-1. Although vo1d didn't provide the exact (or even correct) answer, I shall award points for having been kicked into the right direction.
0
 
LVL 2

Author Comment

by:bgauweiler
ID: 16734403
OK, one more update on this for the record:

System.IO.Ports.SerialPort.ReadByte() bypases encoding, and so do the byte and byte[] based overloads of the Write method.
The entire issue was in fact unrelated to Encodings (but now I have learned about encoding objects, great!), but simply caused by the fact that my embedded controller implements a non-standard timing of the handshake lines. Correctly implementing this "on foot" rather than by using the build-in HW handshake support solved the problem.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video tutorial shows you the steps to go through to set up what I believe to be the best email app on the android platform to read Exchange mail.  Get the app on your phone: The first step is to make sure you have the Samsung Email app on your …
Stellar Phoenix SQL Database Repair software easily fixes the suspect mode issue of SQL Server database. It is a simple process to bring the database from suspect mode to normal mode. Check out the video and fix the SQL database suspect mode problem.
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question