<big id="a5mua"></big>

      <tt id="a5mua"><dfn id="a5mua"></dfn></tt>
      <wbr id="a5mua"><sup id="a5mua"></sup></wbr>

        
        

      1. 亚洲综合小综合中文字幕,国产久爱免费精品视频,精品国产品香蕉在线,国产午夜精品在人线播放,精品一二三四区在线观看,国产成人无码免费看视频软件 ,色欲久久人妻内射,午夜在线观看成人av
        夢雨天涯
        微信hui530527   &
        級別: 網(wǎng)絡(luò)英雄
        精華主題: 0
        發(fā)帖數(shù)量: 4994 個(gè)
        工控威望: 8435 點(diǎn)
        下載積分: 18664 分
        在線時(shí)間: 3496(小時(shí))
        注冊時(shí)間: 2016-10-31
        最后登錄: 2025-11-01
        查看夢雨天涯的 主題 / 回貼
        樓主  發(fā)表于: 2025-04-23 11:24
        問題是我是一個(gè)PLC工程師,想用寫一個(gè)上位機(jī)程序和三菱FX3U通訊,PLC的IP地址為192.168.0.250,端口號(hào)為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站賬號(hào),夢雨天涯1991
        請不要隨意加我,不會(huì)隨便通過。
        工控技術(shù)交流QQ群942493953
        工控tucao
        級別: 略有小成
        精華主題: 0
        發(fā)帖數(shù)量: 194 個(gè)
        工控威望: 376 點(diǎn)
        下載積分: 880 分
        在線時(shí)間: 171(小時(shí))
        注冊時(shí)間: 2013-01-30
        最后登錄: 2025-05-12
        查看工控tucao的 主題 / 回貼
        1樓  發(fā)表于: 2025-04-23 17:03
        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)存儲(chǔ)
                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 = $"讀取錯(cuò)誤: {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,      // 站號(hào)、保留
                        (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,      // 站號(hào)、保留
                        (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());
                }
            }
        }

        主站蜘蛛池模板: 亚洲一区在线观看青青蜜臀| 久久91精品牛牛| 日韩中文字幕精品人妻| 日本又黄又爽gif动态图| 精品国产一区二区三区久| 亚洲无码a∨在线视频| 亚洲综合色区在线播放2019| 亚洲国产美女精品久久久| 久久亚洲中文字幕精品有坂深雪| 国产精品一区二区日韩精品 | 人人妻人人澡AV天堂香蕉| 国产区精品福利在线熟女| 国内精品久久久久影院不卡| brazzers欧美巨大| 久久精品国产亚洲不AV麻豆| 国产一区二区三区4区| 久久99热只有频精品8| 一本色道久久88综合日韩精品 | 99久久无码私人网站| 国产精品亚洲中文字幕| 99re在线免费视频| 国产剧情视频一区二区麻豆| 亚洲男人的天堂久久香蕉| 国产va免费精品观看| 欧美s码亚洲码精品m码| 亚洲AV综合A∨一区二区| 免费人成网站免费看视频| 国产色婷婷视频在线观看| 最新亚洲av日韩av二区| 国产高清毛片| 日韩高清不卡一区二区三区| 成人亚欧欧美激情在线观看| 国产亚洲精品日韩av在| 麻豆a级片| 精品亚洲国产成人| 亚洲av无码专区在线厂| 国产极品精品自在线不卡| 中文国产成人久久精品小说| 国产人成午夜免费看| 国产97视频人人做人人爱| 午夜免费视频国产在线|