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);
        //}
    }
NoviceToDotNet
  • 10,387
  • 36
  • 112
  • 166
  • 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 http://stackoverflow.com/questions/11654562/how-convert-byte-array-to-string or http://stackoverflow.com/questions/1003275/converting-byte-to-string-in-c-sharp – Nikola Davidovic Jan 29 '14 at 13:40
  • You are missing a closing bracket in your code. – Max Jan 29 '14 at 13:40
  • why are you using an int array? – Idov Jan 29 '14 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.. – NoviceToDotNet Jan 29 '14 at 13:47
  • As a side comment, there is [`SerialPort.Read`](http://msdn.microsoft.com/en-us/library/vstudio/ms143549.aspx) 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. – Sinatr Jan 29 '14 at 13:49

2 Answers2

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.

Gigi
  • 28,163
  • 29
  • 106
  • 188
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.

Nikola Davidovic
  • 8,556
  • 1
  • 27
  • 33
  • do i need to know type of encoding in it? – NoviceToDotNet Jan 29 '14 at 13:58
  • 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. – Nikola Davidovic Jan 29 '14 at 14:03
  • 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.. – NoviceToDotNet Jan 29 '14 at 14:03
  • 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](http://msdn.microsoft.com/en-us/library/system.text.encoding%28v=vs.110%29.aspx), there you can find different encodings that you can use. – Nikola Davidovic Jan 29 '14 at 14:09
  • serialPort.ReadByte(); return int – NoviceToDotNet Jan 29 '14 at 14:16
  • "The byte, cast to an Int32, or -1 if the end of the stream has been read.", check the [link](http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.readbyte%28v=vs.110%29.aspx). – Nikola Davidovic Jan 29 '14 at 19:08