예제 #1
0
        private void tmrData_Tick(object sender, EventArgs e)
        {
            int  i;
            uint recLength;

            recLength = 25;
            // Get the Live data, to show in gauge
            txBuf[0] = (byte)CMD.GET_LIVE_DATA;
            txBuf[1] = 2;
            //USB.SendReceivePacket(txBuf, 2, rxBuf, ref recLength);
            string port = cbxPorts.SelectedItem.ToString();

            if (port == "")
            {
                return;
            }
            System.IO.Ports.SerialPort myPort = new System.IO.Ports.SerialPort(port);
            if (myPort.IsOpen == false) //if not open, open the port
            {
                myPort.Open();
            }
            //do your work here
            if (myPort.IsOpen == false) //if not open, open the port
            {
                return;
            }
            String data;

            data = myPort.ReadLine();
            myPort.Close();
            string [] dht = data.Split(',');
            //s.dataPtr = rxBuf[18];
            //i = 19;
            //convertData(ref currData, ref i);
            //gTemp.Value = currData.temp;
            //gPressure.Value = currData.pressure;
            //gHumid.Value = currData.humidity;
            if (dht.Length != 3)
            {
                return;
            }
            gHumid.Value    = float.Parse(dht[0]);
            gTemp.Value     = float.Parse(dht[1]);
            gPressure.Value = int.Parse(dht[2]);

            if (stt == 0)
            {
                //Start Excel and get Application object.
                oXL         = new Excel.Application();
                oXL.Visible = true;

                //Get a new workbook.
                oWB    = (Excel._Workbook)(oXL.Workbooks.Add(Missing.Value));
                oSheet = (Excel._Worksheet)oWB.ActiveSheet;
            }
            //Add table headers going cell by cell.
            oSheet.Cells[stt + 1, 1] = gTemp.Value.ToString();
            oSheet.Cells[stt + 1, 2] = gPressure.Value.ToString();
            oSheet.Cells[stt + 1, 3] = gHumid.Value.ToString();
            //oSheet.Cells[1, 4] = "Salary";


            DateTime timeNow = DateTime.Now;

            w[stt].humidity = gHumid.Value;
            w[stt].temp     = gTemp.Value;
            w[stt].pressure = gPressure.Value;
            w[stt++].time   = timeNow;
            if (stt == 999)
            {
                stt = 0;
            }
            Redraw(0);

            goLive = false;
            if (goLive) // Only load data to w if graph shows live data from PIC
            {
                recLength = 64;
                txBuf[0]  = (byte)CMD.GET_FIRST_DATA;
                txBuf[1]  = 2;
                USB.SendReceivePacket(txBuf, 2, rxBuf, ref recLength);
                loadData(10);
                for (i = 0; i < 5; i++)
                {
                    txBuf[0] = (byte)CMD.GET_NEXT_DATA;
                    txBuf[1] = 2;
                    USB.SendReceivePacket(txBuf, 2, rxBuf, ref recLength);
                    loadData(10);
                }

                txBuf[0] = (byte)CMD.GET_CURR_DATA;
                txBuf[1] = 2;
                USB.SendReceivePacket(txBuf, 2, rxBuf, ref recLength);
                loadData((int)s.dataPtr);
            }
        }