Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

convert byte to string

byte string serial port

  • Please log in to reply
3 replies to this topic

#1 mwesigebrian

mwesigebrian

    New Member

  • Members
  • Pip
  • 3 posts

Posted 30 April 2013 - 06:55 PM

so a i have a gsm cellular sheild connected to a netduino com1 and i want to read data from com1, but i have failed to convert the bytes output to string, the method:

string result =System.Text.Encoding.UTF8.GetSTring(bytes);

doesnt seem to exist in the microframe work library so i tried to use this:

 

 
        static void port_DataReceived(object sender, SerialDataReceivedEventArgs e) {
 
 
            byte[] bytes = new byte[1];
         
           while (port.BytesToRead > 0)
            {
                port.Read(bytes, 0 , bytes.Length);
                //port.Write(bytes, 0, bytes.Length);
                         
            }
 
           
 
            try
            {
            char[] cc = Encoding.UTF8.GetChars(bytes, 0, bytes.Length);
 
            string s = new string(cc);
            Debug.Print(s);
            Debug.Print("then");
            }
            catch(Exception ex) {
 
              Debug.Print(ex.Message);
            Debug.Print("n");
            Debug.Print(ex.StackTrace);
            }
        }
       
 
but it throws  a system.exception  and i dont why. 
HELP PLIZ


#2 ziggurat29

ziggurat29

    Advanced Member

  • Members
  • PipPipPip
  • 244 posts

Posted 30 April 2013 - 07:06 PM

try something like:

 

String strLine = new String(System.Text.Encoding.UTF8.GetChars(bytes,0,bytes.Length));

EDIT:  oh wait I see you did.

 

also be aware that nonconvertible chars (like if there is junk on the line) will cause an exception to be thrown.



#3 mwesigebrian

mwesigebrian

    New Member

  • Members
  • Pip
  • 3 posts

Posted 01 May 2013 - 11:18 AM

thanks, how would you suggest i handle the junk on the line, because an exception is thrown each time the gsm cellular module receives a call



#4 ziggurat29

ziggurat29

    Advanced Member

  • Members
  • PipPipPip
  • 244 posts

Posted 01 May 2013 - 12:52 PM

Well, junk on the line really shouldn't happen except during, say, powerup.  I catch that exception (and others) in my recovery logic, which causes the whole process of initializing the modem, establishing connection, etc.  Coincidentally I am also using a gsm modem, though not for sms, and it worked fine.  there arent any normal cases where strange characters are transmitted for text purposes, although this can easily happen when you're transporting binary (which you probably shouldn't convert to text anyway).

 

If you're getting it every time a call comes in, then you probably need to look further into it, since that should not cause 'junk on the line'.  Personally I would start by:

*  dumping the binary in your buffer 'bytes' as you have received it (before conversion).  this way you can inspect whats going into the conversion process

*  you probably have, but sanity check your port settings.  if you're getting anything meaning at all, but then failing once in a blue moon, your settings are probably fine.

*  when I was doing my gsm modem, I found it invaluable to use one of those USB-2-serial FTDI adapaters, and 'tap' the connection between the modem and the netduino with a session in putty (or any other serial program on your desktop).  this took a lot of guesswork out.  if you do tap it, just do the rx side of the adapter; tx would cause contention.

 

if all that checks out then tell me which gsm shield you are using, and which modem response is causing the woes.







Also tagged with one or more of these keywords: byte, string, serial, port

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.