| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 5988 人关注过本帖
标题:c#与三菱PLC进行串口通信
只看楼主 加入收藏
zhangyanpei
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2015-6-6
结帖率:0
收藏
已结贴  问题点数:20 回复次数:4 
c#与三菱PLC进行串口通信
public void Button2Click(object sender, System.EventArgs e)//启动按钮m0.0
        {
            char[]c={(char)2,(char)49,(char)48,(char)49,(char)48,(char)48,(char)48,(char)49,(char)48,(char)49,(char)3,(char)66,(char)55};
            //char[] c = { (char)2, (char)48, (char)49, (char)48, (char)48, (char)48, (char)50, (char)48, (char)3, (char)53, (char)54 };
            
            int i=0;
            string s="";
            
            for (i=0;i<13;i++)
               
            {
                s=s+c[i].ToString();
               
            }
            serialPort1.Write(s);
            ghu ();
            
            
            
        }
        public void Button5Click(object sender, EventArgs e)//停止按钮m0.1
        {
            char[]c={(char)2,(char)49,(char)48,(char)49,(char)48,(char)48,(char)48,(char)49,(char)48,(char)50,(char)3,(char)66,(char)56};
            //char[] c = { (char)2, (char)48, (char)49, (char)48, (char)48, (char)48, (char)50, (char)48, (char)3, (char)53, (char)54 };
            
            int i=0;
            string s="";
            
            for (i=0;i<13;i++)
               
            {
                s=s+c[i].ToString();
               
            }
            serialPort1.Write(s);
            ghu();
        }
        public void ghu()//读取y的状态
        {
        
            char[]c={(char)2,(char)48,(char)48,(char)48,(char)65,(char)48,(char)48,(char)49,(char)3,(char)57,(char)53};
            //char[] c = { (char)2, (char)48, (char)49, (char)48, (char)48, (char)48, (char)50, (char)48, (char)3, (char)53, (char)54 };
            
            int i=0;
            string s="";
            
            for (i=0;i<11;i++)
               
            {
                s=s+c[i].ToString();
               
            }
            serialPort1.Write(s);
            timer2.Enabled=true;
            
            
            }
            
            
        void Timer2Tick(object sender, EventArgs e)
        {
            string data1="";
            bool  tr;
            SolidBrush bush3 = new SolidBrush(Color.Red);
            SolidBrush bush4 = new SolidBrush(Color.Green);
            Graphics g9 = this.pictureBox9.CreateGraphics();
            Graphics g10 = this.pictureBox10.CreateGraphics();
            
            data1=serialPort1.ReadExisting();
            if (data1 != null)
            {
               
            int b = data1.Length;
            if (b>0)
            {
                byte [] h=strToToHexByte(data1.Substring(1,2));//提取有用字符串转化成十进制数组
                byte [] p=strToToHexByte(data1.Substring(3,2));
                int  oy =Convert.ToInt32(h [0]);//将十进制数组转换成整形整数
                int  oj=Convert.ToInt32(p [0]);
                string eu=Convert.ToString(oy, 2);//将整数转化成二进制
                eu =Fill(eu);
                //string er1=Parent.ToSByte(er);
                string ru= Convert.ToString(oj, 2);
                ru =Fill(ru);
               
                if (eu!=""&&ru!="")
                {
                    tr=true;
                    
                }
                else{
                    tr=false;
                    MessageBox.Show("y的状态读取不成功");
                }
               
               
                if (tr==true )
                {
                    
                    int? sum =null;
                    bool ?res=null;
                    for (int l = 0 ;l <eu.Length; l++)
                    {
                        sum =Convert.ToUInt16(eu.Substring(l,1));
                        switch (sum)
                           
                        {
                                case 0: res=false;
                                break;
                                case 1:  res=true ;
                                break ;
                                
                        }
                        if(l==8&&res==true)
                        {
                        
                        g9.FillEllipse(bush3, 0, 0, 20, 20);
                        
                        }
                        else
                        {
                        g10.FillEllipse(bush4, 0, 0, 20, 20);
                        
                        }
                        if(l==7&&res==true)
                        {
                        
                        g10.FillEllipse(bush3, 0, 0, 20, 20);
                        
                        }
                        else
                        {
                        g10.FillEllipse(bush4, 0, 0, 20, 20);
                        
                        }

                        
                    }
                    
                    

                    
                }
            }
            else
            {
               
                MessageBox.Show("y的状态读取不成功");
               
            }
            }
            else{
               
                MessageBox.Show("通信超时");
               
            }
            
            timer2.Enabled=false;
        }


    }
}

[local]1[/local]
一执行到data1时就跳出对话框了,下面的程序怎么不执行
搜索更多相关主题的帖子: 三菱PLC public 通信 
2015-06-22 22:27
zhangyanpei
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2015-6-6
收藏
得分:0 
回复 楼主 zhangyanpei
红色标明的程序段
图片附件: 游客没有浏览图片的权限,请 登录注册
2015-06-22 22:32
wangnannan
Rank: 18Rank: 18Rank: 18Rank: 18Rank: 18
等 级:贵宾
威 望:87
帖 子:2546
专家分:9359
注 册:2007-11-3
收藏
得分:20 
可以用串口调试助手

出来混,谁不都要拼命的嘛。 。拼不赢?那就看谁倒霉了。 。有机会也要看谁下手快,快的就能赢,慢。 。狗屎你都抢不到。 。还说什么拼命?
2015-06-23 10:40
俞挫俞奋
Rank: 1
等 级:新手上路
帖 子:30
专家分:0
注 册:2017-5-14
收藏
得分:0 
ComFxPlc V27.rar (523.12 KB)

和C#开发的,里面有说明文档,亲测可用;

[此贴子已经被作者于2017-12-11 20:09编辑过]

2017-05-14 15:31
俞挫俞奋
Rank: 1
等 级:新手上路
帖 子:30
专家分:0
注 册:2017-5-14
收藏
得分:0 
这里有更多的源代码下载:
http://blog.
2017-06-25 21:53
快速回复:c#与三菱PLC进行串口通信
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.028163 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved