Hi I have developed a Web API in which I am taking some parameters in URL and then processing them. The protocol I am using is Modbus. It returns a byte array and in return, I want to get some specific string data from it. Below is my code
public string GetData(int slaveId, int dataAddress, int registerCount)
{
//string hexValue = intValue.ToString("X");
// Convert the hex string back to the number
int slaveHex = int.Parse(slaveId.ToString("X"), System.Globalization.NumberStyles.HexNumber);
int addressHex = int.Parse(dataAddress.ToString("X"), System.Globalization.NumberStyles.HexNumber);
int countHex = int.Parse(registerCount.ToString("X"), System.Globalization.NumberStyles.HexNumber);
//serial port settings and opening it
var serialPort = new SerialPort("COM2", 9600, Parity.Even, 8, StopBits.One);
serialPort.Open();
var stream = new SerialStream(serialPort);
stream.ReadTimeout = 200;
// send request and waiting for response
// the request needs: slaveId, dataAddress, registerCount
var responseBytes = stream.RequestFunc3(slaveHex, addressHex, countHex);
// extract the content part (the most important in the response)
var data = responseBytes.ToResponseFunc3().Data;
//Convert a byte array to string
var stringFromByteArray = System.Text.Encoding.UTF8.GetString(data);
serialPort.Close();
return stringFromByteArray;
}
The data contains an array of 14 bytes
The stringFromByteArray returns the following
"\0�\0�\0�\0\u0016\0\u0017\0\u0018\0\u001a"
As you can see in above the returned string is not as expected. I want exact values as in data byte array.
The resulting output I want it to be in Json response. Like below
{
[0] => "220",
[1] => "230",
[2] => "240",
[3] => "22",
[4] => "23",
[5] => "24",
[6] => "26",
}
How can I do it? Any help would be highly appreciated.

var result = data.Where(r=>r>0).Select(r=>r.ToString()).ToArray()[220,255,140,255]?Jsonlike I wantvar result = data.Where((r,i)=>i % 2 == 0).Select(r=>r.ToString()).ToArray()And If you want a json result just serialize result array into json.