⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 formmain.cs

📁 windows mobile 5.0的经典客户机、服务器socket通信实例
💻 CS
字号:
//--------------------------------------------------------------------
// FILENAME: FromMain.vb
//
// Copyright(c) 2006 Symbol Technologies Inc. All rights reserved.
//
// DESCRIPTION:
// Demonstrates the usage of scanning, SQLCE and sockets.  The data scanned is stored in a SQLCE database on the device. 
// The data queued in the database is then tranfered to the PC via socket communication.  The VB_CATHost application
// must must be running on the PC for the data transfer to occur. VB_CATHost will repsond to the data recevied from the 
// device by sending the date and time of the receipt.  The databse on the device is then updated with the response 
// received from the host
//
//NOTES:
//
// 
//--------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.IO;
using System.Threading;
using CS_CATClient;

namespace CS_CATSample
{
    public partial class FormMain : Form
    {
        DBComponent dbComponent = null;
        Communication comm = null;

        private Symbol.Barcode.Reader MyReader = null;
        private Symbol.Barcode.ReaderData MyReaderData = null;
        private System.EventHandler BarEventHandler = null;

        // notification events and data
        private NotifyEvents notifyCommEvent;
        Object notifyCommData = null;

        public FormMain()
        {
            InitializeComponent();

            this.textBoxHostIP.Focus();
        }

        // The database is created for queuing the data and the communication module is initiated
        private void FormMain_Load(object sender, EventArgs e)
        {
            Application.DoEvents();

            //MessageBox.Show("This sample is provided for demonstration purpose only. You are welcome to modify the code to suit your requirement. \r\n" +
            //"Please refer to the MSDN help files for description of all SQLCE and socket related calls.", "CATClient");


            // If we can initialize the Reader
            //if (this.InitReader())
            {
                // Start a read on the reader
            //    this.StartRead();
            }


            // Create the DB
            dbComponent = new DBComponent();
            dbComponent.DBDelete = true;
            dbComponent.DBEncrypt = true;

            string curFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName);
            dbComponent.DBName = curFolder + "\\CAT_DB.sdf";
            dbComponent.DBPassword = "d@$123";

            dbComponent.DBCreate();
         
            // Attach the grid to the data source
            dbComponent.DBOpen();

            dbComponent.DBExecute("CREATE TABLE DataQueue (UniqueId int IDENTITY (0,1) PRIMARY KEY, DataSent ntext, DataRcvd ntext, SentFlag bit)");

            // Start the communication module
            comm = new Communication();
            comm.Notify += new Communication.NotifyEventHandler(Comm_Notify);
        }

        // Dispose all the objects before exiting
        private void FormMain_Closing(object sender, CancelEventArgs e)
        {
            dbComponent.DBClose();
            comm.Close();

            // Terminate reader
            this.TermReader();
        }

        // Update the data grid with the latest data from SQLCE
        private void DataGrid_Refresh()
        {
            try
            {
                dataGrid1.DataSource = dbComponent.DBQuery("SELECT DataSent,DataRcvd FROM DataQueue").Tables[0].Copy();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        // If the data is manually entered instead of scanning, accept it and queue it in SQLCE
        private void textBoxScan_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Return && textBoxScan.Text.Length > 0)
            {
                DataInsert(textBoxScan.Text, "");
                textBoxScan.Text = "";
            }
        }

        // Insert the data into the queue
        private void DataInsert(string dataSent, string dataRcvd)
        {
            string cmd = "INSERT INTO DataQueue (DataSent, DataRcvd, SentFlag) VALUES('" + 
                dataSent + "','" + dataRcvd + "', 0)";

            dbComponent.DBExecute(cmd);
            DataGrid_Refresh();
        }

        // Establishes the socket connection between the device and PC
        private void buttonConnect_Click(object sender, EventArgs e)
        {
            if (buttonConnect.Text == "Connect")
                comm.Open(textBoxHostIP.Text, Convert.ToInt32(textBoxPort.Text));
            else
                comm.Close();
        }

        private void buttonConnect_KeyDown(object sender, KeyEventArgs e)
        {
            // Checks if the key pressed was an enter button (character code 13)
            if (e.KeyValue == (char)13)
                buttonConnect_Click(this, e);
        }

