Exemplo n.º 1
0
        private void sendbt()
        {
            if (bflag == true && bdll == "BTLibCs")
            {
                CalibCs.BTLibCs.BTLib_Initialize();
                IntPtr btest = CalibCs.SerialFuncCs.CreateFile(com + ":", CalibCs.SerialFuncCs.GENERIC_WRITE | CalibCs.SerialFuncCs.GENERIC_READ, 0, IntPtr.Zero, CalibCs.SerialFuncCs.OPEN_EXISTING, CalibCs.SerialFuncCs.FILE_ATTRIBUTE_NORMAL, IntPtr.Zero);
                if (btest != IntPtr.Zero)
                {
                    CalibCs.SerialFuncCs.DCB portconfig = new CalibCs.SerialFuncCs.DCB();
                    bool test2 = CalibCs.SerialFuncCs.GetCommState(btest, ref portconfig);
                    portconfig.BaudRate = SerialFuncCs.CBR_19200;
                    portconfig.Flag32  |= SerialFuncCs.fBinary;
                    portconfig.Parity   = SerialFuncCs.NOPARITY;
                    portconfig.ByteSize = 8;
                    portconfig.StopBits = SerialFuncCs.ONESTOPBIT;
                    test2 = CalibCs.SerialFuncCs.SetCommState(btest, ref portconfig);



                    int  licznik   = 0;
                    uint modemstat = 0;
                    while (modemstat != 176 && licznik != 30)
                    {
                        licznik += 1;

                        label1.Text = "CZEKAM NA PO£¥CZENIE 30 sekund: " + licznik.ToString();
                        label1.Refresh();
                        label1.Focus();
                        System.Threading.Thread.Sleep(1000);

                        SerialFuncCs.GetCommModemStatus(btest, ref modemstat);
                    }
                    if (licznik >= 30)
                    {
                        SerialFuncCs.CloseHandle(btest);
                        CalibCs.BTLibCs.BTLib_DeInitialize();


                        label1.Text = "KONIEC CZASU";
                    }
                    else
                    {
                        System.IO.FileStream file = new FileStream(ufile, FileMode.Open, FileAccess.Read);
                        byte[] buffer             = new byte[file.Length];
                        file.Read(buffer, 0, buffer.Length);
                        long   lenght = file.Length;
                        byte[] flaga  = new byte[1];
                        flaga[0] = 1;
                        byte[] bytec   = BitConverter.GetBytes(lenght);
                        uint   bytestw = Convert.ToUInt32(buffer.Length);
                        uint   byteswr = new uint();
                        label1.Text = "WYSY£AM";
                        label1.Focus();
                        label1.Refresh();
                        System.Threading.Thread.Sleep(3000);

                        test2 = SerialFuncCs.WriteFile(btest, flaga, Convert.ToUInt32(flaga.Length), ref byteswr, IntPtr.Zero);

                        System.Threading.Thread.Sleep(3000);

                        test2 = SerialFuncCs.WriteFile(btest, bytec, Convert.ToUInt32(bytec.Length), ref byteswr, IntPtr.Zero);
                        System.Threading.Thread.Sleep(3000);
                        //pEvtMask = 0;
                        //SerialFuncCs.WaitCommEvent(btest, ref pEvtMask, IntPtr.Zero);
                        //label3.Text = pEvtMask.ToString();
                        //MessageBox.Show("Wyœlij2");
                        //	pEvtMask = 0;
                        //	SerialFuncCs.WaitCommEvent(btest, ref pEvtMask, IntPtr.Zero);
                        //label1.Text = pEvtMask.ToString();;
                        //while(pEvtMask != SerialFuncCs.EV_TXEMPTY)
                        //{
                        //}
                        //status_l.Text = "WYSY£ANIE";
                        test2 = SerialFuncCs.WriteFile(btest, buffer, bytestw, ref byteswr, IntPtr.Zero);
                        System.Threading.Thread.Sleep(2000);
                        flaga[0] = 3;
                        test2    = SerialFuncCs.WriteFile(btest, flaga, Convert.ToUInt32(flaga.Length), ref byteswr, IntPtr.Zero);
                        SerialFuncCs.CloseHandle(btest);
                        CalibCs.BTLibCs.BTLib_DeInitialize();
                        //pEvtMask = 0;
                        //label2.Text = pEvtMask.ToString();
                        label1.Text = "DANE WYS£ANE";
                        label1.Focus();
                        label1.Refresh();

                        file.Close();
                        testflag = 1;
                        confirm_send();
                    }
                }
                else
                {
                    MessageBox.Show("Niepoprawny port COM");
                }
            }
            else if (bflag == true && bdll == "MSStack")
            {
                sendbtms();
            }
        }
        //catch
        //{
        //	string connectionString;
        //	connectionString = "DataSource=Baza.sdf; Password=matrix1";
        //	SqlCeConnection cn = new SqlCeConnection(connectionString);
        //	SqlCeCommand cmd6 = new SqlCeCommand("UPDATE opcje SET devstat = 'Err')", cn);
        //	cmd6.Prepare();
        //	cmd6.ExecuteNonQuery();
        //	cn.Close();
        //}
        private void recive2()
        {
            //	string connectionString;
            //	connectionString = "DataSource=Baza.sdf; Password=matrix1";
            //	SqlCeConnection cn = new SqlCeConnection(connectionString);
            if (bflag == true && bdll == "BTLibCs")
            {
                CalibCs.BTLibCs.BTLib_Initialize();
                this.label1.Text = "OTWIERAM PORT";
                label1.Refresh();
                IntPtr btest = CalibCs.SerialFuncCs.CreateFile(com + ":", CalibCs.SerialFuncCs.GENERIC_WRITE | CalibCs.SerialFuncCs.GENERIC_READ, 0, IntPtr.Zero, CalibCs.SerialFuncCs.OPEN_EXISTING, CalibCs.SerialFuncCs.FILE_ATTRIBUTE_NORMAL, IntPtr.Zero);
                if (btest != IntPtr.Zero)
                {
                    //	MessageBox.Show("Com ok");
                    CalibCs.SerialFuncCs.DCB portconfig = new CalibCs.SerialFuncCs.DCB();
                    bool test2 = CalibCs.SerialFuncCs.GetCommState(btest, ref portconfig);
                    if (test2 == true)
                    {
                        //		MessageBox.Show("Get com st ok");
                        portconfig.BaudRate = SerialFuncCs.CBR_115200;
                        portconfig.Flag32  |= SerialFuncCs.fBinary;
                        portconfig.Parity   = SerialFuncCs.NOPARITY;
                        portconfig.ByteSize = 8;
                        portconfig.StopBits = SerialFuncCs.ONESTOPBIT;
                        test2 = CalibCs.SerialFuncCs.SetCommState(btest, ref portconfig);
                        if (test2 == true)
                        {
                            byte[] bytecount = new byte[1024];
                            uint   bytestw   = new uint();
                            uint   byteswr   = new uint();
                            uint   pEvtMask  = SerialFuncCs.EV_RXCHAR;
                            uint   pEvtMask2 = SerialFuncCs.EV_RXCHAR;
                            uint   CTS       = SerialFuncCs.EV_CTS;
                            SerialFuncCs.WaitCommEvent(btest, ref pEvtMask, IntPtr.Zero);

                            System.IO.FileStream file = new FileStream(dfile, FileMode.Create, FileAccess.Write);
                            byte[] flaga = new byte[1];
                            flaga[0] = 19;
                            int  licznik   = 0;
                            uint modemstat = 0;
                            while (modemstat != 176 && licznik != 30)
                            {
                                licznik += 1;

                                label1.Text = "CZEKAM NA PO£¥CZENIE 30 sekund: " + licznik.ToString();
                                label1.Focus();
                                System.Threading.Thread.Sleep(1000);
                                label1.Refresh();
                                SerialFuncCs.GetCommModemStatus(btest, ref modemstat);
                            }

                            if (licznik >= 30)
                            {
                                file.Close();
                                CalibCs.BTLibCs.BTLib_DeInitialize();
                                SerialFuncCs.CloseHandle(btest);
                                label1.Text = "KONIEC CZASU";
                                label1.Refresh();
                            }
                            else
                            {
                                //
                                //									if (devstat == "Err")
                                //									{
                                //										flaga[0] = 5;
                                //
                                //									}
                                this.label1.Text = "CZEKAM NA DANE";
                                label1.Refresh();
                                test2 = SerialFuncCs.WriteFile(btest, flaga, Convert.ToUInt32(flaga.Length), ref byteswr, IntPtr.Zero);
                                System.Threading.Thread.Sleep(400);
                                test2 = SerialFuncCs.WriteFile(btest, impdate2, 16, ref byteswr, IntPtr.Zero);
                                System.Threading.Thread.Sleep(1000);

                                test2 = SerialFuncCs.ReadFile(btest, bytecount, Convert.ToUInt32(bytecount.Length), ref byteswr, IntPtr.Zero);

                                uint lenght = BitConverter.ToUInt32(bytecount, 0);
                                this.label1.Text = "ODBIERAM DANE: " + (lenght / 1024).ToString() + " Kb";
                                label1.Refresh();
                                byte[] buffer = new byte[lenght];
                                SerialFuncCs.WaitCommEvent(btest, ref pEvtMask2, IntPtr.Zero);
                                test2    = SerialFuncCs.ReadFile(btest, buffer, Convert.ToUInt32(buffer.Length), ref byteswr, IntPtr.Zero);
                                flaga[0] = 2;
                                test2    = SerialFuncCs.WriteFile(btest, flaga, Convert.ToUInt32(flaga.Length), ref byteswr, IntPtr.Zero);

                                SerialFuncCs.CloseHandle(btest);
                                CalibCs.BTLibCs.BTLib_DeInitialize();

                                this.label1.Text = "ZAPISUJE DANE: " + (lenght / 1024).ToString() + "Kb " + "PROSZÊ CZEKAÆ";
                                label1.Refresh();
                                file.Write(buffer, 0, Convert.ToInt32(lenght));

                                file.Close();
                                this.label1.Text = "DANE ZAPISANE";
                                label1.Refresh();
                            }
                        }
                        else
                        {
                        }
                    }
                }
                else
                {
                    MessageBox.Show("Niepoprawny port com");
                    this.Close();
                }
            }
            else if (bflag == true && bdll == "MSStack")
            {
                recivebtms();
            }
            //SqlCeCommand cmd6 = new SqlCeCommand("UPDATE opcje SET devstat = 'Err')", cn);
            //cmd6.Prepare();
            //cmd6.ExecuteNonQuery();
            //	cn.Close();
        }