我試圖爲稱重橋架製作軟件,同時嘗試讀取使用C#代碼,它顯示類似下面如何使用C#
)0 12589 00 )0 12589 00 )0 12589 00 )0 12589 00 )0 12589 00 )0 12589 00
private void timer1_Tick(object sender, EventArgs e)
{
string Port = GenDbUtility.GetElixirConfigValue("SERIAL_PORT", Globals.CompCode);
//<-- This block ensures that no exceptions happen
if (serialPort1 != null && serialPort1.IsOpen)
serialPort1.Close();
if (serialPort1 != null)
serialPort1.Dispose();
//<-- End of Block
serialPort1 = new SerialPort("COM1"); //<-- Creates new SerialPort using the name selected in the combobox
serialPort1.Encoding = Encoding.ASCII;
serialPort1.BaudRate = 9660;
serialPort1.Parity = Parity.None;
serialPort1.StopBits = StopBits.One;
serialPort1.DataBits = 50;
serialPort1.Handshake = Handshake.None;
serialPort1.RtsEnable = true;
serialPort1.ReadBufferSize = 4096;
serialPort1.ReceivedBytesThreshold = 100000;
serialPort1.NewLine = "\r\n";
serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);
serialPort1.Open(); //<-- make the comport listen
}
void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
if (serialPort1.IsOpen)
{
SerialPort sp = (SerialPort)sender;
string newVal = sp.ReadExisting().ToString();
///string[] qty = newVal.Split(' ');
//txtQuantity.Invoke(this.myDelegate, new Object[] { qty[3].ToString() });
//string[] qty = newVal.Split(' ');
//decimal Quantity1 = Convert.ToDecimal(qty[0]);
//decimal Quantity2 = Convert.ToDecimal(qty[1]);
//decimal Quantity3 = Convert.ToDecimal(qty[2]);
//decimal Quantity4 = Convert.ToDecimal(qty[3]);
//txtQuantity.Text = Quantity3.ToString();
//if (String.Compare(txtQuantity.Text, qty[3]) != 0)
//{
// txtQuantity.Text = Convert.ToString(qty[3]);
// //lblweight.Text = Convert.ToString(qty[2]);
// //qty[2].ToString();
//}
}
}
你正在閱讀的數據包中的每個字節應該是有意義的。閱讀他們的意思文檔。並非全部都是數據。 – Ogbe
可能會嘗試使用工具來讀出COM端口以將結果與您收到的結果進行比較。可能適應COM端口的項目會返回錯誤的數據。 – Oswald
嘗試像MSDN(https://msdn.microsoft.com/en-us/library/system.io.ports.serialport(v=vs.110).aspx)中的示例一樣實現串行端口讀取器有一個完整的示例你可以使用的課程。 – Oswald