2

I have some code like this which stores serial port data to a array of int named buffer here Now I want to that buffer to convert it in string back. How can I do that?

  private void serialPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
    {
        //if (cCommon.DecryptText(CallerId) == "enable")
        //{
        if (buffer.Length > 0)
        {
            try
            {
                for (int c = 0; c != serialPort.BytesToRead; c++)
                {
                    buffer[pointer] = serialPort.ReadByte();
                    pointer++;
                }
            }
            catch (TimeoutException x)
            {
                //BackgroundWorker bw = new BackgroundWorker();



                bw = new BackgroundWorker();
                bw.DoWork += new DoWorkEventHandler(bw_DoWork);
                bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
                bw.RunWorkerAsync();
            }
        }
        // }
        //else
        //{
        //    MessageBox.Show("You do not have permission to use This feature serialPort", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        //}
    }
5
  • 3
    First why using int array? Shouldn't byte array serve better? Did you try searching a little bit? For example something like this could help stackoverflow.com/questions/11654562/… or stackoverflow.com/questions/1003275/… Commented Jan 29, 2014 at 13:40
  • You are missing a closing bracket in your code. Commented Jan 29, 2014 at 13:40
  • why are you using an int array? Commented Jan 29, 2014 at 13:43
  • In my case i need to read the number from the port attached to caller id, but in the first few second i get the number but after that it keep ringing the RING RING while i want to skip the event and in first few second want to read number there and want to start something in background with that number.. Commented Jan 29, 2014 at 13:47
  • As a side comment, there is SerialPort.Read method, used to read into a byte array (your buffer). And uninitialized pointer makes me a bit scared (if it's a field, then where do you set it to 0?). As well as no buffer overrun chack. Commented Jan 29, 2014 at 13:49

2 Answers 2

1

See Encoding.GetString(). If your array of integers can be resolved to an array of bytes, and you know the encoding, then you should be able to do something like:

Encoding.UTF8.GetString(buffer)

...after converting the integer array into a byte array.

Sign up to request clarification or add additional context in comments.

Comments

1

I'm not sure about the exact solution since it is depending on the device you are communicating with but I could suggest the following approach. First you are reading bytes, then you should use byte array instead of integer array. The fact the you want to read digits there doesn't mean that you should use integers (numbers?). I guess that you should probably have ASCII characters there so you should use that conversion but this is something you should see for yourself.

byte[] buffer = new byte[255];
private void serialPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
      try
      {
         for (int c = 0; pointer+c < buffer.Length && c < serialPort.BytesToRead; c++)
         {
            buffer[pointer++] = (byte)serialPort.ReadByte();
         }
      }
      catch
      {
          MessageBox.Show("Error reading port!");
      }
}
.
.
.
//and then you convert what you have read with something like this:

System.Text.Encoding.ASCII.GetString(buffer);

But bare in mind that you are converting the whole 255 bytes there while you might have less characters read. Therefore you should probably revise code that reads from the port.

6 Comments

do i need to know type of encoding in it?
As I said, it is probably ASCII and probably behaving like a modem but you should check anyways. Also, take into account @Sinatr's comment. He pointed in good directions.
i changed the code with exact code of mine..would please relook it i am toiling it long..i want to read the number..i want to make it generalise solution caller if can be any thing..
Well, your code doesn't mean anything to me in this context. If you want to see the possibilities of getting strings out of byte arrays, you should check this link, there you can find different encodings that you can use.
serialPort.ReadByte(); return int
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.