Пример #1
0
        /// <summary>
        /// 绑定RFID卡与商品
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBindingRfid_Click(object sender, EventArgs e)
        {
            if (curCardId == null)
            {
                MessageBox.Show("请放卡!");
                return;
            }
            if (dgvBindingItem.CurrentRow == null)
            {
                MessageBox.Show("请选择商品!");
            }
            if (curCardId.Equals(""))
            {
                Random rand = new Random();
                int    temp = rand.Next(1, 10000);
                for (; USDataAccess.Select($"select * from [terminal] where [cardId] like '{temp}';").Rows.Count > 0;)
                {
                    temp = rand.Next(1, 10000);
                }
                curCardId = temp.ToString();
            }

            for (; !serialPort.ReadAllData().Contains("addr:0x0001 para:0x");)
            {
                serialPort.Write($"CM+WRITE -addr=0x01 -value=0x{curCardId}");
            }
            USDataAccess.Insert($"insert into [terminal] values(0,'{curCardId}',{dgvBindingItem.CurrentRow.Cells[0].Value.ToString()});");

            curCardId = null;
        }
        /// <summary>
        /// 定时器,负责检查货柜上商品变动情况
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timerSerialPort_Tick(object sender, EventArgs e)
        {
            try
            {
                //读取数据,看看有没有新的RFID标签添加到货柜上
                foreach (var str in serialPort.ReadAllData().Split('\n'))
                {
                    if (str.Contains("addr:0x0001 read data:0x"))
                    {
                        //查询对应卡id的信息
                        string cardId = str.Substring(24, 4);
                        //判断rfid标签是否绑定
                        if (cardId.Equals("0000"))
                        {
                            return;
                        }

                        var dataTable = USDataAccess.Select($"select * from [terminal] where [cardId] like '{cardId}';");
                        if (dataTable.Rows.Count > 0)
                        {
                            //判断是添加商品到货柜还是拿走商品
                            if (int.Parse(dataTable.Rows[0][0].ToString()) == 0)
                            {
                                USDataAccess.Update($"update [terminal] set [tid] = {curTid} where [cardId] like '{cardId}';");
                            }
                            else
                            {
                                USDataAccess.Delete($"delete from [terminal] where [cardId] like '{cardId}';");
                                serialPort.Write("CM+WRITE -addr=0x01 -value=0x0000");
                            }
                            dgvDisplay.DataSource = USDataAccess.Select($"select tis as 所在货柜, name as 商品名, price as 价格, manufacturer as 生产厂家, productiondate as 生产日期, validuntil as 有效期, count(commodityId) as 库存数 from[terminal] as t1 join[commodity] as t2 on t1.commodityId = t2.id and tid = {curTid} group by commodityId;");
                        }
                    }
                }
                serialPort.Write("CM+READ -addr=0x01");
            }
            catch (UnauthorizedAccessException)
            {
                MessageBox.Show("遇到了一些问题,请检查串口是否被占用!", "异常");
                return;
            }
        }