1
私はrs232を使用してスケールからデータを読み込む予定のプロジェクトを持っています。スケールからの出力は、 "L00000"の形式です。私の問題は、スケールが "L00000、L00001、L00002、L00003、L00004"のように重くなると連続してデータを送信することです。この場合の私の関心は、最後の部分 "L00004"をdbに格納することです。 ここに私のコードです:親切に私はそれを改善するのに役立ちます。ありがとうrs232ポートから取得したバッファデータの処理
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
namespace WindowsFormsApplication5
{
public partial class Form1 : Form
{
private List<byte> PortBuffer = new List<byte>();
SerialPort comPort = new SerialPort("COM3", 9600, Parity.None, 8, StopBits.One);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (comPort.IsOpen)
{
comPort.DiscardOutBuffer();
MessageBox.Show("Data cleared");
comPort.Close();
}
if(!comPort.IsOpen)
{
comPort.Open();
//sample of data as written by the scale. How can I get the last chunk only so that I format it for my needs
comPort.Write("L00002 L00002 L00002");
int bytes = comPort.BytesToRead;
byte[] buffer = new byte[bytes];
comPort.Read(buffer, 0, buffer.Length);
comPort.DataReceived += new SerialDataReceivedEventHandler(data_received);
comPort.Close();
}
}
public void data_received(object sender,SerialDataReceivedEventArgs e)
{
MessageBox.Show("test" + comPort.ReadExisting().ToString());
}
}
}
いいえ、それゆえ私は(「」)スプリットのスペースに置き換えますが、動作しないようです。バッファのデータをキャプチャしたと思いますか? 'code' int bytes = comPort.BytesToRead; バイト[]バッファ=新しいバイト[バイト]; comPort.Read(buffer、0、buffer.Length); – KIUFELIX
はい、あなたは既にデータをバッファーに読み込んだと思います。最初のコードで読み取りコマンドを削除します。 –
これらの3行を削除し、メッセージボックスから取得した正確なデータを投稿しますか? –