        private void buttonExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void buttonExit_KeyDown(object sender, KeyEventArgs e)
        {
            // Checks if the key pressed was an enter button (character code 13)
            if (e.KeyValue == (char)13)
                buttonExit_Click(this, e);
        }

        private void FormMain_KeyUp(object sender, KeyEventArgs e)
        {
            this.textBoxHostIP.Focus();
        }

        // Catch socket notification events
        private void Comm_Notify(NotifyEvents nEvent, object data)
        {
            try
            {
                lock (this)
                {
                    notifyCommEvent = nEvent;
                    notifyCommData = data;
                    this.Invoke(new EventHandler(ProcessCommNotifications));
                }
            }
            catch
            {
            }
        }


        private void ProcessCommNotifications(object sender, EventArgs e)
        {
            switch (notifyCommEvent)
            {
                case NotifyEvents.Connected:
                    statusBar1.Text = "Connected to the host";
                    buttonConnect.Text = "Disconnect";
                    break;

                case NotifyEvents.DataReceived:
                    DataGrid_Refresh();
                    break;

                case NotifyEvents.Disconnected:
                    statusBar1.Text = "Disconnected from the host";
                    buttonConnect.Text = "Connect";
                    break;

                default:
                    statusBar1.Text = notifyCommEvent.ToString() + "-" + notifyCommData.ToString();
                    break;
            }
        }
        
        // Initialize the barcode reader.
        private bool InitReader()
        {
            // If reader is already present then fail initialize
            if (this.MyReader != null)
            {
                return false;
            }

            // Create new reader, first available reader will be used.
            this.MyReader = new Symbol.Barcode.Reader();

            // Create reader data
            this.MyReaderData = new Symbol.Barcode.ReaderData(
                Symbol.Barcode.ReaderDataTypes.Text,
                Symbol.Barcode.ReaderDataLengths.MaximumLabel);

            // Create event handler delegate
            this.BarEventHandler = new EventHandler(BarReader_ReadNotify);

            // Enable reader, with wait cursor
            this.MyReader.Actions.Enable();

            this.MyReader.Parameters.Feedback.Success.BeepTime = 0;
            this.MyReader.Parameters.Feedback.Success.WaveFile = "\\windows\\alarm3.wav";

            return true;
        }

        
        // Stop reading and disable/close barcode reader
        private void TermReader()
        {
            // If we have a reader
            if (this.MyReader != null)
            {
                // Disable the reader
                this.MyReader.Actions.Disable();

                // Free it up
                this.MyReader.Dispose();

                // Indicate we no longer have one
                this.MyReader = null;
            }

            // If we have a reader data
            if (this.MyReaderData != null)
            {
                // Free it up
                this.MyReaderData.Dispose();

                // Indicate we no longer have one
                this.MyReaderData = null;
            }
        }

        
        // Start a read on the barcode reader
        private void StartRead()
        {
            // If we have both a reader and a reader data
            if ((this.MyReader != null) &&
                 (this.MyReaderData != null))
            {
                // Submit a read
                this.MyReader.ReadNotify += this.BarEventHandler;
                this.MyReader.Actions.Read(this.MyReaderData);
            }
        }

        
        // Stop all reads on the barcode reader
        private void StopRead()
        {
            // If we have a reader
            if (this.MyReader != null)
            {
                // Flush (Cancel all pending reads)
                this.MyReader.ReadNotify -= this.BarEventHandler;
                this.MyReader.Actions.Flush();
            }
        }

        
        // Read complete or failure notification
        private void BarReader_ReadNotify(object sender, EventArgs e)
        {
            Symbol.Barcode.ReaderData TheReaderData = this.MyReader.GetNextReaderData();

            // If it is a successful read (as opposed to a failed one)
            if (TheReaderData.Result == Symbol.Results.SUCCESS)
            {
                // Handle the data from this read
                this.HandleData(TheReaderData);

                // Start the next read
                this.StartRead();
            }
        }

        
        // Handle data from the barcode reader
        private void HandleData(Symbol.Barcode.ReaderData TheReaderData)
        {
            DataInsert(TheReaderData.Text, "");
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -