本文中所试用的是针对赛多利斯电子天平的称重数据读取,可试用赛多利斯电子天平的类型为SQP系列和BSA系列,两种类型的区别在波特率等的设置不同,其他相同。
点击开始实验,打开串口,准备读取数据,读取到的数据自动显示在改称重结果上;
具体实现代码:
SerialPort receivePort = null;
public delegate void UpdateTextEventHandler(string text);
public UpdateTextEventHandler updateText;
private void button1_Click(object sender, EventArgs e)
{//开始试验
try
{
if (comboBox1.Text.Equals(""))
{
MessageBox.Show("请选择天平种类!");
}
else
{
if (comboBox1.Text.Equals("SQP"))
{
receivePort = new SerialPort("COM1", 9600, Parity.Odd, 8, StopBits.One);
}
else
{
receivePort = new SerialPort("COM1", 1200, Parity.None, 7, StopBits.One);
}
updateText = new UpdateTextEventHandler(UpdateTextBox);//实例化委托对象
receivePort.Handshake = Handshake.RequestToSend;
receivePort.DtrEnable = true;
receivePort.Open();
MessageBox.Show("实验开始");
receivePort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void UpdateTextBox(string text)
{//更新接收端信息
int rowNum = ultraGrid1.Rows.Count;
if (rowNum > 0)
{
for (int i = 0; i < rowNum; i++)
{
string str = ultraGrid1.Rows[i].Cells["selected"].Value.ToString();
if (str.Equals("True"))
{
ultraGrid1.Rows[i].Cells["TP_RES"].Value = text.Trim();
ultraGrid1.Rows[i].Cells["selected"].Value = false;
break;
}
}
}
}
private string readSave = "";
private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{//接收数据
//byte[] readBuffer = new byte[receivePort.ReadBufferSize];
//receivePort.Read(readBuffer, 0, readBuffer.Length);
//this.Invoke(updateText, new string[] { Encoding.UTF8.GetString(readBuffer) });
string readString = receivePort.ReadExisting();
readSave += readString;
int n1 = readSave.IndexOf("\n");
//int n2 = readSave.IndexOf("N");
if (n1 != -1)
{
char[] c = { 'N', '+', '-', 'g', '\\', 'G' };
string[] res = readSave.Split(c);
//MessageBox.Show(res[2]);
Regex t1 = new Regex(@"^\d+\.\d+$");
string str = "";
for (int i = 0; i < res.Length; i++)
{
if (t1.IsMatch(res[i].Trim()))
{
str = res[i].Trim();
}
}
this.BeginInvoke(updateText, new string[] { str });
readSave = readSave.Substring(n1 + 1);
}
}
至此成功接收到数据,至于保存到数据库部分就不再赘述,在其他文章中包含有。
还有一个简单的测试用程序,作为串口通信的入门,在此需要自己在电脑上下载一个vspd,串口模拟化工具,可创建两个串口,用来测试程序。
设计一个简单的界面
SerialPort sendPort = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
SerialPort receivePort = new SerialPort("COM2", 9600, Parity.None, 8, StopBits.One);
public delegate void UpdateTextEventHandler(string text);
public UpdateTextEventHandler updateText;
public SerialPortTest()
{
updateText = new UpdateTextEventHandler(UpdateTextBox);//实例化委托对象
sendPort.Open();
receivePort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
InitializeComponent();
receivePort.Open();
}
private void UpdateTextBox(string text)
{//更新接收端信息
textBox1.Text = text;
}
private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{//接收数据
string readString = receivePort.ReadExisting();
this.Invoke(updateText,new string[]{readString});
}
private void button2_Click(object sender, EventArgs e)
{//发送
sendPort.WriteLine(textBox2.Text);
}
private void button3_Click(object sender, EventArgs e)
{//清空
textBox1.Clear();
textBox2.Clear();
}
详细代码下载处:https://download.csdn.net/download/qq_25086397/10500211