private BTPrintClass() { WaitPrintTimeDefault = Program.Default.WaitPrintTimeDefault; for (int iCnt = 0; iCnt < BTDEF_MAX_INQUIRY_NUM; iCnt++) { bt_di[iCnt] = new Calib.BluetoothLibNet.BTST_DEVICEINFO(); } //BTPrinterInit(); }
public void TestPrint(string addr) { const int BTDEF_MAX_INQUIRY_NUM = 16; for (int iCnt = 0; iCnt < 16; iCnt++) { bt_di[iCnt] = new Calib.BluetoothLibNet.BTST_DEVICEINFO(); } IntPtr[] bt_hdev; bt_hdev = new IntPtr[BTDEF_MAX_INQUIRY_NUM + 1]; int bt_dmax; int BtRet; bool PrinterFound; //DialogResult Result; string PrinterAdr = addr;//"00:80:37:17:78:DA"; // 'Set some variables used by serial IntPtr hSerial; int i, j, iii, ii = 0; int CommStatus = 0; string label1 = "***** TEST PRINT CS.NET *****" + "\r" + "\n" + "IT-600 or DT-X11" + "\r" + "\n" + "CASIO" + "\r" + "\n" + "www.casio.co.jp/English/system/" + "\r" + "\n" + "\r" + "\n" + "CASIO Computer Co., Ltd." + "\r" + "\n" + "\r" + "\n" + "6-2, Hon-machi 1-chome" + "\r" + "\n" + "\r" + "\n" + "Shibuya-ku, Tokyo 151-8543, Japan" + "\r" + "\n" + "\r" + "\n" + "Tel.: +81-3-5334-4771" + "\r" + "\n" + "Fax.: +81-3-5334-4656" + "\r" + "\n" + "\r" + "\n" + "\r" + "\n" + "\r" + "\n" + "\r" + "\n" + "\r" + "\n" + "\r" + "\n"; Encoding ascii = Encoding.ASCII; byte[] prnout = ascii.GetBytes(label1); //Status.Text = SetStatusEvent("initialize bluetooth modul..."); //Cursor.Current = Cursors.WaitCursor; // initialize bluetooth device (power on) BtRet = BluetoothLibNet.Api.BTInitialize(); //Cursor.Current = Cursors.Default; if (BtRet != BluetoothLibNet.Def.BTERR_SUCCESS) { //Status.Text = ""; //Result = MessageBox.Show( SetErrorEvent("BT Init Error"); return; } string swork = new string(' ', 82); bt_li.LocalName = swork.ToCharArray(); bt_li.LocalAddress = " ".ToCharArray(); bt_li.LocalDeviceMode = 0; bt_li.LocalClass1 = 0; bt_li.LocalClass2 = 0; bt_li.LocalClass3 = 0; bt_li.Authentication = false; bt_li.Encryption = false; //Status.Text = SetStatusEvent("get local device info..."); BtRet = BluetoothLibNet.Api.BTGetLocalInfo(bt_li); if (BtRet != BluetoothLibNet.Def.BTERR_SUCCESS) { //Status.Text = ""; //Result = MessageBox.Show( SetErrorEvent("BT get local info Error"); BluetoothLibNet.Api.BTDeInitialize(); return; } // set fixed values for our local bt device // (can be skiped if already done before) bt_li.LocalDeviceMode = BluetoothLibNet.Def.BTMODE_BOTH_ENABLED; bt_li.Authentication = false; bt_li.Encryption = false; //Status.Text = SetStatusEvent("set new local device info..."); BtRet = BluetoothLibNet.Api.BTSetLocalInfo(bt_li); if (BtRet != BluetoothLibNet.Def.BTERR_SUCCESS) { //Status.Text = ""; //Result = MessageBox.Show SetErrorEvent("BT set local info Error"); BluetoothLibNet.Api.BTDeInitialize(); return; } BtRet = BluetoothLibNet.Api.BTRegisterLocalInfo(); if (BtRet != BluetoothLibNet.Def.BTERR_SUCCESS) { //Status.Text = ""; //Result = MessageBox.Show SetErrorEvent("BT register local info Error"); BluetoothLibNet.Api.BTDeInitialize(); return; } /**********************************************************************/ /*******END* *INIT BT PRINTER*****************************************/ /**********************************************************************/ /************BEGIN SERACH PRINTER*************************************/ // search for availible bluetooth devices bt_dmax = BTDEF_MAX_INQUIRY_NUM; //Status.Text = SetStatusEvent("searching bluetooth devices..."); //Cursor.Current = Cursors.WaitCursor; BtRet = BluetoothLibNet.Api.BTInquiry(IntPtr.Zero, ref bt_dmax, 5000); //Cursor.Current = Cursors.Default; if (BtRet != BluetoothLibNet.Def.BTERR_SUCCESS) { //Status.Text = ""; //Result = MessageBox.Show SetErrorEvent("BT Inquiry Error"); BluetoothLibNet.Api.BTDeInitialize(); return; } //Status.Text = "found " + bt_dmax.ToString() + " bluetooth devices!"; SetStatusEvent(String.Format("Found {0} bluetooth devices!", bt_dmax)); PrinterFound = false; swork = ""; for (iii = 0; iii < 82; iii++) swork = swork + " "; for (j = 0; j < bt_dmax; j++) { bt_di[j].DeviceErrorFlag = 0; bt_di[j].DeviceHandle = 0; bt_di[j].DeviceName = swork.ToCharArray(); bt_di[j].DeviceAddress = swork.Substring(1, 18).ToCharArray(); bt_di[j].LocalClass1 = 0; bt_di[j].LocalClass2 = 0; bt_di[j].LocalClass3 = 0; bt_di[j].ProfileNumber = 0; for (i = 0; i < 16; i++) { bt_di[j].ProfileUUID[i] = 0; } } BtRet = BluetoothLibNet.Api.BTGetDeviceInfo(bt_di, bt_dmax, 0); /****************END SEARCH***************************************/ if (BtRet == BluetoothLibNet.Def.BTERR_SUCCESS) { for (i = 0; i < bt_dmax; i++) { string xxx = ""; for (int ia = 0; ia < 17; ia++) { xxx = xxx + bt_di[i].DeviceAddress[ia].ToString(); } if (xxx == PrinterAdr) { // we found the printer and try to get service informations // (can be skiped because we know printer capabilities) /************CONNECT TO PRINTER********************/ BtRet = BluetoothLibNet.Api.BTGetServiceInfo(bt_di[i]); // register this device in registry // (can be skiped if already done before) BtRet = BluetoothLibNet.Api.BTRegisterDeviceInfo(bt_di[i]); if (BtRet != BluetoothLibNet.Def.BTERR_SUCCESS) { //Status.Text = ""; //Result = MessageBox.Show SetErrorEvent("BT register printer info Error"); BluetoothLibNet.Api.BTDeInitialize(); return; } PrinterFound = true; ii = i; } } } else { //Result = MessageBox.Show SetErrorEvent("BT Get device info error"); BluetoothLibNet.Api.BTDeInitialize(); return; } if (PrinterFound == false) { //Status.Text = ""; //Result = MessageBox.Show SetErrorEvent("BT Printer not found!"); BluetoothLibNet.Api.BTDeInitialize(); return; } // set printer as default device // (can be skiped) //Status.Text = SetStatusEvent("Extech BT printer found!"); BtRet = BluetoothLibNet.Api.BTSetDefaultDevice(bt_di[ii], BluetoothLibNet.Def.BTPORT_SERIAL); if (BtRet != BluetoothLibNet.Def.BTERR_SUCCESS) { //Status.Text = ""; //Result = MessageBox.Show SetErrorEvent("BT Printer set default device Error"); BluetoothLibNet.Api.BTDeInitialize(); return; } // open serial communication //Status.Text = SetStatusEvent("try to connect to printer..."); hSerial = PortOpen(string.Format("COM{0}:", Program.Settings.TypedSettings[0].BTComPort), CBR_19200, 8, NOPARITY, ONESTOPBIT, 3000, 3000); if (hSerial.ToInt32() == INVALID_HANDLE_VALUE) { //Status.Text = ""; //MessageBox.Show SetErrorEvent("BT Open Error"); BluetoothLibNet.Api.BTDeInitialize(); return; } // dummy write to etablish bt connection PortWrite(prnout, 0, hSerial); for (i = 0; i < 76; i++) { //Status.Text = SetStatusEvent(String.Format("wait for answer...{0}", i.ToString())); GetCommModemStatus(hSerial, ref CommStatus); if ((CommStatus & (MS_RING_ON | MS_RLSD_ON)) != 0) { break; } Thread.Sleep(200); } // write label to printer //Status.Text = SetStatusEvent("sending datas..."); if (PortWrite(prnout, label1.Length, hSerial) == 1) { //Status.Text = ""; //MessageBox.Show SetErrorEvent("BT Write Error"); } //MessageBox.Show SetStatusEvent("BT Printing"); //Status.Text = SetStatusEvent("disconnect printer..."); // deinitialize bt device (power off) PortClose(hSerial); BluetoothLibNet.Api.BTDeInitialize(); //Status.Text = ""; }