2012年4月30日 星期一

PDA連線BlueToothPrinter(使用InTheHand元件)

公司目前案子需求是利用 PDA 連線至 BTPrinter 印出條碼;PDA 系統:WinCE5.0,BTPrinter 型號:MP3200。 雖然有寫過 WinCE 系統下執行的程式經驗,但連線至 BT 設備還是頭一遭,所以利用 google 搜尋相關資訊後,發現很多人推薦 InTheHand 元件,因此決定要用它來試看看。 其實網路上有太多程式可以參考了,從中選一個最簡單明瞭的程式做為參考,並自己動手來做做看。 程式畫面 (借用 Winform 程式畫面):
畫面中 [SearchDevices] 尋找 BlueTooth 設備,選擇欲連線之設備 (此範例為:MP3200BT-8005),按下 [Connect] 與其設備連線。並且可利用 [SendByte] 鈕與 BTPrinter 溝通。

using與變數定義如下:
using InTheHand.Net.Bluetooth;
using InTheHand.Net.Sockets;

private BluetoothClient bluetoothClient;
private Guid service = BluetoothService.SerialPort;
BluetoothService.SerialPort這個搞了我好久,原本範例中的變數定義為BluetoothService.DialupNetworking,但始終都沒有連上線,所以提醒大家不妨先看看自己要連線的設備規格再去做選擇以甚麼樣的方式連線。
搜尋設備之程式如下:
private void btnSearch_Click(object sender, EventArgs e)
        {           
            // 先 new 一個 bluetoothClient
            bluetoothClient = new BluetoothClient();
            Cursor.Current = Cursors.WaitCursor;
            // 存放 bluetooth 設備資訊
            BluetoothDeviceInfo[] bluetoothDeviceInfo = { };
            // 利用 new 出來的 bluetoothClient 搜尋 buletoothDevices
            bluetoothDeviceInfo = bluetoothClient.DiscoverDevices(10);
            // 搜尋到的設備資訊放至 Combobox 中
            cmbBTDev.DataSource = bluetoothDeviceInfo;
            // 顯示為 DeviceName
            cmbBTDev.DisplayMember = "DeviceName";
            // 值為 DeviceAddress(Connect 時會用到)
            cmbBTDev.ValueMember = "DeviceAddress";
            cmbBTDev.Focus();
            Cursor.Current = Cursors.Default;
        }
[Connect] 程式如下:
private void btnConnect_Click(object sender, EventArgs e)
        {            
            if (cmbBTDev.SelectedValue != null)
            {
                try
                {
                    //先將Combobox之value轉換為BluetoothAddress,之後再轉為BluetoothEndPoint並利用serialport連線
                    bluetoothClient.Connect(new BluetoothEndPoint((BluetoothAddress)cmbBTDev.SelectedValue, service));
                    MessageBox.Show("Connected");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }            
        }
[SendByte]程式如下:
if (bluetoothClient.Connected == true)
            {
                //FullCmd為byte[]
                bluetoothClient.Client.Send(FullCmd);
            }
目前以上面程式完成連線並成功傳值至Bluetooth設備,當然傳甚麼樣的值要參考每個設備的protocol,因此在此不做示範。下次見:)

MP3200 簡介:http://www.cino.com.tw/products/mp/mp3200.htm
InTheHand 元件:http://inthehand.com/content/32feet.aspx
程式參考:codeproject

2012年4月19日 星期四

Form間傳參數(C#)

由於測試程式 (以下稱 A) 需模擬成多台真實程式測試其效能。有一種方法是測試程式當作一個物件,程式開啟時依照設定創造多個物件出來以達到其效果。當然另一種更簡單的方法是撰寫另一隻程式 (以下稱 B) 呼叫執行測試程式並傳入不同參數達到。
以下是 B 程式參數傳入 Function 撰寫:
private void btnRun_Click(object sender, EventArgs e)
        {
            // 參數值
            int StartPort = Convert.ToInt32(txtSPort.Text);
            // 模擬數量
            int VirtualNum = Convert.ToInt32(txtVNum.Text);
            // 跑回圈的方式開啟多台
            for (int i = StartPort; i < StartPort+VirtualNum; i++)
            {
                // 要開啟的程式名稱寫在這
                string exename = "APLoadTesting.exe";

                string target = Application.StartupPath + @"\" + exename;                
                
                ProcessStartInfo pInfo = new ProcessStartInfo(target);
                // 要傳的參數寫在這
                pInfo.Arguments = i.ToString();
                using (Process p = new Process())
                {
                    p.StartInfo = pInfo;
                    p.Start();
                }

                // 避免開啟程式共用資料會錯亂
                Thread.Sleep(2000);
            }                                  
        }
A程式之Program.cs中的Application.Run改成以下方式:
static void Main(string[] Args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            //有參數則接參數的方式開啟
            if (Args.Length > 0)
            {
                Application.Run(new Form1(Args[0]));
            }             
            else//無參數傳入則正常開啟
            {
                Application.Run(new Form1());
            }            
        }
A程式的Form1.cs加入以下程式:
public Form1(string EQPID)//EQPID為傳進來的參數
        {
            InitializeComponent();            
            txtIP.Text = "127.0.0.1";
            txtPort.Text = EQPID;
            txtEQID.Text = EQPID;
        }