using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; using ORiN2.interop.CAO; namespace QRCode { public partial class Form1 : Form { private delegate void SetTextCallback(string msg); // delegate for processing OnMessage private CaoEngine caoEng; private CaoWorkspaces caoWss; private CaoWorkspace caoWs; private CaoVariable caoVar; // Scanner objects private CaoController caoCtrl; //-----------------------------Initialization---------------------------- public Form1() { InitializeComponent(); //CreateEngine caoEng = new CaoEngine(); caoWss = caoEng.Workspaces; caoWs = caoWss.Item(0); cmdConnectScanner.Enabled = true; btnRead.Enabled = false; cmbMode.SelectedIndex = 0; } // --------------------------------------------------------------- // Connect with the Scanner // --------------------------------------------------------------- private void cmdConnectScanner_Click(object sender, EventArgs e) { String strCom = ""; bool bReadData = false; if(cmbMode.SelectedItem.ToString()=="EVENT") { strCom = "5"; bReadData = false; // Disable ReadData button } else{ strCom = "4"; bReadData = true; // Enable ReadData button } String strConnOpt = "Conn=com:" + numudCom.Value.ToString() + ":38400:N:8:1,Mode=" + strCom + ",Protocol=0:0:0,Timeout=1000"; try { // Connect with the QR Scanner caoCtrl = caoWs.AddController("QRCode", "CaoProv.DENSO.QRCode", "", strConnOpt); // Enable command buttons cmdConnectScanner.Enabled = false; cmdDisconnectScanner.Enabled = true; cmbMode.Enabled = false; numudCom.Enabled = false; // Add event handler for OnMessage caoCtrl.OnMessage += new _ICaoControllerEvents_OnMessageEventHandler(OnMessage); // Get variable object caoVar = caoCtrl.AddVariable("ReadData", ""); // Enable/Disable ReadData button btnRead.Enabled = bReadData; } catch (Exception ex) { cmdDisconnectScanner_Click(sender, e); MessageBox.Show(ex.Message); } } // --------------------------------------------------------------- // Disconnect from the Scanner // --------------------------------------------------------------- private void cmdDisconnectScanner_Click(object sender, EventArgs e) { if (caoVar != null) { Marshal.ReleaseComObject(caoVar); caoVar = null; } // Disconnect from the controller if (caoCtrl != null) { CaoControllers ctrls = caoWs.Controllers; ctrls.Remove(caoCtrl.Name); Marshal.ReleaseComObject(caoCtrl); caoCtrl = null; Marshal.ReleaseComObject(ctrls); ctrls = null; } // Disable command buttons cmdConnectScanner.Enabled = true; cmdDisconnectScanner.Enabled = false; cmbMode.Enabled = true; numudCom.Enabled = true; btnRead.Enabled = false; } // OnMessage event private void OnMessage(CaoMessage pICaoMsg) { try { SetTextCallback SetMsg = new SetTextCallback(SetText); Invoke(SetMsg, pICaoMsg.Value.ToString()); } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void SetText(string msg) { lblDsp.Text += msg; } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { cmdDisconnectScanner_Click(sender, e); // Delete CaoEngine if (caoWs != null) { Marshal.ReleaseComObject(caoWs); caoWs = null; } if (caoWss != null) { Marshal.ReleaseComObject(caoWss); caoWss = null; } if (caoEng != null) { Marshal.ReleaseComObject(caoEng); caoEng = null; } } private void btnClear_Click(object sender, EventArgs e) { lblDsp.Clear(); } private void btnRead_Click(object sender, EventArgs e) { try { lblDsp.Text += caoVar.Value.ToString(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } }