C# 读数秤Tru rs232

C# 读数秤Tru rs232,c#,serial-port,C#,Serial Port,我一直在使用这个代码 但不幸的是,我一直收到这些数据 收听COM6。。。 03301B+00003301B+00003301B+00003301B+00003301B+00003301B03301B+00003301B+000033+00003301B+00003301B+00003301B+00003301B+00003301B03301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+0000330

我一直在使用这个代码

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

收听COM6。。。 03301B+00003301B+00003301B+00003301B+00003301B+00003301B03301B+00003301B+000033+00003301B+00003301B+00003301B+00003301B+00003301B03301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+0000330033003301B0301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B03301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301301B+00003301B03301B+00003301B+00003301B+00003301B+00003301B+00003301B03301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B03301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+0000330033B+00003301B+00003301B03301B+00003301B+00003301B+00003301B+00003301B+00003301B03301B+00003301B03301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B03301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+00003301B+000033303301B+001B+00003301B+00003301B03301B+00003301B03301B+00003301B+00003301B+000033+00003301B+00003301B+00003301B

有人能帮我吗 谢谢你,祝你快乐


顺便说一句,我正在使用xk3190-a1称重指示器,您收到的数据格式将是。
一个数据被认为是0x02(开始)、“+”、“000033”(重量)、“0”(分贝位置)、“1B”(检查XRL)、0x03(结束)处的12字节。
但是,由于开始代码和结束代码不是可打印的字符,因此可能不会显示或复制它们,因此看起来是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;
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Text.RegularExpressions;
名称空间控制台EAPP1
{
班级计划
{
const int singleDataLength=12;//从0x02到0x03
常量int-weightDataLength=7;/+/+/-,6位数的权重
const int decimalPositionIndex=8;//从0x02开始的索引
静态正则表达式rx=newregex(@“\x02[+-][0-9]{6}[0-4][0-9A-F]{2}\x03”,RegexOptions.Compiled | RegexOptions.IgnoreCase);
静态字符串fragmentString=“;//如果一个数据被多个事件通知,或者如果一个数据片段保留在末尾。
静态void Main(字符串[]参数)
{
//以下ReadTextAll()模拟SerialPort.ReadExisting()。
string readexistingstring=File.ReadAllText(args[0]);
如果(ReadExistingString.Length>0)
{
List foundList=GetAvailableDalitList(readExsistingString,ref fragmentString);
foreach(foundList中的字符串foundString)
{
WriteLine(“接收:{0}”,foundString);
}
如果(fragmentString.Length>0)
{
WriteLine(“不完全数据:{0}”,fragmentString);
}
}
}
静态列表GetAvailableDataList(string inputString、ref string fragmentString)
{
列表结果列表=新列表();
如果(inputString.Length>=singleDataLength)
{
int lastSTXIndex=inputString.LastIndexOf('\x02');
如果(lastSTXIndex>=0)
{
MatchCollection mc=rx.Matches(inputString);
foreach(在mc中匹配m)
{
如果(m.成功)
{
//ToDo:必须执行XRL检查
//bool checked=checkXRL(m.值);
//如果(选中)
// {
string formatteddata=m.Value.Substring(1,weightDataLength);
int decimalPoint=int.Parse(m.Value.Substring(decimalPositionIndex,1));
如果(小数点>0)
{
formatteddata=formatteddata.Insert((weightDataLength-decimalPoint),“);
}
结果列表.Add(格式化数据);
if(m.Index==lastSTXIndex)
{
lastSTXIndex=-1;
}
// }
}
}
}
如果((lastSTXIndex>=0)和((inputString.Length-lastSTXIndex)
为错误消息添加了报价。我没有