无法正确读取费尔班克的秤重

时间:2015-01-14 17:16:57

标签: c# scale

我试图连接天平并从视觉工作室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();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

通常使用串行通信,您必须多次读取并连接每个读取,直到您最终检测到您已阅读显示您已收到所有内容的部分。检测结束可以基于所接收的特定字节数,或者它可能正在寻找特定字节或字节序列。但这取决于你做出决定,并继续阅读,直到满足这一条件。当您第一次阅读时,可能会有人物等待,但设备尚未发送,所以您必须在第一次阅读后立即再次阅读。你正在处理一个流。处理器事件在流启动时触发,但它不知道流将流多长时间。

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