国产福利一区二区麻豆,无码少妇精品一区二区免费,成人A免费播放网站,免费国产精品自产拍

夢雨天涯
微信hui530527   &
級別: 網(wǎng)絡(luò)英雄
精華主題: 0
發(fā)帖數(shù)量: 4651 個(gè)
工控威望: 8050 點(diǎn)
下載積分: 17206 分
在線時(shí)間: 3229(小時(shí))
注冊時(shí)間: 2016-10-31
最后登錄: 2025-04-24
查看夢雨天涯的 主題 / 回貼
樓主  發(fā)表于: 前天
問題是我是一個(gè)PLC工程師,想用寫一個(gè)上位機(jī)程序和三菱FX3U通訊,PLC的IP地址為192.168.0.250,端口號為5551.要求顯示X0到X7,輸出顯示Y0到Y(jié)7,還有M0到M9切換按鈕,輸出格式為直接打開的exe文件
以下是使用C#開發(fā)的三菱FX3U上位機(jī)通信程序?qū)崿F(xiàn)方案,包含可直接運(yùn)行的EXE文件生成方法:
一、開發(fā)環(huán)境配置
‌開發(fā)工具‌:
Visual Studio 2022 (社區(qū)版)
.NET Framework 4.8
通信參數(shù)‌
NuGet包:Mx.Component.Melsec(三菱協(xié)議庫)
var config = new MelsecConfiguration {
    ProtocolType = ProtocolType.TCP,
    Host = "192.168.0.250",
    Port = 5551,
    CpuType = CpuType.FX3U,
    ConnectTimeout = 3000
};
二、界面設(shè)計(jì)(Windows Forms)
<!-- MainForm.Designer.cs -->
this.Controls.AddRange(new Control[] {
    // X0-X7狀態(tài)指示燈
    new StatusLight { Name="x0", Location=new Point(20,20) },
    // ...重復(fù)創(chuàng)建x1-x7
    
    // Y0-Y7狀態(tài)顯示
    new StatusLight { Name="y0", Location=new Point(120,20) },
    // ...重復(fù)創(chuàng)建y1-y7
    
    // M0-M9切換按鈕
    new ToggleButton { Name="m0", Text="M0", Location=new Point(220,20) },
    // ...創(chuàng)建m1-m9
    
    // 狀態(tài)欄
    new StatusStrip {
        Items = {
            new ToolStripStatusLabel { Name="lblStatus" }
        }
    }
});
三、核心通信代碼
public partial class MainForm : Form {
    private MelsecFxPLC plc;
    
    // 初始化PLC連接
    private void ConnectPLC() {
        plc = new MelsecFxPLC(config);
        var result = plc.Connect();
        
        if (result.IsSuccess) {
            timer1.Interval = 500;  // 500ms輪詢周期
            timer1.Start();
        }
    }

    // 定時(shí)讀取狀態(tài)
    private void timer1_Tick(object sender, EventArgs e) {
        // 批量讀取X/Y狀態(tài)
        var xStatus = plc.ReadBytes("X0", 1);
        var yStatus = plc.ReadBytes("Y0", 1);
        
        // 更新指示燈
        UpdateLights(xStatus, "x");
        UpdateLights(yStatus, "y");
    }

    // M按鈕點(diǎn)擊事件
    private void mButton_Click(object sender, EventArgs e) {
        var btn = sender as ToggleButton;
        plc.WriteBit($"M{btn.Tag}", btn.Checked);
    }
}
四、可視化組件類
// 狀態(tài)指示燈控件
public class StatusLight : PictureBox {
    public StatusLight() {
        Size = new Size(30, 30);
        BackColor = Color.Silver;
    }

    public bool Active {
        set => BackColor = value ? Color.Lime : Color.Red;
    }
}

// 切換按鈕控件
public class ToggleButton : CheckBox {
    public ToggleButton() {
        Appearance = Appearance.Button;
        TextAlign = ContentAlignment.MiddleCenter;
        Size = new Size(50, 30);
    }
}
五、項(xiàng)目打包部署
‌發(fā)布設(shè)置‌:

項(xiàng)目屬性 → 發(fā)布 → 選擇"從CD/USB安裝"
包含.NET Framework 4.8運(yùn)行庫
生成單文件EXE
‌依賴文件‌
├── MelsecFx.dll
├── Newtonsoft.Json.dll
└── Mx.Component.Core.dll
六、協(xié)議處理流程圖
sequenceDiagram
    participant UI as 上位機(jī)
    participant PLC as FX3U PLC
    
    UI->>PLC: TCP Connect(192.168.0.250:5551)
    PLC-->>UI: 連接成功響應(yīng)
    
    loop 500ms輪詢
        UI->>PLC: 讀取X0-X7(0x00B0命令)
        PLC-->>UI: X狀態(tài)數(shù)據(jù)
        UI->>PLC: 讀取Y0-Y7(0x00B0命令)
        PLC-->>UI: Y狀態(tài)數(shù)據(jù)
    end
    
    UI->>PLC: 寫入M位(0x1401命令)
    PLC-->>UI: 寫入確認(rèn)
七、測試驗(yàn)證步驟
‌PLC準(zhǔn)備‌:
; 測試程序
LD M8000
MOV K85 K1Y0  // Y0-Y7循環(huán)顯示
‌上位機(jī)操作檢查表‌:
微信hui530527      b站賬號,非標(biāo)自動化譚工
請不要隨意加我,不會隨便通過。QQ群942493953
工控tucao
級別: 略有小成
精華主題: 0
發(fā)帖數(shù)量: 192 個(gè)
工控威望: 374 點(diǎn)
下載積分: 875 分
在線時(shí)間: 170(小時(shí))
注冊時(shí)間: 2013-01-30
最后登錄: 2025-04-24
查看工控tucao的 主題 / 回貼
1樓  發(fā)表于: 前天
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Net.Sockets;
using System.Text;
using System.Windows.Forms;

