与串口通信相关

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]; } }

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值