|
- using CnasSynchronousCommon;
- using System;
- using System.Collections.Generic;
- using System.IO.Ports;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
-
- namespace CNAS_SerialPort
- {
- public class SerialPortDataReceive
- {
- public static string ReceivePortData(Control thisControl, SerialPort sPortBalance,List<string> lstReadTxt)
- {
- string strReceiveData = "";
- //读取缓冲区三次数据,类似{N +} {0.0000}{ g},这里只要第二部分的内容
- try
- {
- //读取数据
- thisControl.Invoke((EventHandler)(delegate
- {
- string strReadTxt = sPortBalance.ReadExisting();
- if (strReadTxt.StartsWith("N"))
- {
- lstReadTxt.Clear();
- }
- lstReadTxt.Add(strReadTxt);
- if (lstReadTxt.Count == 3)
- {
- strReceiveData = lstReadTxt[1];
- }
- }
- )
- );
- }
- catch (Exception ex)
- {
- AppLog.Error($"接收数据失败:{ex.Message}");
- throw ex;
- }
- return strReceiveData;
- }
- }
- }
|