树莓派没有发送到windowsform

时间:2017-12-23 13:14:54

标签: c#

任何想法为什么我的视觉工作室不会从pi发送到windowsform但是当我使用我学校的视觉工作室时它会发送相同的版本)。我是否必须下载任何特定的东西或任何东西才能使它工作?请帮忙。

这里也是我的代码。如果有人可以通过它来帮助我(学校的项目)会很酷!非常感谢!

 string strConnectionString =
        ConfigurationManager.ConnectionStrings["DataCommsDBConnection"].ConnectionString;

    DataComms dataComms;

    public delegate void myprocessDataDelegate(string strData);

    private void saveWaterSensorDataToDB(string strTime, string strWaterValue, string strStatus)
    {
        SqlConnection myConnect = new SqlConnection(strConnectionString);

        string strCommandText = "INSERT MySensor (TimeOccurred, SensorValue, SensorStatus) +" +
            "VALUES (@time, @value, @status)";

        SqlCommand updateCmd = new SqlCommand(strCommandText, myConnect);
        updateCmd.Parameters.AddWithValue("@time", strTime);
        updateCmd.Parameters.AddWithValue("@value", strWaterValue);
        updateCmd.Parameters.AddWithValue("@status", strStatus);

        myConnect.Open();

        int result = updateCmd.ExecuteNonQuery();

        myConnect.Close();
    }

    private string extractStringValue(string strData, string ID)
    {
        string result = strData.Substring(strData.IndexOf(ID) + ID.Length);
        return result;
    }

    private float extractFloatValue(string strData, string ID)
    {
        return (float.Parse(extractStringValue(strData, ID)));
    }

    private void handleWaterSensorData(string strData, string strTime, string ID)
    {
        string strWaterValue = extractStringValue(strData, ID);

        txtWaterValue.Text = strWaterValue;
        txtWaterLevel.Text = strWaterValue;

        float fWaterValue = extractFloatValue(strData, ID);
        string status = "";
        if (fWaterValue <= 500)
            status = "Dry";
        else
            status = "Wet";
        txtRoomStatus.Text = status;


    }

    private void handleButtonData(string strData, string strTime, string ID)
    {
        string strbuttonValue = extractStringValue(strData, ID);

        txtButtonValue.Text = strbuttonValue;
        txtDoorBell.Text = strbuttonValue;
    }

    private void extractSensorData(string strData, string strTime)
    {
        if (strData.IndexOf("WaterLevel=") != -1)
            handleWaterSensorData(strData, strTime, "WaterLevel=");
        else if (strData.IndexOf("BUTTON=") != -1)
            handleButtonData(strData, strTime, "BUTTON=");
    }

    public void handleSensorData(string strData)
    {
        string dt = DateTime.Now.ToString();
        extractSensorData(strData, dt);

        string strMessage = dt + ":" + strData;
        lbDataComms.Items.Insert(0, strMessage);

    }

    public void processDataReceive(string strData)
    {
        myprocessDataDelegate d = new myprocessDataDelegate(handleSensorData);
        lbDataComms.Invoke(d, new object[] { strData });
    }

    public void commsDataReceive(string dataReceived)
    {
        processDataReceive(dataReceived);
    }

    public void commsSendError(string errMsg)
    {
        MessageBox.Show(errMsg);
        processDataReceive(errMsg);
    }

    private void InitComms()
    {
        dataComms = new DataComms();
        dataComms.dataReceiveEvent += new DataComms.DataReceivedDelegate(commsDataReceive);
        dataComms.dataSendErrorEvent += new DataComms.DataSendErrorDelegate(commsSendError);
    }



    public frmDataComms()
    {
        InitializeComponent();
    }

    private void frmDataComms_Load(object sender, EventArgs e)
    {
        InitComms();
    }

    private void btnClear_Click(object sender, EventArgs e)
    {
        lbDataComms.Items.Clear();
    }

    private void btnSendLight_Click(object sender, EventArgs e)
    {
        dataComms.sendData("SENDWATER");
    }

    private void btnSendButton_Click(object sender, EventArgs e)
    {
        dataComms.sendData("SENDBUTTON");
    }

    private void btnCmd_Click(object sender, EventArgs e)
    {
        dataComms.sendData(txtCmd.Text);
    }

    private void btnSendAll_Click(object sender, EventArgs e)
    {
        dataComms.sendData("SENDALL");
    }

    private void btnSendTemp_Click(object sender, EventArgs e)
    {
        dataComms.sendData("SENDTEMP");
    }
}

