2

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

Thomas Smyth - Treliant
  • 4,993
  • 6
  • 25
  • 36
jhunkhan
  • 35
  • 6

1 Answers1

2

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;
        }
    }
}
kunif
  • 4,060
  • 2
  • 10
  • 30
  • how can i tell the machine or cut the data just to show this only +00003301B string data = serialPort.ReadTo(System.Convert.ToString(serialPort.ReadExisting())) – jhunkhan Jan 12 '18 at 02:11
  • It will be difficult to do with one statement. It would be better to create a subroutine that performs format checking and extraction. With this format only, you can handle it with this regular expression("\x02[+-][0-9]{6}[0-4][0-9A-F]{2}\x03") using the RegEx/Match/MatchCollection class in the subroutine. – kunif Jan 13 '18 at 06:23
  • how will i do that? will i be using array to store temp data from readexisting? – jhunkhan Jan 13 '18 at 08:40
  • Program example was added. – kunif Jan 13 '18 at 11:41
  • thank you so much for your help. you made my day happy,, – jhunkhan Jan 15 '18 at 02:50
  • follow up question, how can i tell if the data is increasing or decreasing? – jhunkhan Jan 19 '18 at 01:47
  • You can memorize each weight together with the reception time and calculate the difference from the previous stable weight when the change in weight becomes small, you can know the increase or decrease and the amount. – kunif Jan 19 '18 at 02:18
  • Is it business of you to think about it? – kunif Jan 19 '18 at 03:56
  • when closing the port, after a transaction is done, i get some error on the part on serialport.readexisting. – jhunkhan Jun 25 '19 at 02:53
  • For example, if the port is closed, it is natural for InvalidOperationException to occur. [SerialPort.ReadExisting Method](https://learn.microsoft.com/en-us/dotnet/api/system.io.ports.serialport.readexisting?view=netframework-4.8) – kunif Jun 25 '19 at 03:35