已有141人关注
写了一段代码,运行后不知弹出的这是什么东东?很神奇的。
发表在C#答疑区 2017-09-29 悬赏:10 学分
是否精华
版块置顶:

尊敬的老师:

     下午好

      我写了一段代码,运行后,我先点写入按钮,弹出02000200,然后我又点击确定,又弹出111940402000200EB,我再点击确定,居然软件死机,然后在com.Write(sWriteData);这里停下,弹出一个未处理的invalid Operation Exception  这是什么意思呢?为什么会弹出这样的异常?我哪里代码未处理了?那我该怎么处理呢?

请老师帮忙修改下代码。

我写的源代码如下:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

using System.IO.Ports;



namespace 三菱PLC

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();


        }


        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)

        {

 //串口初始化

            com = new SerialPort("com5", 9600, Parity.Even, 7, StopBits.One);


            if (com.IsOpen)

            { com.Close(); }

            com.Open();


        }


        private void textBox1_TextChanged(object sender, EventArgs e)

        {


        }



// ASCII转字符


        

        

            public static string Chr(int asciiCode)

            {

                if (asciiCode >= 0 && asciiCode <= 255)

                {

                    System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();

                    byte[] byteArray = new byte[] { (byte)asciiCode };

                    string strCharacter = asciiEncoding.GetString(byteArray);

                    return (strCharacter);

                }


                else

                {

                    throw new Exception("ASCII Code is not valid");//ASCII 缺失


                }

            }

    

 //字符转ASCII

   


        public static int Asc(string character)

  {

            if (character.Length == 1)

            {


                   System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();

                   int intAsciiCode = (int)asciiEncoding.GetBytes(character)[0];

                   return (intAsciiCode);


            }

            else

            {


                 throw new Exception("character is not valid");

                

            }

  

        

  }






  // 写入串口的命令字符的和校验

       

        public string SumCheck(string data)

        {

            int sum = 0;


            for (int i = 0; i < data.Length; i++)

            {

                sum += Asc(data.Substring(i, 1));


               // sum += Convert.ToInt16(data.Substring(i, 1));

            }

            string res = sum.ToString("X");

            res = res.Substring(res.Length - 2, 2);

            return res;

        }

 

    

    

      private void btnWrite_Click(object sender, EventArgs e)

  {

            string[] write = new string[] { "2", "2" };//将准备写入PLC的值

            // 将要写入PLC的值转化为16进制数,补齐2个字节,注意高低字节需要交换

            string sWriteData = "";

            for (int i = 0; i < write.Length; i++)

            {


                int val = Convert.ToInt32(write[i].Length > 0 ? write[i] : "0");

                string s = val.ToString("X");

                while (s.Length < 4)

                {


                    s = "0" + s;

                }

                sWriteData += s.Substring(2, 2) + s.Substring(0, 2);


            }


            MessageBox.Show(sWriteData);

            //写入命令,1表示写入,1194表示D202这个地址的16进制,04表示D202,D203为4个BYTE,

            //1194=(202*2)+4096的16进制数,至于用它表示D202的起始位置,三菱故意要这么麻烦了。

            

            sWriteData = "1119404" + sWriteData + Chr(3);

            // chr(2)和chr(3)是构成命令的标识符,然后加上校验和,命令组织完成。


            sWriteData = Chr(2) + sWriteData + SumCheck(sWriteData);

            MessageBox.Show(sWriteData);

            //写入串口

            com.Write(sWriteData);

            //byte[]data = Encoding.ASCII.GetBytes(sWriteData);

            //com.Write(data,0,data.Length);


        }


    }

      

    }


   

   

   

异常图片如下:



2017-09-29 17:34:48编辑
分享到:
精彩评论 1
小禾斗
学分:7308 LV13
TA的每日心情
奋斗
2017-04-21 14:00:25
2017-09-30
沙发

读者朋友:

  您好,提示端口关闭,把打开窗口的代码写到窗体的Load事件里面,再试一下。

首页上一页 1 下一页尾页 1 条记录 1/1页
手机同步功能介绍
友情提示:以下图书配套资源能够实现手机同步功能
明日微信公众号
明日之星 明日之星编程特训营
客服热线(每日9:00-17:00)
400 675 1066
mingrisoft@mingrisoft.com
吉林省明日科技有限公司Copyright ©2007-2022,mingrisoft.com, All Rights Reserved长春市北湖科技开发区盛北大街3333号长春北湖科技园项目一期A10号楼四、五层
吉ICP备10002740号-2吉公网安备22010202000132经营性网站备案信息 营业执照