I am using RS232 to extract data from a digital weight machine. I received the following pattern and need to decode it:
“?????????????????x?x?????????????????x?x?????????????????x?x?????????????????x?x????????????x???x?x????????????x???x?x?????????????????x?x?????????????????x?x?????????????????x?x?????????????????x?x?????????????????x?x????????????x???x?xrn”
My expected value is around 32,390, but I got 2,940.
Here’s my current code:
string data = "?\0\0?\0?????????????\0??\0x\0?x?\0\0?\0?????????????\0??\0x\0?x?\0\0?\0?????????????\0??\0x\0?x?\0\0?\0?????????????\0??\0x\0?x?\0\0?\0??????????x?\0??\0x\0?x?\0\0?\0??????????x?\0??\0x\0?x?\0\0?\0?????????????\0??\0x\0?x?\0\0?\0?????????????\0??\0x\0?x?\0\0?\0?????????????\0??\0x\0?x?\0\0?\0?????????????\0??\0x\0?x?\0\0?\0??????????x?\0??\0x\0?x\r\n";
// Convert data to hexadecimal representation
StringBuilder hexData = new StringBuilder();
foreach (char c in data)
{
hexData.AppendFormat("{0:X2} ", (int)c);
}
string hexString = hexData.ToString();
Console.WriteLine("Hexadecimal Representation:");
Console.WriteLine(hexString);
Regex regex = new Regex(@"d+");
MatchCollection matches = regex.Matches(hexString);
var total_value_calc = 0;
List<int> numbers = new List<int>();
foreach (Match match in matches)
{
total_value_calc = total_value_calc + int.Parse(match.Value);
};
Bishal Gautam is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
4