三菱PLC串口通讯MCRS232

本文介绍了如何进行三菱PLC的串口通讯,重点在于D100地址的数据读写操作。通过代码示例展示了读取和写入数据的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

下面为简单的PLC数据读写
在这里插入图片描述
地址:如D100;位数:读取或写入的个数;写入数据:写入地址的数据;读数据:显示读出的数据
废话不多说,先上代码:

  class cplcom
    {
              
            public System.IO.Ports.SerialPort serialPort1=new  System.IO.Ports.SerialPort();//實例化串口號
            public string enq = "05 ";
            public string head = "F90000FF00";
            public string order = "1401";//命令----批量写:1401;批量读:0401
            public string childorder = "0000";//子命令
            public string A_dress = "";//软元件代码
            public string start = "";  //起始软元件        
            public string count = "";//软元件点数          
            
            public void ss()//先設置端口
            {
   
            serialPort1.PortName="COM1";
            serialPort1.BaudRate =9600;// int.Parse(cobBaudrate.SelectedValue.ToString());
            //serialPort1.Parity = int.Parse(cobCheckDigit.SelectedValue.ToString());
            serialPort1.Parity = System.IO.Ports.Parity.None;
            serialPort1.DataBits = 8;// int.Parse(cobDataBit.SelectedValue.ToString());
            serialPort1.StopBits = System.IO.Ports.StopBits.One ;//
                         
            }
            public void connect()
            {
   
                if (serialPort1.IsOpen == false)
                {
   
                    serialPort1.Close();
                    serialPort1.ReceivedBytesThreshold = 1;
                    serialPort1.RtsEnable = true;
                    serialPort1.DtrEnable = true;
                    serialPort1.Open();
                }
                
            }
            public void noconnect()
            {
   
                serialPort1.ReceivedBytesThreshold = 1;
                serialPort1.RtsEnable = false;
                serialPort1.DtrEnable = false;
                serialPort1.Close();

            }
            public void writePlcData(string szDevice, int dwSize, short[] write)//写入數據(多个)
            {
   
                order = "1401";//批量写             
                szDevice = szDevice.ToUpper();//將寄存器改成大寫
                string adress = szDevice.Substring(0, 1);
                string AD1 = "";
                switch (adress)
                {
   
                    case "D": AD1 = "D"; break;
                    case "R": AD1 = "R"; break;
                    case "M": AD1 = "M"; break;
                    default: break;
                }
                A_dress = AD1 + "*";
                start = (szDevice.Substring(1, szDevice.Length - 1)).ToString().PadLeft(6, '0');
                count = Convert.ToString(dwSize, 16).PadLeft(4, '0');
                string concent="";//寫入的內容
                for (int i = 0; i < write.Length; i++)
                {
   
                     concent += write[i].ToString("X4");//写入的值                    

                }              
                //string sendMsg = "05 46 39 30 30 30 30 46 46 30 30 31 34 30 31 30 30 30 30 44 2A 30 30 30 " + ASC(szDevice) + ASC(Convert.ToString(dwSize, 16).PadLeft(4, '0')) + WriteNum;//.padleft(2,"0");將數據改成連接的字符串形式,後期需要在進行書寫
                string sendMsg = enq + ASC(head + order + childorder + A_dress + start + count + concent);//.padleft(2,"0");將數據改成連接的字符串形式,後期需要在進行書寫
                //項將字符串請求命令延時寄存器讀取位數連接起來
                string ckSum = CheckSum(sendMsg);
                sendMsg = sendMsg + " 0D 0A";//和校驗之後加上結束符
                //sendMsg = sendMsg + " 0D 0A";//和校驗之後加上結束符
                SendString(sendMsg);//发送数据
            }
            public string[] readPlcData(string szDevice, int dwSize)//讀取數據 (读取多个)
            {
                 
                order = "0401";
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

susan花雨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值