我有一個項目,我必須從稱量機的輸出接收到計算機的輸出,然後緩存在我的軟件中。如何從加權比例得到穩定的結果?
這種規模的品牌是費正清鱗
模型:基於手動上perkinsscale.com/files/benchscales/manual1.pdf
:70-2455-4w/431
手冊我上面列出的,我從數據庫中檢索數據的想法是發送和十六進制代碼到規模,並獲取它響應的信息。我已經有了一個接收輸出的代碼,但是我一直沒有得到精確的結果。有時我得到截斷的結果。例如,不是向我顯示150.15磅,而是顯示15磅或0.15磅。此外,有時我無法從中收到任何數據。它只會給我一個空白的結果。我需要的是,無論何時我運行代碼,它都會返回給我顯示的規模相同的數字。我使用的代碼如下:
任何人都可以幫我嗎?
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
{
char CarriageReturn = (char)0x0D;
string final = CarriageReturn.ToString();
SerialPort mySerialPort = new SerialPort("COM3");
mySerialPort.BaudRate = 9600;
mySerialPort.Parity = Parity.Odd;
mySerialPort.StopBits = StopBits.Two;
mySerialPort.Encoding = System.Text.Encoding.GetEncoding(1252);
mySerialPort.DataBits = 7;
mySerialPort.Handshake = Handshake.None;
mySerialPort.ReadTimeout = 12000;
mySerialPort.WriteTimeout = 5000;
mySerialPort.DtrEnable = true;
mySerialPort.RtsEnable = true;
while (true)
{
mySerialPort.Open();
mySerialPort.Write(final+"\r\n");
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();
int i = sp.BytesToRead;
indata = " " + indata + " ";
if((indata.IndexOf(" "+"kg"+" ")>=0)||(indata.IndexOf(" "+"lb"+" ")>=0))
{
Console.WriteLine("Data Received:");
Console.Write(indata);
Console.ReadKey();
}
}
}
}
請修復您的代碼的格式,它傷害了眼睛。 (沒有空行......) – DrKoch 2015-01-15 15:35:39
這肯定是假的:I'd sp = ByteToRead;' – DrKoch 2015-01-15 15:37:09
你應該從esrial端口讀**足夠的**字節。例如:嘗試讀取,直到接收到換行符或設備用來標記'傳輸結束' – DrKoch 2015-01-15 15:38:31