Link to home
Create AccountLog in
Avatar of Chris Jones
Chris JonesFlag for United States of America

asked on

Help with c# function and return types

Hello,

i a new to C# and i am working on some code that reads serial data and i need help with my function ineed it to return back a string form a byte[] that i am reading it in as.


HERE IS MY CODE
        public static String ColorPal()
        {
            byte[] buffer = new byte[32];

            SerialPort port = new SerialPort("COM2", 9600);
            port.ReadTimeout = 0;
            port.Open();
            port.Read(buffer, 0, buffer.Length);

            return buffer.ToString;
        }

Open in new window

Avatar of Dale Burrell
Dale Burrell
Flag of New Zealand image

return System.Text.Encoding.Default.GetString(buffer);
Avatar of Chris Jones

ASKER

Hello @ dale_burrell

the line gives me an error on Default

Error      4      'System.Text.Encoding' does not contain a definition for 'Default'      E:\MicroMedic\MicroMedic\MicroMedic\MicroMedic\Program.cs      74      41      MicroMedic
Avatar of Naman Goel
use encoding from serialport instead of DefaultEncoding

so the resultant code will be

public static String ColorPal()
        {
            byte[] buffer = new byte[32];

            SerialPort port = new SerialPort("COM2", 9600);
            port.ReadTimeout = 0;
            port.Open();
            port.Read(buffer, 0, buffer.Length);

            return port.Encoding.GetString(buffer);
        }

Open in new window

Thats odd, cos that property has always existed as far as I can tell.

http://msdn.microsoft.com/en-us/library/system.text.encoding.default(v=vs.90).aspx

Try using a specific encoding e.g.

Encoding.UTF8.GetString(buffer, 0, buffer.Length);

http://stackoverflow.com/questions/11654562/how-convert-byte-array-to-string
@naman_goel

that statement results in

Error      4      'System.Text.Encoding' does not contain a definition for 'GetString' and no extension method 'GetString' accepting a first argument of type 'System.Text.Encoding' could be found (are you missing a using directive or an assembly reference?)      E:\MicroMedic\MicroMedic\MicroMedic\MicroMedic\Program.cs      74      34      MicroMedic

i have been on this issue all night i have tried a lot of those ideals


here is my includes list
using System;
using System.Net;
using System.Text;
using System.Net.Sockets;
using System.IO.Ports;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoPlus;

Open in new window

If you are using a project you also need a add a reference in addition to including the namespace on your page.

http://msdn.microsoft.com/en-nz/library/wkze6zky(v=vs.80).aspx
i think i have all of my refrences   this is the line that has issues

Encoding.UTF8.GetString

and this is the section it messes up on

GetStrings
Unfortunately I think that means the error lies elsewhere, because I have successfully converted byte arrays to strings many times using this method.

Maybe follow this example through from scratch, maybe even try it on a fresh project just to be sure:

http://msdn.microsoft.com/en-us/library/744y86tc(v=vs.90).aspx

You're not by chance using the client profile version of .net are you? Its a cut down version and doesn't contain everything in it.
it's difficult to see this:

which .net version you are using actually this method is available from .net2.0.

http://msdn.microsoft.com/en-us/library/system.text.encoding.getstring(v=vs.80).aspx
ASKER CERTIFIED SOLUTION
Avatar of AshwaniMunshi
AshwaniMunshi

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Hello @naman_goel

I am using VS 2010 on Framework V4.0
Hello @ AshwaniMunshi

this line throws error on a new project and existing so I tried another system it also throws errors

string value = ASCIIEncoding.ASCII.GetString(array);
Avatar of AshwaniMunshi
AshwaniMunshi

Could you please mention what is the error
If you have not imported System.Text, then please import it

using System.Text;
not what i was looking for found it on my own