using System;
|
using System.Collections.Generic;
|
using System.IO.Ports;
|
using System.Linq;
|
using System.Threading;
|
using System.Timers;
|
using System.Windows.Forms;
|
using Gs.DevApp.Entity;
|
using Gs.DevApp.ToolBox;
|
using Newtonsoft.Json;
|
|
namespace Gs.DevApp.DevFrm.Work
|
{
|
public partial class Frm_Work09 : DevExpress.XtraEditors.XtraForm
|
{
|
string _webServiceName = "WorkWeight/";
|
string value = string.Empty;
|
/// <summary>
|
/// 串口类
|
/// </summary>
|
private SerialPort Sp = new SerialPort();
|
System.Timers.Timer timer = new System.Timers.Timer(10000);
|
public Frm_Work09()
|
{
|
InitializeComponent();
|
this.FormClosing += (s, e) =>
|
{
|
if (Sp.IsOpen)
|
Sp.Close();
|
if (timer != null)
|
{
|
timer.Stop();
|
timer.Dispose();
|
}
|
};
|
GetComList();
|
this.comLine.getSuppler("");
|
Gs.DevApp.ToolBox.UtilityHelper.SetGridViewParameterMx(gvMx1);
|
}
|
|
/// <summary>
|
/// 从注册表获取系统串口列表
|
/// </summary>
|
private void GetComList()
|
{
|
this.cmbSerialPortNum.Clear();
|
string[] ports = SerialPort.GetPortNames();
|
foreach (string port in ports)
|
{
|
cmbSerialPortNum.Properties.Items.Add(port);
|
}
|
}
|
/// <summary>
|
/// 启动事件
|
/// </summary>
|
/// <param name="sender"></param>
|
/// <param name="e"></param>
|
private void btnStart_Click(object sender, EventArgs e)
|
{
|
Sp.DataReceived -= new SerialDataReceivedEventHandler(Sp_DataReceived);
|
string _comLine = comLine.GetId();
|
string _cmbSerialPortNum = cmbSerialPortNum.Text.Trim();
|
string _cmbBaudRate = cmbBaudRate.Text.Trim();
|
string _comSjw = comSjw.Text.Trim();
|
string _comStopBits = comStopBits.Text.Trim();
|
string _comParity = comParity.Text.Trim();
|
if (string.IsNullOrEmpty(_comLine))
|
{
|
Gs.DevApp.ToolBox.MsgHelper.ShowError("请选择线体");
|
this.comLine.Focus();
|
return;
|
}
|
if (cmbSerialPortNum.SelectedIndex < 0)
|
{
|
Gs.DevApp.ToolBox.MsgHelper.ShowError("请选择串口号");
|
this.cmbSerialPortNum.Focus();
|
return;
|
}
|
if (cmbBaudRate.SelectedIndex < 0)
|
{
|
Gs.DevApp.ToolBox.MsgHelper.ShowError("请选择波特率");
|
this.cmbBaudRate.Focus();
|
return;
|
}
|
if (comSjw.SelectedIndex < 0)
|
{
|
Gs.DevApp.ToolBox.MsgHelper.ShowError("请选择数据位");
|
this.comSjw.Focus();
|
return;
|
}
|
if (comStopBits.SelectedIndex < 0)
|
{
|
Gs.DevApp.ToolBox.MsgHelper.ShowError("请选择停止位");
|
this.comStopBits.Focus();
|
return;
|
}
|
if (comParity.SelectedIndex < 0)
|
{
|
Gs.DevApp.ToolBox.MsgHelper.ShowError("请选择校验");
|
this.comParity.Focus();
|
return;
|
}
|
try
|
{
|
Sp.ReceivedBytesThreshold = 1;
|
Sp.PortName = _cmbSerialPortNum;
|
Sp.BaudRate = Convert.ToInt32(_cmbBaudRate);//设置波特率
|
if (Enum.TryParse(_comParity, out Parity pt1))
|
{
|
Sp.Parity = pt1;// 设置校验位
|
}
|
else
|
{
|
Gs.DevApp.ToolBox.MsgHelper.ShowError("无法识别的校验");
|
}
|
if (Enum.TryParse(_comStopBits, out StopBits pt2))
|
{
|
Sp.StopBits = pt2;//设置停止位
|
}
|
else
|
{
|
Gs.DevApp.ToolBox.MsgHelper.ShowError("无法识别的停止位");
|
}
|
Sp.DataBits = int.Parse(_comSjw); // 设置数据位
|
//Sp.ReadTimeout = 500; // 设置超时时间,例如500毫秒
|
//Sp.ReadBufferSize = 4096; // 设置缓冲区大小
|
Sp.DataReceived += new SerialDataReceivedEventHandler(Sp_DataReceived);
|
Sp.ReceivedBytesThreshold = 1;
|
LogHelper.Debug(this.ToString(), "打开串口通信PortName-" + Sp.PortName.ToString() + ",BaudRate-" + Sp.BaudRate.ToString() + ",StopBits-" + Sp.StopBits.ToString() + ",Parity-" + Sp.Parity.ToString());
|
if (!Sp.IsOpen)
|
Sp.Open();
|
setEnable(false);
|
timer.Elapsed += OnTimedEvent;
|
timer.AutoReset = true; // 设置为true表示重复执行,false表示执行一次后停止
|
timer.Enabled = true; // 开始计时
|
Gs.DevApp.ToolBox.MsgHelper.ShowInformation("打开成功!");
|
}
|
catch (Exception exe)
|
{
|
LogHelper.Debug(this.ToString(), "打开失败:" + exe.Message);
|
Gs.DevApp.ToolBox.MsgHelper.ShowError("打开失败" + exe.Message);
|
}
|
}
|
|
/// <summary>
|
/// 串口收到数据
|
/// </summary>
|
/// <param name="sender"></param>
|
/// <param name="e"></param>
|
public void Sp_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
|
{
|
string _tmpStr = string.Empty;
|
try
|
{
|
int i = Sp.BytesToRead;
|
if (i > 0)
|
{
|
_tmpStr = Sp.ReadExisting();
|
// Thread.Sleep(1000);
|
LogHelper.Debug(this.ToString(), $"监听到数据,字节长度:{i.ToString()},{_tmpStr}");
|
this.BeginInvoke(new Action(() =>
|
{
|
txtMsg.AppendLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff->字节数字" + i.ToString() + "->") + _tmpStr);
|
//ST,GS,+ 250.2kg
|
if (_tmpStr.Length <= 0)
|
{
|
LogHelper.Debug(this.ToString(), "_tmpStr长度<=0,退出上报");
|
return;
|
}
|
if (!_tmpStr.Contains("ST"))
|
{
|
LogHelper.Debug(this.ToString(), "_tmpStr重量状态不为ST,退出上报");
|
return;
|
}
|
if (!_tmpStr.Contains("+"))
|
{
|
LogHelper.Debug(this.ToString(), "_tmpStr不包含重量+,退出上报");
|
return;
|
}
|
string[] _ary=_tmpStr.Split('+');
|
string abc = _ary.Last();//ST,GS,
|
txtNum.Text = (abc.Trim().Replace("k", "").Replace("g", "").Replace("S","").Replace("T","").Replace("G","").Replace(",",""));
|
addModel(txtNum.Text, _tmpStr);
|
}));
|
}
|
}
|
catch (Exception ex)
|
{
|
LogHelper.Debug(this.ToString(), $"监听到数据error:{ex.Message}");
|
return;
|
}
|
}
|
|
|
/// <summary>
|
/// 启用或禁用
|
/// </summary>
|
/// <param name="bl"></param>
|
private void setEnable(bool bl)
|
{
|
comLine.Enabled = bl;
|
cmbSerialPortNum.Enabled = bl;
|
cmbBaudRate.Enabled = bl;
|
comSjw.Enabled = bl;
|
comStopBits.Enabled = bl;
|
comParity.Enabled = bl;
|
btnStart.Enabled = false;
|
}
|
/// <summary>
|
/// 上传称重数据
|
/// </summary>
|
/// <param name="_realWeight">截取后的数量</param>
|
/// <param name="_realWeightTxt">原子串</param>
|
private void addModel(string _realWeight, string _realWeightTxt)
|
{
|
string _lineId = comLine.GetId();
|
var _obj = new
|
{
|
lineId = _lineId,
|
realWeight = _realWeight.Trim(),
|
realWeightTxt = _realWeightTxt.Trim(),
|
};
|
try
|
{
|
string strJson = UtilityHelper.HttpPost("", _webServiceName + "EditModel", JsonConvert.SerializeObject(_obj));
|
ReturnModel<dynamic> _rtn = ToolBox.UtilityHelper.ReturnToDynamic(strJson);
|
if (_rtn.rtnCode <= 0)
|
{
|
Toast vm = new Toast(-1, _rtn.rtnMsg);
|
vm.Show();
|
}
|
}
|
catch (Exception ex)
|
{
|
ToolBox.MsgHelper.Warning("提示:" + ex.Message);
|
}
|
}
|
|
/// <summary>
|
/// 定时读取列表
|
/// </summary>
|
/// <param name="source"></param>
|
/// <param name="e"></param>
|
private void OnTimedEvent(Object source, ElapsedEventArgs e)
|
{
|
var _obj = new
|
{
|
lineId = comLine.GetId(),
|
};
|
try
|
{
|
var lst = new List<string>();
|
var strReturn = UtilityHelper.HttpPost("", _webServiceName + "GetModelList", JsonConvert.SerializeObject(_obj), false);
|
var dd = UtilityHelper.ReturnToList(strReturn);
|
var dt = dd.rtnData;
|
BeginInvoke(new Action(() =>
|
{
|
gcMx1.BindingContext = new BindingContext();
|
gcMx1.DataSource = dt;
|
gcMx1.ForceInitialize();
|
gvMx1.BestFitColumns();
|
}));
|
}
|
catch (Exception ex)
|
{
|
Toast vm = new Toast(-1, "gvMx1:" + ex.Message);
|
vm.Show();
|
}
|
}
|
}
|
}
|