Form1.cs 11.5 KB
using DeviceLibrary;
using OnlineStore.Common;
using OnlineStore;
using System.Text;

namespace TinLabel
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public static PuYueRFID_FR540SP_C2S RFID = new PuYueRFID_FR540SP_C2S();
        public static PrinterHelper printerHelper = new PrinterHelper();
        Task? RFIDTask;
        bool rfidwork = false;
        private void Form1_Load(object sender, EventArgs e)
        {
            Label loadinfolabel = new Label();
            loadinfolabel.AutoSize = false;
            //loadinfolabel.Size = new Size(Width, Height);
            loadinfolabel.Dock = DockStyle.Fill;
            loadinfolabel.TextAlign = ContentAlignment.MiddleCenter;
            loadinfolabel.Text = crc.GetString("Res0001.03426328","设备初始化中...");
            loadinfolabel.Font = new Font("Arial", 21.75F, FontStyle.Regular, GraphicsUnit.Point);
            this.Controls.Add(loadinfolabel);
            loadinfolabel.BringToFront();
            Task.Run(() =>
            {
                if (init(out string errmsg))
                {
                    this.Controls.Remove(loadinfolabel);
                }
                else
                {
                    this.Invoke(new Action(() =>
                    {
                        loadinfolabel.ForeColor = Color.Red;
                        loadinfolabel.Text = errmsg;
                    }));
                }
            });

            textBox_qrcode.KeyUp += TextBox_qrcode_KeyUp;
            RFIDTask = Task.Run(() =>
            {
                RFIDProcess();
            });
            btn_printlabel.Enabled = false;
            btn_cancel.Visible = false;
            btn_rewriterfid.Visible = false;
            btn_readrfid.Enabled = false;
            label_codeinfo.Text = crc.GetString("Res0002.a336d735","请扫码");
            label_process.Text = string.Empty;
        }

        private void TextBox_qrcode_KeyUp(object? sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Tab || e.KeyCode == Keys.Enter)
            {
                process();
            }
        }
        ReelParam? reelParam = new ReelParam();
        void process()
        {
            textBox_qrcode.SelectAll();
            reelParam = new ReelParam();
            var rightcode = textBox_qrcode.Text.Trim();
            if (!string.IsNullOrEmpty(rightcode))
            {
                var cs = rightcode.Split('|');
                if (cs.Length >= 7)
                {
                    reelParam.PN = cs[0].Substring(1);
                    reelParam.LOT = cs[3].Substring(1);
                    reelParam.EXP = cs[4].Substring(1);
                    reelParam.SN = cs[5].Substring(1);
                    reelParam.WareCode = rightcode;
                    reelParam.ReeID = reelParam.SN;
                    reelParam.RFID = "S" + reelParam.SN;

                    label_codeinfo.Text = $"SN:   {reelParam.SN}\r\nPN:   {reelParam.PN}\r\nRFID: {reelParam.RFID}";
                    label_codeinfo.ForeColor = Color.DarkGreen;
                    if (cb_autoprint.Checked)
                    {
                        PrintLabel();
                    }
                    btn_printlabel.Enabled = true;
                    btn_readrfid.Enabled = true;
                }
                else
                {
                    reelParam = null;
                    btn_printlabel.Enabled = false;
                    label_codeinfo.Text = crc.GetString("Res0003.8cac7dc7","条码不正确,请重新扫码.");
                    label_codeinfo.ForeColor = Color.Red;
                    textBox_qrcode.Text = "";
                }
            }
        }
        void PrintLabel()
        {
            if (reelParam == null)
            {
                MessageBox.Show(crc.GetString("Res0004.92e1e70c","未读到有效的条码,无法打印"));
                return;
            }
            btn_printlabel.Enabled = false;
            var PrintTask = RobotManage.printerHelper.PrintLabel(reelParam, Setting.Printer_Labelname);
            PrintTask.Wait(5000);
            if (PrintTask.IsCompleted)
            {
                var (result, msg) = PrintTask.Result;
                if (result)
                {
                    rfidwork = true;
                    btn_printlabel.Enabled = false;
                    btn_readrfid.Enabled = false;
                }
                else
                {
                    MessageBox.Show(crc.GetString("Res0005.05c6d4d0","打印失败:") + msg);
                    btn_printlabel.Enabled = true;
                    return;
                }
            }
            else
            {
                MessageBox.Show(crc.GetString("Res0006.15521f9b","打印机响应超时."));
                return;
            }

        }
        void RFIDProcess()
        {

            while (!this.IsDisposed)
            {
                Task.Delay(500);
                if (reelParam == null)
                {
                    LogUtil.info("未读到有效的条码,无法写入rfid");
                    continue;
                }
                if (!rfidwork)
                    continue;
                this.Invoke(() =>
                {
                    btn_cancel.Visible = true;
                    label_process.Text = crc.GetString("Res0007.8f9184ed","请将标签放置到RFID读卡器上,并稍作等待");
                    label_process.ForeColor = Color.Yellow;
                });
                var epc = reelParam.RFID.PadRight(16);
                var r = RFID.ReOpen();
                RFID.WriteByte(550, new byte[] { 0x40, 0x00 });
                Thread.Sleep(300);
                RFID.WriteEPC(Encoding.ASCII.GetBytes(epc));
                Thread.Sleep(300);
                if (RobotManage.RFID.ReadEPC(16, out byte[] data))
                {
                    if (data[0] != 0xFF)
                    {
                        var ds = Encoding.ASCII.GetString(data).Trim();
                        if (ds == reelParam.RFID)
                        {
                            LogUtil.info("RFID 写入成功:" + ds);
                            rfidwork = false;
                            this.Invoke(() =>
                            {
                                label_process.Text = crc.GetString("Res0008.0c5aab19","RFID写入成功! ") + " RFID:" + ds;
                                label_process.ForeColor = Color.DarkGreen;
                                btn_cancel.Visible = false;
                                btn_rewriterfid.Visible = false;
                                btn_printlabel.Enabled = true;
                                btn_readrfid.Enabled = true;
                                //label_codeinfo.Text = "请扫码";
                            });
                            continue;
                        }
                        else
                        {
                            LogUtil.info("失败的 RFID:" + ds);
                        }
                    }
                }
            }
        }
        public bool init(out string errmsg)
        {
            bool IsLoadOk = true;
            errmsg = "";
            if (!printerHelper.Connection(Setting.Printer_IP))
            {
                errmsg += crc.GetString("Res0009.f11b67ff","标签打印机打开失败") + "\n";
                IsLoadOk = false;
            }
            else
                LogUtil.info("标签打印机打开成功");


            RFID.Open(Setting.Device_RFID_IP);
            if (!RFID.IsConn)
            {
                IsLoadOk = false;
                errmsg += crc.GetString("Res0010.279f5658","RFID连接失败,IP:") + $"{Setting.Device_RFID_IP}\n";
            }
            else
                LogUtil.info("RFID连接成功,IP:" + Setting.Device_RFID_IP);

            if (!IsLoadOk)
            {
                //label_process.Text = msg;
            }

            return IsLoadOk;
        }

        private void label1_Click(object sender, EventArgs e)
        {
            ConfigHelper.AdvanceConfigForm.ShowEditDialog(this);
        }

        private void btn_printlabel_Click(object sender, EventArgs e)
        {
            LogUtil.info("手动点击标签打印");
            PrintLabel();
        }

        private void 关于ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutBox1 aboutBox1 = new AboutBox1();
            aboutBox1.ShowDialog();
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            btn_cancel.Visible = false;
            rfidwork = false;
            label_process.Text = crc.GetString("Res0011.cbadd79b","RFID写入已取消");
            btn_rewriterfid.Visible = true;
            btn_printlabel.Enabled = true;
            btn_readrfid.Enabled = true;
        }

        private void btn_rewriterfid_Click(object sender, EventArgs e)
        {
            btn_rewriterfid.Visible = false;
            rfidwork = true;
            btn_cancel.Visible = true;
        }

        private void 标签设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new Frm_LabelEdit().ShowDialog();

        }
        private void btn_readrfid_click(object sender, EventArgs e)
        {
            if (reelParam == null)
            {
                MessageBox.Show(crc.GetString("Res0004.92e1e70c","未读到有效的条码,无法打印"));
                return;
            }
            label_process.Text = crc.GetString("Res0012.2228db58","正在读取RFID");
            label_process.ForeColor = Color.Yellow;
            Application.DoEvents();
            Thread.Sleep(500);
            if (RobotManage.RFID.ReadEPC(16, out byte[] data))
            {
                if (data[0] != 0xFF)
                {
                    var ds = Encoding.ASCII.GetString(data).Trim();
                    if (ds == reelParam.RFID)
                    {
                        LogUtil.info("RFID与条码相符! " + " RFID:" + ds);
                        this.Invoke(() =>
                        {
                            label_process.Text = crc.GetString("Res0013.cb82b33c","RFID与条码相符! ") + " RFID:" + ds;
                            label_process.ForeColor = Color.DarkGreen;

                            //label_codeinfo.Text = "请扫码";
                        });

                    }
                    else
                    {
                        LogUtil.info("RFID与条码不相符! " + " RFID:" + ds);
                        this.Invoke(() =>
                        {
                            label_process.Text = crc.GetString("Res0014.2f81e963","RFID与条码不相符! ") + " RFID:" + ds;
                            label_process.ForeColor = Color.Red;

                            //label_codeinfo.Text = "请扫码";
                        });
                    }
                    btn_cancel.Visible = false;
                    btn_rewriterfid.Visible = false;
                    btn_printlabel.Enabled = true;
                    btn_readrfid.Enabled = true;
                }
                else
                {
                    label_process.Text = crc.GetString("Res0015.5a9787db","RFID读取失败");
                    label_process.ForeColor = Color.Red;
                }
            }
        }

        private void btn_barcodeenter_Click(object sender, EventArgs e)
        {
            process();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            textBox_qrcode.Focus();
        }
    }
}