表格***

namespace DataCommsRpi
{
public sealed class StartupTask : IBackgroundTask
{
    const int MODE_SENDWATER = 1;
    const int MODE_SENDBUTTON = 2;
    const int MODE_SENDALL = 3;
    const int MODE_SENDTEMP = 4;
    static int curMode;

    Pin waterPin = Pin.AnalogPin2;
    IButtonSensor button = DeviceFactory.Build.ButtonSensor(Pin.DigitalPin4);
    Pin tempPin = Pin.AnalogPin0;

    DataComms dataComms;

    string strDataReceived = "";

    private bool buttonPressed = false;
    private bool prevButtonStatus = false;

    int moistureAdcValue = 800;
    double tempDegree = 20;
    private bool moistureWet = false;
    private bool moistureDry = false;
    private bool tempHot = false;
    private bool tempCold = false;

    private void Sleep(int NoOfMs)
    {
        Task.Delay(NoOfMs).Wait();
    }

    private async void startWaterMonitoring()
    {
        int iPrevAdcValue = 800, iReadAdcValue, iDiff;
        await Task.Delay(100);
        while (true)
        {
            Sleep(500);
            iReadAdcValue = DeviceFactory.Build.GrovePi().AnalogRead(waterPin);
            if (iPrevAdcValue > iReadAdcValue)
                iDiff = iPrevAdcValue - iReadAdcValue;
            else
                iDiff = iReadAdcValue - iPrevAdcValue;
            iPrevAdcValue = iReadAdcValue;
            if (iDiff < 100)
                moistureAdcValue = iReadAdcValue;
        }

    }

    private async void startTempMonitoring()
    {

        await Task.Delay(100);
        int adcValue; double tempCalculated = 0, R;

        while (true)
        {
            Sleep(1000);
            adcValue = DeviceFactory.Build.GrovePi().AnalogRead(tempPin);

            int B = 4250, R0 = 100000;
            R = 100000 * (1023.0 - adcValue) / adcValue;
            tempCalculated = 1 / (Math.Log(R / R0) / B + 1 / 298.15) - 273.15;
            if (!Double.IsNaN(tempCalculated))
                tempDegree = tempCalculated;


        }

    }

    private async void startButtonMonitoring()
    {
        await Task.Delay(100);
        while(true)
        {
            Sleep(100);
            string buttonState = button.CurrentState.ToString();
            if (buttonState.Equals("On"))
            {
                Sleep(100);
                buttonState = button.CurrentState.ToString();
                if(buttonState.Equals("On"))
                {
                    buttonPressed = true;
                }
            }
        }
    }


    private void commsDataReceive(string dataReceived)
    {
        strDataReceived = dataReceived;
        Debug.WriteLine("Data Received : " + strDataReceived);
    }

    private void sendDataToWindows(string strDataOut)
    {
        try
        {
            dataComms.sendData(strDataOut);
            Debug.WriteLine("Sending Msg : " + strDataOut);
        }

        catch(Exception)
        {
            Debug.WriteLine("ERROR. Did you forget to initComms()?");
        }
    }

    private void initComms()
    {
        dataComms = new DataComms();
        dataComms.dataReceiveEvent += new DataComms.DataReceivedDelegate(commsDataReceive);

    }

    private void handleModeSendWater()
    {
        if (moistureAdcValue <= 500)
            moistureWet = true;
        else
            moistureWet = false;
        if (moistureAdcValue <= 500 && moistureDry != moistureWet)
            sendDataToWindows("WaterLevel=" + moistureAdcValue + " wet");
        if (moistureAdcValue > 500 && moistureDry != moistureWet)
            sendDataToWindows("WaterLevel=" + moistureAdcValue + " dry");
        moistureDry = moistureWet;

        if (strDataReceived.Equals("SENDBUTTON"))
        {
            curMode = MODE_SENDBUTTON;
            Debug.WriteLine("===Entering MODE_SENDBUTTON===");
        }
        else if (strDataReceived.Equals("SENDALL"))
        {
            curMode = MODE_SENDALL;
            Debug.WriteLine("===Entering MODE_SENDALL");
        }
        else if (strDataReceived.Equals("SENDTEMP"))
        {
            curMode = MODE_SENDTEMP;
            Debug.WriteLine("===Entering MODE_SENDTEMP");
        }

        strDataReceived = "";
    }

