Beispiel #1
0
        public int ConnToPrinter(BluetoothLibNet.BTST_DEVICEINFO btdevice)
        {

            BtRet = BluetoothLibNet.Api.BTGetServiceInfo(btdevice);
            // register this device in registry
            // (can be skiped if already done before)
            BtRet = BluetoothLibNet.Api.BTRegisterDeviceInfo(btdevice);
            if (BtRet != BluetoothLibNet.Def.BTERR_SUCCESS)
            {
                SetErrorEvent("BT register printer info Error");
                BluetoothLibNet.Api.BTDeInitialize();
                PrinterFound = false;
                _connected = false;
                BTPrinterInit();
                return BtRet;
            }
            PrinterFound = true;

            /*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(btdevice, 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();
                _connected = false;
                return BtRet;
            }
            // dummy write to etablish bt connection
            byte[] prnout = new byte[1];

            //BtRet = BluetoothLibNet.Api.BTSetPassKey("111");
            BtRet = BluetoothLibNet.Api.BTConnectSerial(BluetoothLibNet.Def.BTCONNECT_SERIAL_SERVER,
                1000, 1000);
            //BtRet = BluetoothLibNet.Api.BTSendSerialData(ref prnout, 1, 1);
            // 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();
                BtRet = BluetoothLibNet.Def.BTERR_CONNECTION_FAILED;
                _connected = false;
                return BtRet;
            }



            PortWrite(prnout, 0, hSerial);
            for (int i = 0; i < 76; i++)
            {
                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);
            }
            BtRet = BluetoothLibNet.Def.BTERR_CONNECTED;
            _connected = true;
            return BtRet;
        }
Beispiel #2
0
        public int ConnToPrinter3(BluetoothLibNet.BTST_DEVICEINFO btdevice)
        {
            try
            {
                if (sp.IsOpen)
                    return BluetoothLibNet.Def.BTERR_CONNECTED;
                int status = 0;
                BtRet = BluetoothLibNet.Api.BTGetLibraryStatus(ref status);

                if (status == BluetoothLibNet.Def.BTSTATUS_NOT_INITIALIZED)
                {
                    BTPrinterInit();
                }
                //BtRet = BluetoothLibNet.Api.BTGetDefaultDeviceInfo(btDefaultdevice, BluetoothLibNet.Def.BTPORT_SERIAL);
                BtRet = BluetoothLibNet.Api.BTSelectDevice(IntPtr.Zero, BluetoothLibNet.Def.BTPORT_SERIAL);
                if (BtRet != BluetoothLibNet.Def.BTERR_SUCCESS)
                {
                    SetErrorEvent("Стандартный принтер не найден!");
                    if (Program.Settings.TypedSettings[0]["BTPrinterAddress"] != null &&
                        Program.Settings.TypedSettings[0]["BTPrinterAddress"] != DBNull.Value &&
                        Program.Settings.TypedSettings[0]["BTPrinterAddress"].ToString() != string.Empty &&
                        Program.Settings.TypedSettings[0]["BTPrinterAddress"].ToString() != "00:00:00:00:00:00")
                    {

                        SetStatusEvent("Поиск принтера {0}", Program.Settings.TypedSettings[0].BTPrinterAddress.ToUpper());
                        BtRet = SearchDevices();
                        if (BtRet != BluetoothLibNet.Def.BTERR_SUCCESS)
                        {
                            SetErrorEvent("Принтер не найден!");
                            throw new BTConnectionFailedException();
                            //return BtRet;
                        }
                        SetStatusEvent("Установка принтера {0} стандартным", Program.Settings.TypedSettings[0].BTPrinterAddress.ToUpper());

                        SetDefaultDevice(Program.Settings.TypedSettings[0].BTPrinterAddress.ToUpper());

                    }
                    else
                    {
                        SetErrorEvent("Стандартный принтер не установлен");
                        //                        return BtRet;
                        throw new BTConnectionFailedException();
                    }

                }

                PrinterFound = true;

                SetStatusEvent("{0} BT принтер найден!", Program.Settings.TypedSettings[0].BTPrinterAddress.ToUpper());

                return ConnToPrinter3();
            }
            catch (Exception err)
            {
                BtRet = BluetoothLibNet.Def.BTERR_FAILED;
                _connected = false;
                SetErrorEvent(err.ToString());
                SetErrorEvent("Ошибка подключения. Переподключите принтер и попробуйте еще раз.");
                if (OnConnectionError != null && doEvents)
                    OnConnectionError();
                return BtRet;
            }

            /*
            sp.Open();
            if (sp.IsOpen)
            {
                SetStatusEvent("BT Printer connected");
                BtRet = BluetoothLibNet.Def.BTERR_CONNECTED;
                _connected = true;
            }
            else
            {
                BtRet = BluetoothLibNet.Def.BTERR_FAILED;
                SetErrorEvent("BT Printer connection failed");
                _connected = false;
            }
            return BtRet;*/
        }
Beispiel #3
0
        public int ConnToPrinter2(BluetoothLibNet.BTST_DEVICEINFO btdevice)
        {
            BtRet = BluetoothLibNet.Api.BTSelectDevice(btdevice, BluetoothLibNet.Def.BTPORT_SERIAL);
            if (BtRet != BluetoothLibNet.Def.BTERR_SUCCESS)
            {
                SetErrorEvent("BT register printer info Error");
                BluetoothLibNet.Api.BTDeInitialize();
                PrinterFound = false;
                _connected = false;
                return BtRet;
            }
            byte[] prnout = new byte[1];
            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)
            {
                SetErrorEvent("BT Open Error");
                BluetoothLibNet.Api.BTDeInitialize();
                BtRet = BluetoothLibNet.Def.BTERR_CONNECTION_FAILED;
                _connected = false;
                return BtRet;
            }



            PortWrite(prnout, 0, hSerial);
            for (int i = 0; i < 76; i++)
            {
                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);
            }
            BtRet = BluetoothLibNet.Def.BTERR_CONNECTED;
            _connected = true;
            return BtRet;
        }