0

I am trying to read a message from serialport. I am reading it as bytes of data. I have a byte array which holding all the data bytes i read from serial port. My byte array holds a message string like this after converting into string.

\u0006\u0001\0d\o?\"\0$?##STA\r\nSystemLevel:Run\r\nstatus:1\r\nSensor Value:12.45\r\n................\r\nSTOP##

I look for certain byte pattern (for example start byte)from byte array once it matches converting everything to char string from that point of index to certain index length. since i know the length of the message i want convert. for example byte[25] to byte[185].

I just want to know which is the best way to read result values from the message string?? result values for example byte[28] to byte[32],byte[42] to byte[49]...etc.

Is it better to convert all required bytes to string and parse it?? or is it better to use several byte patterns to get the characters. which is the best method.

PRV
  • 129
  • 1
  • 7

3 Answers3

0

Here is a sample of my code that I used in a project where receiveMessage is a byte[] :

year = receiveMessage[4];
month = receiveMessage[5];
day = receiveMessage[6];
hour = receiveMessage[7];
minute = receiveMessage[8];
second = receiveMessage[9];
date = new DateTime(2000 + year, month, day, hour, minute, second);

WriteDBMessageLocation locationMessage = new WriteDBMessageLocation();
locationMessage.message = DATABASE_MESSAGE_TYPE.LOCATION;

locationMessage.trackTime = date;
locationMessage.currTime = DateTime.Now;

locationMessage.lattitude = new byte[4];
Array.Copy(receiveMessage, 11, locationMessage.lattitude, 0, 4);

locationMessage.longitude = new byte[4];
Array.Copy(receiveMessage, 15, locationMessage.longitude, 0, 4);
locationMessage.speed = receiveMessage[19];

locationMessage.courseStatus = new byte[2];
Array.Copy(receiveMessage, 20, locationMessage.courseStatus, 0, 2);

locationMessage.IMEI = byteState.Value.IMEI;
WriteDBAsync.ReadWriteFifo(WriteDBAsync.Mode.WRITE, locationMessage);```


Platypus
  • 321
  • 1
  • 4
  • 17
jdweng
  • 33,250
  • 2
  • 15
  • 20
0

You can always do something with for loop, like this:

using System;
using System.Text;

namespace _ {
  class Q {
    public static void Main(string[] args) {
      string str = @"\u0006\u0001\0d\o?\""\0$?##STA\r\nSystemLevel:Run\r\nstatus:1\r\nSensor Value:12.45\r\n................\r\nSTOP##";
      byte[] bts = Encoding.UTF8.GetBytes(str);
      var g23to40 = getRange(bts, 22, 39);
      Console.WriteLine(Encoding.UTF8.GetString(g23to40));
    }
    public static byte[] getRange(byte[] a, int s, int e = -1) {
      if (e == -1) { e = a.Length-1; }
      if (e <= s) { return new byte[]{}; }
      byte[] r = new byte[e-s];
      int bi = 0;
      for (int i = s; i != e; i++) {
        if (i >= s && i <= e) {
          r[bi] = a[i];
          bi++;
        }
      }
      return r;
    }
  }
}
BladeMight
  • 2,670
  • 2
  • 21
  • 35
0

in my opinion:

  1. i would process it as byte array and remove header/checksum stuff/...

byte.Skip(..).Take(...)

  1. give that bytearray to a function, which converts to right string format, and does the over stuff.

if you search for stuff in the byte array: I guess you are not working with much data (100Mb+). Cause Linq isnt the fastet.

Byte<Span>

ist the faster one (.net Core)

and the fastest is found here:

byte[] array pattern search

(the Buffer.BlockCopy thing)

Crimson
  • 103
  • 6