C#字符串转数值形式(串口助手)

作者在开发串口助手的过程中,需要将角度方框中的值与增量方框中的值相加进行发送,因此,则需要先将textbox中的字符串转换成数值,再由数值计算相加,然后使用串口调用函数将数据进行写入。经过尝试,作者终于将该功能实现,现与读者们分享。

 private void button4_Click(object sender, EventArgs e)
        {
            if (serialPort1.IsOpen)    //如果串口设备已经打开了
            {
                if (radioButton2.Checked)  //如果是以字符的形式发送数据
                {
                    if (radioButton3.Checked)
                    {
                        try
                        {
                            string strNum = textBox3.Text ;
                            string strNum1 = textBox9.Text;
                            int age1 = int.Parse(strNum);
                            int age2 = int.Parse(strNum1);
                            int age3 = age1 + age2;
                            string mynum = Convert.ToString(age3);
                            serialPort1.WriteLine("F8"+mynum);//写数据
                        }
                        catch
                        {
                            MessageBox.Show("窗口数据写入错误", "错误");//出错提示
                        }
                    }

如读者们有任何问题,欢迎前来评论区进行提问,有更好的方法也欢迎与作者进行讨论。

猜你喜欢

转载自blog.csdn.net/weixin_61056520/article/details/129735094