最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#使用TextBox作數(shù)據(jù)輸入方法

 更新時(shí)間:2021年06月01日 11:17:49   作者:怪都督  
筆者最近需要上位機(jī)與下位機(jī)進(jìn)行數(shù)據(jù)交互,在廣泛參考大佬的資料后,較為完善地使用Textbox控件進(jìn)行數(shù)據(jù)輸入的功能。感興趣的可以了解一下

筆者最近需要上位機(jī)與下位機(jī)進(jìn)行數(shù)據(jù)交互,在廣泛參考大佬的資料后,較為完善地使用Textbox控件進(jìn)行數(shù)據(jù)輸入的功能。
程序段主要功能:實(shí)現(xiàn)輸入數(shù)據(jù)并轉(zhuǎn)換成byte數(shù)組再通過串口發(fā)送至下位機(jī)。

讀取TextBox控件中數(shù)據(jù)并發(fā)送

private void Botton_Float_Click(object sender, EventArgs e)
 {
     if (button1.Text == "關(guān)閉串口")
     {
         if(TextBox_Tem_Cal.Text != String .Empty) //判斷數(shù)據(jù)輸入框是否為空
         {
             HexMath CRC = new HexMath();
             Byte[] buffer = new Byte[6];
             
             float tem_cal_float = float.Parse(TextBox_Tem_Cal.Text);
             Byte[] float_byte_array = new Byte[4];
             float_byte_array = FloatToBytes(tem_cal_float);

             buffer[0] = float_byte_array[0];
             buffer[1] = float_byte_array[1];
             buffer[2] = float_byte_array[2];
             buffer[3 ] = float_byte_array[3];
             
             CRC.CalculateCrc16(buffer, out buffer[5], out buffer[4]);
             serialPort1.Write(buffer, 0, 6);
         }
        else
         {
             MessageBox.Show("校準(zhǔn)數(shù)據(jù)不能為空");
         }
     }
     else
     {
         MessageBox.Show("串口未打開");
     }
 }

限制TextBox控件輸入數(shù)據(jù)

private void TextBox_Tem_Cal_KeyPress(object sender, KeyPressEventArgs e)//在TextBox中按下按鍵時(shí)觸發(fā)事件,保證只能輸入數(shù)字
{
    //判斷按鍵是不是要輸入的類型。
    if (((int)e.KeyChar < 48 || (int)e.KeyChar > 57) && (int)e.KeyChar != 8 && (int)e.KeyChar != 46)
        e.Handled = true;

    //小數(shù)點(diǎn)的處理。
    if ((int)e.KeyChar == 46)                           //小數(shù)點(diǎn)
    {
        if (TextBox_Tem_Cal.Text.Length <= 0)
            e.Handled = true;   //小數(shù)點(diǎn)不能在第一位
        else
        {
            float f;
            float oldf;
            bool b1 = false, b2 = false;
            b1 = float.TryParse(TextBox_Tem_Cal.Text, out oldf);
            b2 = float.TryParse(TextBox_Tem_Cal.Text + e.KeyChar.ToString(), out f);
            if (b2 == false)
            {
                if (b1 == true)
                    e.Handled = true;
                else
                    e.Handled = false;
            }
        }
    }
}

Float 與 byte數(shù)組 互轉(zhuǎn)

private static byte[] FloatToBytes(float data) 
{
    unsafe
    {
        byte* pdata = (byte*)&data;
        byte[] byteArray = new byte[sizeof(float)];
        for (int i = 0; i < sizeof(float); ++i)
            byteArray[i] = *pdata++;
        return byteArray;
    }
}
private static float BytesToFloat(byte[] data)
{
    unsafe
    {
        float a = 0.0F;
        byte i;
        byte[] x = data;
        void* pf;
        fixed (byte* px = x)
        {
            pf = &a;
            for (i = 0; i < data.Length; i++)
            {
                *((byte*)pf + i) = *(px + i);
            }
        }
        return a;
    }
}

程序參考:

TextBox輸入限制
C# byte與float轉(zhuǎn)換

到此這篇關(guān)于C#使用TextBox作數(shù)據(jù)輸入方法的文章就介紹到這了,更多相關(guān)C# TextBox數(shù)據(jù)輸入內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

额尔古纳市| 海晏县| 永登县| 那坡县| 开封县| 宝丰县| 江门市| 镇安县| 彰化县| 叶城县| 永寿县| 乌什县| 宣威市| 玉林市| 梁山县| 盈江县| 杭锦旗| 九江市| 临颍县| 张北县| 上饶县| 历史| 页游| 剑阁县| 乌拉特后旗| 洪泽县| 霸州市| 新乡县| 新密市| 平潭县| 旬阳县| 沁源县| 太谷县| 赤峰市| 许昌市| 北流市| 汉源县| 公主岭市| 保德县| 临汾市| 灵寿县|