views:

250

answers:

2

I'm reading data from a serial port. I read this posting: http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/a709d698-5099-4e37-9e10-f66ff22cdd1e

He is writing about many of the issues I have encounter, but in his writing he refers to using: System.Text.Encoding.GetEncoding("Windows-1252"). The problem I'm having is when and how to apply this. There are three potitional spots in my opinion. When the serial port object is define:

private SerialPort comport = new SerialPort();

The Event handler:

comport.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);

Or when reading the data:

string data = comport.ReadExisting();

No matter where I add it. I seem to get errors. How would one use Encoding?

+4  A: 

You should set the appropriate encoding before sending or receiving data, so the constructor is a good choice.

var sp = new SerialPort
{
    Encoding = Encoding.GetEncoding("Windows-1252")
};

If you still have problems receiving data after this you need to make sure that the data being sent to the serial port is in the encoding you specified ("Windows-1252").

João Angelo
@João: your answer relies on a C# 3.0+ feature (and I think you still need parentheses after `new SerialPort`, but I'm not sure). Writing to the latest version of C# is indeed fair game, but if the asker doesn't happen to have it, oh well. My answer only worked (and yours didn't compile) because I still have C# 2.0 on my home box.
MusiGenesis
+1 anyway, since it's a better and simpler approach.
MusiGenesis
+1  A: 

Instead of using ReadExisting, use the port's Read method to get the bytes and then convert them to a string with the desired encoding, like this:

void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    SerialPort port = (SerialPort)sender;
    byte[] data = new byte[port.BytesToRead];
    port.Read(data, 0, data.Length);
    string s = Encoding.GetEncoding("Windows-1252").GetString(data);
}

Update: Here's a simpler, still-C#-2.0-friendly version based on João's answer. After you instantiate your SerialPort object, set its Encoding property like so:

port.Encoding = Encoding.GetEncoding("Windows-1252");

Then your DataReceived method becomes just this:

void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    SerialPort port = (SerialPort)sender;
    string s = port.ReadExisting();
}
MusiGenesis
Does this method act as an event handler also? Because right now I'm instantiating the serial port then creating the event handler. That event handler calls the method with the ReadExisting in it. How would this method fit in?
rross
@rross: yes, this method would just replace your existing port_DataReceived method. This is virtually the same solution as the other answer, except that this approach explicity uses the desired encoding each time, rather than relying on the default encoding's having been set previously. Half of one, six dozen of the other. :)
MusiGenesis
Plus, the other answer doesn't compile, if that sort of thing matters to you. :)
MusiGenesis
@MusiGenesis, I'm curious about the build error you're getting for the other answer. The OP does not state a .NET version, so assuming the latest one is fair game.
João Angelo