using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using MSTL.LogAgent;
namespace IECSC.Labelling
{
public partial class Mainfrm : Form
{
public Mainfrm()
{
InitializeComponent();
this.WindowState = FormWindowState.Normal;
this.ControlBox = true;
}
private void Mainfrm_Load(object sender, EventArgs e)
{
AddParameters();
string[] itemName = SerialPort.GetPortNames(); //获取当前计算机串型端口名称数组.
cboPortName.Items.Clear();
foreach (var item in itemName)
{
cboPortName.Items.Add(item);
}
//串口索引
cboPortName.SelectedIndex = 0;
//波特率索引
cboBaudRate.SelectedIndex = 1;
this.txtbox_PalletNo.Focus();
this.sep_com.Open();
}
/// <summary>
/// 显示信息到TextBox
/// </summary>
/// <param name="infoByPalletNo"></param>
private void UpdateTextBoxByEntity(InfoByPalletNo infoByPalletNo)
{
if (Convert.ToInt32(infoByPalletNo.salesType).Equals(1))
{
this.txtbox_SalType.Text = "OE不贴标";
}
else if (Convert.ToInt32(infoByPalletNo.salesType).Equals(2))
{
this.txtbox_SalType.Text = "不贴标;<- 送往111处理";
}
else if (Convert.ToInt32(infoByPalletNo.salesType).Equals(3))
{
this.txtbox_SalType.Text = "内销";
}
else if (Convert.ToInt32(infoByPalletNo.salesType).Equals(4))
{
this.txtbox_SalType.Text = "外销-巴西";
}
else if (Convert.ToInt32(infoByPalletNo.salesType).Equals(5))
{
this.txtbox_SalType.Text = "外销-欧洲";
}
else if (Convert.ToInt32(infoByPalletNo.salesType).Equals(6))
{
this.txtbox_SalType.Text = "外销-中南美";
}
else if (Convert.ToInt32(infoByPalletNo.salesType).Equals(7))
{
this.txtbox_SalType.Text = "异常";
}
else
{
this.txtbox_SalType.Text = null;
}
this.txtbox_ProductId.Text = infoByPalletNo.productId;
this.txtbox_TreadStamp.Text = infoByPalletNo.treadStamp;
this.txtbox_RealPalletQty.Text = infoByPalletNo.realPalletQty;
this.txtbox_MaterName.Text = infoByPalletNo.materName;
this.txtbox_BatchNo.Text = infoByPalletNo.batchNo;
}
private void InitEntityByDbInfo(string readString)
{
string ErrMsg = string.Empty;
if (!DbAction.Instance.GetDbTime())
{
ErrMsg = "数据库连接失败。";
return;
}
try
{
var dTableFromPort = DbAction.Instance.GetDbInfoByPalletNo(readString);
foreach (DataRow row in dTableFromPort.Rows)
{
InfoByPalletNo infoByPalletNo = new InfoByPalletNo();
infoByPalletNo.productId = row["PRODUCT_ID"].ToString();
infoByPalletNo.salesType = row["SALES_TYPE"].ToString();
infoByPalletNo.treadStamp = row["TREAD_STAMP"].ToString();
infoByPalletNo.realPalletQty = row["REAL_PALLET_QTY"].ToString();
infoByPalletNo.materName = row["MATER_NAME"].ToString();
infoByPalletNo.batchNo = row["BATCH_NO"].ToString();
UpdateTextBoxByEntity(infoByPalletNo);
}
}
catch (Exception ex)
{
log.Error("给实体类赋值时出错:" + ex.ToString());
}
}
/// <summary>
///显示界面
/// </summary>
/// <param name="readString"></param>
private void SetReadStringToTextBox(string readString)
{
if (this.InvokeRequired)
{
Action<string> act = new Action<string>(SetReadStringToTextBox);
this.Invoke(act, new object[] { readString });
}
else
{
this.txtbox_PalletNo.Text = readString;
}
}
/// <summary>
/// 设置波特率可选数组
/// </summary>
private void AddParameters()
{
this.cboBaudRate.Items.AddRange(new object[] { "4800", "9600", "14400", "19200", "38400", "56000", "57600", "115200", "128000" });
}
private void cboPortName_SelectedIndexChanged(object sender, EventArgs e)
{
this.sep_com.Close();
sep_com.PortName = cboPortName.Items[cboPortName.SelectedIndex].ToString();
this.sep_com.Open();
}
private void cboBaudRate_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
this.sep_com.Close();
sep_com.BaudRate = Convert.ToInt32(cboBaudRate.Items[cboBaudRate.SelectedIndex] ?? 0);
this.sep_com.Open();
}
catch (Exception ex)
{
log.Error("打开串口失败:" + ex.ToString());
}
}
private void sep_com_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
string readString = this.sep_com.ReadExisting();
SetReadStringToTextBox(readString);
}
private void txtbox_PalletNo_KeyPress(object sender, KeyPressEventArgs e)
{
if (Convert.ToInt32(e.KeyChar).Equals(13))
{
updateTextBoxToEmpty();
string c = "C";
if (this.txtbox_PalletNo.Text != null && this.txtbox_PalletNo.Text.StartsWith(c))
{
InitEntityByDbInfo(this.txtbox_PalletNo.Text);
}
}
}
/// <summary>
/// 清空TextBox
/// </summary>
private void updateTextBoxToEmpty()
{
string empty = string.Empty;
this.txtbox_SalType.Text = empty;
this.txtbox_ProductId.Text = empty;
this.txtbox_TreadStamp.Text = empty;
this.txtbox_RealPalletQty.Text = empty;
this.txtbox_BatchNo.Text = empty;
this.txtbox_MaterName.Text = empty;
}
private ILog log { get { return Log.Store[this.GetType().FullName]; } }
}
}
与串口通信相关
最新推荐文章于 2023-11-01 15:19:36 发布