    private void handleModeSendTemperature()
    {
        if (tempDegree <= 31)
            tempCold = true;
        else
            tempCold = false;
        if (tempDegree <= 31 && tempCold != tempHot)
        {
            Sleep(3000);
            sendDataToWindows("Temperature = " + tempDegree.ToString("N2") + " : cold");
        }

        else if (tempDegree >= 32 && tempCold != tempHot)
        {
            Sleep(3000);
            sendDataToWindows("Temperature = " + tempDegree.ToString("N2") + " : hot");
        }


        if (strDataReceived.Equals("SENDBUTTON"))
        {
            curMode = MODE_SENDBUTTON;
            Debug.WriteLine("===Entering MODE_SENDBUTTON===");
        }
        else if (strDataReceived.Equals("SENDALL"))
        {
            curMode = MODE_SENDALL;
            Debug.WriteLine("===Entering MODE_SENDALL");
        }
        else if (strDataReceived.Equals("SENDWATER"))
        {
            curMode = MODE_SENDWATER;
            Debug.WriteLine("===Entering MODE_SENDWATER===");
        }
    }

    private void handleModeSendButton()
    {
        if(buttonPressed != prevButtonStatus)
        {
            sendDataToWindows("BUTTON=" + buttonPressed);
        }

        prevButtonStatus = buttonPressed;
        buttonPressed = false;

        if(strDataReceived.Equals("SENDWATER"))
        {
            curMode = MODE_SENDWATER;
            Debug.WriteLine("===Entering MODE_SENDWATER===");
        }
        else if(strDataReceived.Equals("SENDALL"))
        {
            curMode = MODE_SENDALL;
            Debug.WriteLine("===Entering MODE_SENDALL");
        }
        else if (strDataReceived.Equals("SENDTEMP"))
        {
            curMode = MODE_SENDTEMP;
            Debug.WriteLine("===Entering MODE_SENDTEMP");
        }
    }
    private void handleModeSendAll()
    {
        Sleep(5000);
        sendDataToWindows("Water Level=" + moistureAdcValue);
        sendDataToWindows("Temperature = " + tempDegree.ToString("N2"));

        if (strDataReceived.Equals("SENDWATER"))
        {
            curMode = MODE_SENDWATER;
            Debug.WriteLine("===Entering MODE_SENDWATER");
        }
        else if (strDataReceived.Equals("SENDBUTTON"))
        {
            curMode = MODE_SENDBUTTON;
            Debug.WriteLine("===Entering MODE_SENDBUTTON");
        }
        else if (strDataReceived.Equals("SENDTEMP"))
        {
            curMode = MODE_SENDTEMP;
            Debug.WriteLine("===Entering MODE_SENDTEMP");
        }
    }

    public void Run(IBackgroundTaskInstance taskInstance)
    {
        // 
        // TODO: Insert code to perform background work
        //
        // If you start any asynchronous methods here, prevent the task
        // from closing prematurely by using BackgroundTaskDeferral as
        // described in http://aka.ms/backgroundtaskdeferral
        //

        initComms();

        startButtonMonitoring();
        startWaterMonitoring();
        startTempMonitoring();

        curMode = MODE_SENDWATER;

        Debug.WriteLine("===Entering MODE_SENDWATER===");


        while (true)
        {
            Sleep(300);

            if (curMode == MODE_SENDWATER)
                handleModeSendWater();
            else if (curMode == MODE_SENDBUTTON)
                handleModeSendButton();
            else if (curMode == MODE_SENDALL)
                handleModeSendAll();
            else if (curMode == MODE_SENDTEMP)
                handleModeSendTemperature();
            else
                Debug.WriteLine("ERROR: Invalid Mode. Please check your logic");
        }
    }
}
}

0 个答案:

没有答案