无法正确读取 Fairbank 秤重量

Can't read the Fairbank scale weight properly

我尝试连接体重秤并从 visual studio 2013 检索重量,但有时我可以通过电线获得准确的重量,有时却不能。我不确定代码有什么问题。有人可以帮忙吗?下面列出了我的代码

using System;
using System.IO.Ports;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Windows;
using System.Data.SqlClient;
using System.Collections;
using System.Threading;

namespace PortDataReceived
{
    class PortData
    {
        public static void Main(string[] args)
        {
            try
            {
                string lineOne = "One";
                string lineTwo = "Two";
                char CarriageReturn = (char)0x0D;
                string final = lineOne + CarriageReturn.ToString() + lineTwo + CarriageReturn.ToString();// define the hexvalues to the printer
                SerialPort mySerialPort = new SerialPort("COM3");//initiate the new object and tell that we r using COM1

                mySerialPort.BaudRate = 9600;
                //mySerialPort.Parity = Parity.Odd;
                //mySerialPort.StopBits = StopBits.Two;
                //mySerialPort.DataBits = 7;
                mySerialPort.Parity = Parity.Odd;
                mySerialPort.StopBits = StopBits.Two;
                mySerialPort.DataBits = 7;
                mySerialPort.Handshake = Handshake.None;
                mySerialPort.ReadTimeout = 20;
                mySerialPort.WriteTimeout = 50;
                mySerialPort.DtrEnable = true;
                mySerialPort.RtsEnable = true;
                //those r all the setting based on the scale requirement

                /*foreach (string port in System.IO.Ports.SerialPort.GetPortNames())
                {
                    Console.WriteLine(port);
                }*/
                while (true)
                {
                    mySerialPort.Open();
                    mySerialPort.Write(final);
                    mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);


                    Console.WriteLine("Press any key to continue...");
                    Console.WriteLine();
                    Console.ReadKey();
                }
                //mySerialPort.Close();
            }
            catch (System.IO.IOException e)
            {
                if (e.Source != null)
                    Console.WriteLine("IOException source: {0}", e.Source);
                throw;
            }
        }

        private static void DataReceivedHandler(
                            object sender,
                            SerialDataReceivedEventArgs e)
        {
            SerialPort sp = (SerialPort)sender;
            string indata = sp.ReadExisting();
            Console.WriteLine("Data Received:");
            Console.Write(indata);
            Console.ReadKey();
        }
    }
}

通常在串行通信中,您必须多次读取,并连接每次读取,直到您最终检测到您已读取显示您已全部接收的部分。检测结束可能基于接收到的特定字节数,或者它可能正在寻找特定字节或字节序列。但这取决于您做出决定,并继续阅读直到满足该条件。当你第一次阅读时,可能有字符在等待,但设备还没有将它们全部发送出去,所以你几乎必须在第一次阅读后立即再次阅读。你正在处理一个流。处理程序事件在流开始时触发,但它不知道流将流动多长时间。

此博客 post 讨论了这个问题和其他一些常见的串口问题: http://blogs.msdn.com/b/bclteam/archive/2006/10/10/top-5-serialport-tips-_5b00_kim-hamilton_5d00_.aspx