Chris Jones
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
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;
}
return System.Text.Encoding.Defau lt.GetStri ng(buffer) ;
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\M icroMedic\ MicroMedic \Program.c s 74 41 MicroMedic
the line gives me an error on Default
Error 4 'System.Text.Encoding' does not contain a definition for 'Default' E:\MicroMedic\MicroMedic\M
use encoding from serialport instead of DefaultEncoding
so the resultant code will be
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);
}
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(bu ffer, 0, buffer.Length);
http://stackoverflow.com/questions/11654562/how-convert-byte-array-to-string
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(bu
http://stackoverflow.com/questions/11654562/how-convert-byte-array-to-string
ASKER
@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\M icroMedic\ MicroMedic \Program.c s 74 34 MicroMedic
i have been on this issue all night i have tried a lot of those ideals
here is my includes list
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\M
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;
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
http://msdn.microsoft.com/en-nz/library/wkze6zky(v=vs.80).aspx
ASKER
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
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.
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
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
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Hello @naman_goel
I am using VS 2010 on Framework V4.0
I am using VS 2010 on Framework V4.0
ASKER
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.GetStr ing(array) ;
this line throws error on a new project and existing so I tried another system it also throws errors
string value = ASCIIEncoding.ASCII.GetStr
Could you please mention what is the error
If you have not imported System.Text, then please import it
using System.Text;
using System.Text;
ASKER
not what i was looking for found it on my own