Reading Weighing Scale Tru rs232


Keywords:c# 


Question: 

i been using this code from How to display weight from weighing scale into a textbox via serial port RS-232 or usb converter?

but unfortunately i keep receiving this data

Listening on COM6... 03301B+00003301B+000033+00003301B+00003301B+001B+00003301B+00003301B03301B+00003301B+000033+00003301B+00003301B+001B+00003301B+00003301B03301B+00003301B+000033+00003301B+00003301B+001B+00003301B+00003301B03301B+00003301B+000033+00003301B+00003301B+001B+00003301B+00003301B03301B+00003301B03301B+00003301B+000033+00003301B+00003301B+001B+00003301B+00003301B03301B+00003301B+000033+00003301B+00003301B+001B+00003301B+00003301B03301B+00003301B+000033+00003301B+00003301B+001B+00003301B+00003301B03301B+00003301B+000033+00003301B+00003301B+001B+00003301B+00003301B03301B+00003301B03301B+00003301B+000033+00003301B+00003301B+001B+00003301B+00003301B03301B+00003301B+000033+00003301B+00003301B+001B+00003301B+00003301B03301B+00003301B+000033+00003301B+00003301B+001B+00003301B+00003301B03301B+00003301B+000033+00003301B+00003301B+001B+00003301B+00003301B03301B+00003301B03301B+00003301B+000033+00003301B+00003301B+001B+00003301B+00003301B03301B+00003301B+000033+00003301B+00003301B+001B+00003301B+00003301B03301B+00003301B+000033+00003301B+00003301B+001B+00003301B+00003301B03301B+00003301B+000033+00003301B+00003301B+001B+00003301B+00003301B03301B+00003301B03301B+00003301B+000033+00003301B+00003301B+001B+00003301B+00003301B03301B+00003301B+000033+00003301B+00003301B+001B+00003301B+00003301B03301B+00003301B+000033+00003301B+00003301B+001B+00003301B+00003301B03301B+00003301B+000033+00003301B+00003301B+001B+00003301B+00003301B03301B+00003301B03301B+00003301B+000033+00003301B+00003301B

can anyone help me thanks and happy coding

by the way i'm using xk3190-a1 weighing indicator


1 Answer: 

The format of your received data will be the page of this document.
One data is considered to be 12bytes at 0x02(Start), '+', '000033'(Weight), '0'(DecimalPosition), '1B'(CheckingXRL), 0x03(End).
However, since the start code and the end code are not printable characters, perhaps, they are not displayed or copied, it seems to be 10 bytes.

It checks the format of the received data, and if it is valid data, it carries out subsequent processing.
In the format check, it cuts from the start code to the end code and checks whether the data length is 12bytes and whether the XRL code matches the XRL calculation result.

If the presented data is correct, it seems that data loss often occurs.
Please check carefully as to whether it is occurring in the COM port and device driver or somewhere in the application processing.

In Addition
Please refer to the console program example which simulates the input of the serial port by reading the file.

using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;

namespace ConsoleApp1
{
    class Program
    {
        const int singleDataLength = 12;    // from 0x02 to 0x03
        const int weightDataLength = 7;     // +/-, and 6 digit weight
        const int decimalPositionIndex = 8; // index from 0x02
        static Regex rx = new Regex(@"\x02[+-][0-9]{6}[0-4][0-9A-F]{2}\x03", RegexOptions.Compiled | RegexOptions.IgnoreCase);
        static string fragmentString = "";  // If one data is notified by multiple events, or if a data fragment remain at the end.

        static void Main(string[] args)
        {
            // The following ReadTextAll() simulates SerialPort.ReadExisting().
            string readExsistingString = File.ReadAllText(args[0]);

            if (readExsistingString.Length > 0)
            {
                List<string> foundList = GetAvailableDataList(readExsistingString, ref fragmentString);
                foreach (string foundString in foundList)
                {
                    Console.WriteLine("Received:{0}", foundString);
                }
                if (fragmentString.Length > 0)
                {
                    Console.WriteLine("IncompletedData:{0}", fragmentString);
                }
            }
        }

        static List<string> GetAvailableDataList(string inputString, ref string fragmentString)
        {
            List<string> resultList = new List<string>();

            if (inputString.Length >= singleDataLength)
            {
                int lastSTXIndex = inputString.LastIndexOf('\x02');
                if (lastSTXIndex >= 0)
                {
                    MatchCollection mc = rx.Matches(inputString);
                    foreach (Match m in mc)
                    {
                        if (m.Success)
                        {
                            // ToDo: XRL check must be implemented
                            // bool checked = checkXRL(m.Value);
                            // if (checked)
                            // {
                                string formatedData = m.Value.Substring(1, weightDataLength);
                                int decimalPoint = int.Parse(m.Value.Substring(decimalPositionIndex, 1));
                                if (decimalPoint > 0)
                                {
                                    formatedData = formatedData.Insert((weightDataLength - decimalPoint), ".");
                                }
                                resultList.Add(formatedData);
                                if (m.Index == lastSTXIndex)
                                {
                                    lastSTXIndex = -1;
                                }
                            // }
                        }
                    }
                }
                if ((lastSTXIndex >= 0) && ((inputString.Length - lastSTXIndex) < singleDataLength))
                {
                    fragmentString = inputString.Substring(lastSTXIndex);
                }
                else
                {
                    fragmentString = "";
                }
            }
            return resultList;
        }
    }
}