阅读称重秤Tru rs232

时间:2018-01-11 09:10:10

标签: c# serial-port

我一直在使用How to display weight from weighing scale into a textbox via serial port RS-232 or usb converter?

中的代码

但不幸的是我一直收到这些数据

  

听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 + 00003301B 03301B + 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

任何人都可以帮助我 谢谢和快乐的编码

顺便说一句,我使用的是xk3190-a1称重指示器

1 个答案:

答案 0 :(得分:0)

您收到的数据格式为the page of this document 一个数据在0x02(开始),' +',' 000033'(重量),' 0'(DecimalPosition),&#39被认为是12字节; 1B'(CheckingXRL),0x03(结束) 但是,由于起始码和结束码不是可打印的字符,也许它们不会被显示或复制,它似乎是10个字节。

检查接收数据的格式,如果是有效数据,则执行后续处理 在格式检查中,它从开始代码切换到结束代码,并检查数据长度是否为12字节以及XRL代码是否与XRL计算结果相匹配。

如果显示的数据是正确的,似乎经常会发生数据丢失 请仔细检查它是否出现在COM端口和设备驱动程序中或应用程序处理中的某个位置。

另外
请参阅控制台程序示例,该示例通过读取文件来模拟串行端口的输入。

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;
        }
    }
}