namespace MELSEC_FX3U_Comm
{
    public partial class MainForm : Form
    {
        private TcpClient plcClient;
        private NetworkStream stream;
        private Timer refreshTimer;
        private byte station = 0x00;

        // PLC寄存器狀態(tài)存儲
        private bool[] xStatus = new bool[8];
        private bool[] yStatus = new bool[8];
        private bool[] mStatus = new bool[10];

        public MainForm()
        {
            InitializeComponent();
            InitializePLCConnection();
            SetupUI();
            StartRefreshTimer();
        }

        private void InitializePLCConnection()
        {
            try
            {
                plcClient = new TcpClient();
                plcClient.Connect(IPAddress.Parse("192.168.0.250"), 5551);
                stream = plcClient.GetStream();
                StatusLabel.Text = "已連接到PLC";
            }
            catch (Exception ex)
            {
                StatusLabel.Text = $"連接失敗: {ex.Message}";
            }
        }

        private void SetupUI()
        {
            // 初始化輸入顯示區(qū)域
            for (int i = 0; i < 8; i++)
            {
                var cb = new CheckBox
                {
                    Text = $"X{i}",
                    Location = new Point(20 + (i % 4) * 80, 20 + (i / 4) * 30),
                    AutoSize = true
                };
                xCheckBoxes.Add(cb);
                this.Controls.Add(cb);
            }

            // 初始化輸出顯示區(qū)域
            for (int i = 0; i < 8; i++)
            {
                var cb = new CheckBox
                {
                    Text = $"Y{i}",
                    Location = new Point(200 + (i % 4) * 80, 20 + (i / 4) * 30),
                    AutoSize = true
                };
                yCheckBoxes.Add(cb);
                this.Controls.Add(cb);
            }

            // 初始化M寄存器按鈕
            for (int i = 0; i < 10; i++)
            {
                var btn = new Button
                {
                    Text = $"M{i}",
                    Location = new Point(380 + (i % 5) * 80, 20 + (i / 5) * 30),
                    Width = 70
                };
                btn.Click += (s, e) => ToggleMRegister(i);
                mButtons.Add(btn);
                this.Controls.Add(btn);
            }
        }

        private void StartRefreshTimer()
        {
            refreshTimer = new Timer { Interval = 500 };
            refreshTimer.Tick += async (s, e) => await ReadPLCData();
            refreshTimer.Start();
        }

        private async Task ReadPLCData()
        {
            try
            {
                // 讀取X寄存器
                byte[] xData = ReadRegisters(0x0000, 8);
                for (int i = 0; i < 8; i++)
                    xStatus = (xData[i / 8] & (1 << (7 - (i % 8)))) != 0;

                // 讀取Y寄存器
                byte[] yData = ReadRegisters(0x0010, 8);
                for (int i = 0; i < 8; i++)
                    yStatus = (yData[i / 8] & (1 << (7 - (i % 8)))) != 0;

                UpdateUI();
            }
            catch (Exception ex)
            {
                StatusLabel.Text = $"讀取錯誤: {ex.Message}";
            }
        }

        private byte[] ReadRegisters(ushort start, ushort length)
        {
            // 構(gòu)建讀取請求報(bào)文
            List<byte> query = new List<byte>
            {
                0x80, 0x00, 0x00, 0x00,           // 起始符
                0x00, 0x02,                         // 控制代碼
                (byte)(station), 0x00, 0x00,      // 站號、保留
                (byte)(start >> 8), (byte)start,  // 起始地址
                (byte)(length >> 8), (byte)length,// 寄存器數(shù)量
                0x00, 0x00                          // 結(jié)束符
            };

            SendCommand(query.ToArray());
            return ReadResponse();
        }

        private void ToggleMRegister(int index)
        {
            mStatus[index] = !mStatus[index];
            WriteRegister(0x2000 + index, mStatus[index] ? 1 : 0);
            mButtons[index].BackColor = mStatus[index] ? Color.LightGreen : SystemColors.Control;
        }

        private void WriteRegister(ushort address, ushort value)
        {
            // 構(gòu)建寫請求報(bào)文
            List<byte> query = new List<byte>
            {
                0x80, 0x00, 0x00, 0x00,           // 起始符
                0x00, 0x12,                         // 控制代碼
                (byte)(station), 0x00, 0x00,      // 站號、保留
                (byte)(address >> 8), (byte)address,// 地址
                (byte)(value >> 8), (byte)value,  // 值
                0x00, 0x00                          // 結(jié)束符
            };

            SendCommand(query.ToArray());
        }

        private void SendCommand(byte[] command)
        {
            stream.Write(command, 0, command.Length);
        }

        private byte[] ReadResponse()
        {
            byte[] buffer = new byte[1024];
            int bytesRead = stream.Read(buffer, 0, buffer.Length);
            Array.Resize(ref buffer, bytesRead);
            return buffer;
        }

        private void UpdateUI()
        {
            // 更新X寄存器顯示
            for (int i = 0; i < 8; i++)
                xCheckBoxes.Checked = xStatus;

            // 更新Y寄存器顯示
            for (int i = 0; i < 8; i++)
                yCheckBoxes.Checked = yStatus;

            // 更新M寄存器顯示
            for (int i = 0; i < 10; i++)
                mButtons.BackColor = mStatus ? Color.LightGreen : SystemColors.Control;
        }

        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            base.OnFormClosing(e);
            plcClient?.Close();
        }

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());
        }
    }
}