【 C# 】 简易的Socket TCP Client客户端 -- 与PLC通讯

 

public class SocketClient
    {
        //声明IP,端口,和一个用来连接的Socket
        private string _ip;
        private int _port;
        private System.Net.Sockets.TcpClient _tcpClient;
        
        //创建一个委托,用来满足其他类调用
        public delegate void DelegateMessage(byte[] bytes) ;
        public event DelegateMessage OnmessageEvent;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="remoteIp">IP地址</param>
        /// <param name="remotePort">端口号</param>
        public SocketClient(string remoteIp,int remotePort)
        {
            this._ip = remoteIp;
            this._port = remotePort;
        }

        //TCP连接
        public bool Connect()
        {
            _tcpClient = new TcpClient();
            try
            {
                _tcpClient.Connect(IPAddress.Parse(_ip), _port);
                Task.Run(new Action(ReceiveMessage));//开启线程,不停接收消息
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
            return true;//返回连接状态
        }

        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="bytes">需要发送的字节</param>
        public void SendMessage(byte[] bytes)
        {
            NetworkStream networkStream = _tcpClient.GetStream();
            networkStream.Write(bytes, 0, bytes.Length);
        }


        //接收消息
        public void ReceiveMessage()
        {
            NetworkStream networkStream = _tcpClient.GetStream();
            while (true)
            {
                byte[] buffer = new byte[8];
                int size = networkStream.Read(buffer, 0, buffer.Length);
                OnmessageEvent?.Invoke(buffer);
            }            
        }

    }

 

  • 3
    点赞
  • 49
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
C#与三菱PLC之间可以通过TCP/IP技术进行通信。你可以使用C#中的Socket类来实现与PLC的通信。下面是一个简单的示例代码,展示了如何使用C#与三菱PLC进行TCP/IP通信: ```csharp using System; using System.Net; using System.Net.Sockets; class Program { static void Main(string[] args) { // PLC的IP地址和端口号 string plcIpAddress = "192.168.0.1"; int plcPort = 5000; // 创建一个TCP客户端Socket TcpClient client = new TcpClient(); try { // 连接到PLC client.Connect(plcIpAddress, plcPort); // 获取网络流 NetworkStream stream = client.GetStream(); // 发送命令到PLC string command = "YOUR_COMMAND_HERE"; byte[] data = System.Text.Encoding.ASCII.GetBytes(command); stream.Write(data, 0, data.Length); // 接收从PLC返回的数据 byte[] buffer = new byte[1024]; int bytesRead = stream.Read(buffer, 0, buffer.Length); string response = System.Text.Encoding.ASCII.GetString(buffer, 0, bytesRead); // 处理返回的数据 Console.WriteLine("PLC返回的数据: " + response); } catch (Exception ex) { Console.WriteLine("与PLC通信时发生错误: " + ex.Message); } finally { // 关闭连接 client.Close(); } Console.ReadLine(); } } ``` 在上面的示例代码中,你需要将`plcIpAddress`和`plcPort`替换为你实际使用的PLC的IP地址和端口号。然后,你可以使用`client.Connect`方法连接到PLC,使用`stream.Write`方法发送命令,使用`stream.Read`方法接收从PLC返回的数据。 请注意,这只是一个简单的示例代码,实际情况可能更复杂。你需要根据你的具体需求和PLC的通信协议进行相应的调整和处理。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值