본문 바로가기

프로젝트/휴대폰을 BarCode Reader 로 사용하여 C# DB에서 이용하기

DataGrid 프로그램과 앱 연동하기 ( 검색, 추가 )

 

동영상 확인하기

DataGrid 프로그램에 추가한 코드

public static System.IO.Ports.SerialPort    port;     //  다른 폼에서 접근하기 의해  port변수설정

string barCode = "";
 bool barCodeOn=false;

 

 private void btBarcode_Click(object sender, EventArgs e)
        {           
            if (! barCodeOn)
            {
                port = myPort;    // PotOpen폼에서 myPort 이용하기 위해 
                PortOpen frm = new PortOpen();
                DialogResult re= frm.ShowDialog();    
                if (re == DialogResult.OK )
                {
                    btBarcode.Text = "바코드\nOn";
                    btBarcode.BackColor = Color.LawnGreen;
                    barCodeOn = true; 
                    timer1.Enabled = true;  
                }
                 
            }
            else
            {   
                barCodeOn= false;    
                btBarcode.Text = "바코드\nOff";
                btBarcode.BackColor = SystemColors.Control;
                myPort.Close();
                timer1.Enabled = false;
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            string msg=myPort.ReadExisting();
            if (msg == "") return;
            string cmd = msg.Substring(0, 1);
            string msgText =msg.Substring(1);
           
            switch ( cmd) 
            {
                case "B":  txtSerch.Text = msgText;
                           barCode = msgText;
                           foreach (DataRow ro in dt.Rows)
                            {                               
                               if (ro[0].ToString() == msgText )  //  일치하는 바코드가 있다면
                                {
                                   myPort.Write(ro[1].ToString());
                                   return;                            
                                 }                               
                             }
                           myPort.Write("-1");
                           break;
                 case "A":   
                             txtSerch.Text = msgText;
                             if (dt.Rows[0][0].ToString() == "")
                             {
                                dt.Rows[0][0] = barCode;
                                dt.Rows[0][1] = msgText;
                              }
                              else
                              {
                                 DataRow row = dt.NewRow();
                                 row[0] = barCode;
                                 row[1] = msgText;
                                 dt.Rows.Add(row);
                                 NoEdit = false;
                                 myPort.Write("1");
                               }
                               break;
            }
          
        }


using System;
using System.IO.Ports;
using System.Text;
using System.Windows.Forms;

namespace DataGridXML
{
    public partial class PortOpen : Form
    {
        public PortOpen()
        {
            InitializeComponent();
        }


        private void PortOpen_Load(object sender, EventArgs e)
        {
            comboPort.Items.Clear();

            foreach (var items in SerialPort.GetPortNames())
                comboPort.Items.Add(items);
           
        }

        private static void Open()
        {
            throw new NotImplementedException();
        }

        private void bt연결_Click(object sender, EventArgs e)
        {
            if (comboPort.Text == "") return;
            try
            {
                if (Form1.port.IsOpen)
                {
                    Form1.port.Close();
                }
                else
                {
                    Form1.port.PortName = comboPort.SelectedItem.ToString();

                    Form1.port.Encoding = Encoding.UTF8;
                    Form1.port.Open();
                    DialogResult = DialogResult.OK;
                    Close();
                }
            }
            catch (Exception)
            {
                MessageBox.Show("연결안됨", "ComPort", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
             
        }

        private void bt취소_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();    
        }
    }
}