COM Port Encoding in .NET

XBee projects like the adapter, xBee tutorials, tweetawatt/wattcher, etc. from Adafruit

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
us7892
 
Posts: 11
Joined: Fri Apr 03, 2009 2:05 pm

COM Port Encoding in .NET

Post by us7892 »

I can't seem to get the encoding correct when reading the data coming from the device. I get data every two seconds - but it is garbeled. Has anyone here tried reading the COM port in a .NET application? I've tried several encodings.

User avatar
Franklin97355
 
Posts: 23911
Joined: Mon Apr 21, 2008 2:33 pm

Re: COM Port Encoding in .NET

Post by Franklin97355 »

You need to make sure the baud rate is the same on both devices. Also what is it you are getting? Gibberish may just be xbeespeak that you don't understand. what do you expect and what do you get instead?

adafruit
 
Posts: 12151
Joined: Thu Apr 06, 2006 4:21 pm

Re: COM Port Encoding in .NET

Post by adafruit »

can you be more specific about what you're doing with what setup?

us7892
 
Posts: 11
Joined: Fri Apr 03, 2009 2:05 pm

Re: COM Port Encoding in .NET

Post by us7892 »

I've tried several different encodings when setting up the object. I have hard-coded the COM3, baud, parity, etc. Eventually that will be configurable, but you can see what I am using here.

Code: Select all

        private void InitializeComPort()
        {
            port = new SerialPort("COM3", 9600, Parity.None, 8, StopBits.One);
            //port.Encoding = Encoding.GetEncoding(28591); //Western European (ISO) 28591
            //port.Encoding = Encoding.ASCII;
            //port.Encoding = Encoding.GetEncoding(437);
            //port.Encoding = Encoding.GetEncoding("Latin1");
            port.Encoding = Encoding.Default;
            port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
            port.Open();
        }
Then, in the serialDataReceivedEventHandler, I have this. Now, I am in the process of changing this to just read the bytes, and converting to string data myself, but what I had here should also work - and from what I read, it could be the matter of getting the encoding as mentioned above correct, since ReadExisting() converts to string using the set encoding. but so far, I have tried several encodings with no luck.

Code: Select all

        //sb and port defined at top of class
        public void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            string data;

            while (port.BytesToRead != 0)
            {
                data = port.ReadExisting();
                sb.Append(data);
            }

           // This is to raise event for calling thread to grab and eventually get passed to GUI
            ReceivedBlurbEventArgs eventArgs = new ReceivedBlurbEventArgs(sb.ToString());
            ReceivedBlurb(this, eventArgs);
            sb.Length = 0;
        }
I expect to see the <xbee .... ]]}> string, just like I see in the python examples (Wattcher.py). Right now, I am getting data, I just have not converted correctly to the string data I ultimately would like to see.

I have my reciever and transmitter built and working like a charm with all the Python code - so I know I have no issues there. Now, it is just a matter of getting my windows application reading the serial data correctly. If anyone has some tips on what I might be doing wrong here, or any ideas on what I could do differently when reading and trying to convert the data...

adafruit
 
Posts: 12151
Joined: Thu Apr 06, 2006 4:21 pm

Re: COM Port Encoding in .NET

Post by adafruit »

did you try reading the xbee.py library?

us7892
 
Posts: 11
Joined: Fri Apr 03, 2009 2:05 pm

Re: COM Port Encoding in .NET

Post by us7892 »

I did not. I am doing this now. I am only now realizing that the data coming on the port is not ready for easy deconstruction. That python class xbee.py is indeed key. I will try and decypher that...thanks. I just did not notice that. I still have some work to do!

us7892
 
Posts: 11
Joined: Fri Apr 03, 2009 2:05 pm

Re: COM Port Encoding in .NET

Post by us7892 »

Well, I ported this very nice python xbee.py to .NET, and now I get each frame from the xbee.

Code: Select all

<xbee {app_id: 131, address_16: 1, rssi: 71, address_broadcast: False, pan_broadcast: False, total_samples: 19, digital: ][-1,-1,-1,-1,-1,-1,-1,-1,-1][-1,-1,-1,-1,-1,-1,-1,-1,-1][-1,-1,-1,-1,-1,-1,-1,-1,-1][-1,-1,-1,-1,-1,-1,-1,-1,-1][-1,-1,-1,-1,-1,-1,-1,-1,-1][-1,-1,-1,-1,-1,-1,-1,-1,-1][-1,-1,-1,-1,-1,-1,-1,-1,-1][-1,-1,-1,-1,-1,-1,-1,-1,-1][-1,-1,-1,-1,-1,-1,-1,-1,-1][-1,-1,-1,-1,-1,-1,-1,-1,-1][-1,-1,-1,-1,-1,-1,-1,-1,-1][-1,-1,-1,-1,-1,-1,-1,-1,-1][-1,-1,-1,-1,-1,-1,-1,-1,-1][-1,-1,-1,-1,-1,-1,-1,-1,-1][-1,-1,-1,-1,-1,-1,-1,-1,-1][-1,-1,-1,-1,-1,-1,-1,-1,-1][-1,-1,-1,-1,-1,-1,-1,-1,-1][-1,-1,-1,-1,-1,-1,-1,-1,-1][-1,-1,-1,-1,-1,-1,-1,-1,-1]], analog: [[131,-1,-1,-1,493,-1][134,-1,-1,-1,495,-1][265,-1,-1,-1,495,-1][445,-1,-1,-1,495,-1][607,-1,-1,-1,495,-1][757,-1,-1,-1,495,-1][841,-1,-1,-1,495,-1][857,-1,-1,-1,495,-1][820,-1,-1,-1,495,-1][679,-1,-1,-1,495,-1][523,-1,-1,-1,495,-1][359,-1,-1,-1,496,-1][215,-1,-1,-1,495,-1][139,-1,-1,-1,495,-1][126,-1,-1,-1,495,-1][174,-1,-1,-1,495,-1][321,-1,-1,-1,495,-1][474,-1,-1,-1,495,-1][642,-1,-1,-1,496,-1]]}>
Thanks for reminding me of xbee.py ! Porting to C# went better than expected.

adafruit
 
Posts: 12151
Joined: Thu Apr 06, 2006 4:21 pm

Re: COM Port Encoding in .NET

Post by adafruit »

great!

Locked
Please be positive and constructive with your questions and comments.

Return to “XBee products (discontinued)”