如何实现C#通过TCP协议从工业控制器以太网口读取数据?

grape819 2016-07-01 11:38:49
我是新手,想通过C#编写一个小程序,实现通过以太网口从工业控制器以太网读取数据。

目前已知的信息是,我通过超级终端进行以下设置后(工业控制器的端口号设定与之相同),即可接收到来之工业控制器的数据。


麻烦高手帮忙看看代码应该怎么写?谢谢
...全文
1976 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
hanghangz 2019-06-18
  • 打赏
  • 举报
回复
誰把此坟挖出来的
nangongxiaobai 2019-06-18
  • 打赏
  • 举报
回复
首先,你查一下该平台的官网,或者找售后要一些demo,厂家早就写好了各种接口你直接调用就可以了。 其次,如果你实在想写,那你可以用visa库,这个是工业控制里面大家普遍应用的库。
平底锅锅锅 2019-04-04
  • 打赏
  • 举报
回复
Tcp通信。可以下载个此类软件,仿照写个。
Binnary 2019-04-03
  • 打赏
  • 举报
回复
引用 10 楼 qq_41788995 的回复:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;



namespace 客户端
{
public partial class Form1 : Form
{
Socket clientSocket;
Thread clientThread;
public Form1()
{
InitializeComponent();
//对跨线程的非法错误不检查
Control.CheckForIllegalCrossThreadCalls = false;

//客户端IP地址
this.IpAddress_textBox.Text = "127.0.0.1";

//客户端端口号
this.Port_textBox.Text = "6001";
Thread thea = new Thread(resive);
thea.Start();
}
private void ConnectToServer()
{
byte[] data = new byte[1024];

//网络地址和服务端口的组合称为端点,IPEndPoint 类表示这个端点
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse(this.IpAddress_textBox.Text), int.Parse(this.Port_textBox.Text));

//初始化Socket
clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

//将套接字与远程服务器地址相连
try
{
//连接到服务器
clientSocket.Connect(ipep);
}
catch (SocketException ex)
{
MessageBox.Show("connect error: " + ex.Message);
return;
}

}

private void Form1_Load(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
if (IpAddress_textBox.Text == "")
{
MessageBox.Show("请输入!");
return;
}

//开启一个子线程,连接到服务器
clientThread = new Thread(new ThreadStart(ConnectToServer));
clientThread.Start();
}

private void button2_Click(object sender, EventArgs e)
{
try
{
byte[] data = new byte[1024];

//对输入信息进行编码并放到一个字节数组了
data = Encoding.ASCII.GetBytes(txtClient.Text);

//向服务器发送信息
clientSocket.Send(data, data.Length, SocketFlags.None);

}
catch (Exception ex)
{

MessageBox.Show("error"+ex);

}
}
private void resive()
{
try
{
int bytes = 0;
byte[] message = new byte[1024];
bytes = clientSocket.Receive(message);
MessageBox.Show("lllll");
}
catch (Exception ex)
{

MessageBox.Show("error" + ex);

}

}
private void label3_Click(object sender, EventArgs e)
{
}
}
}


请问这个Form是怎么设计的
孙先生iulin 2019-04-03
  • 打赏
  • 举报
回复
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Net; using System.Net.Sockets; using System.Threading; namespace 客户端 { public partial class Form1 : Form { Socket clientSocket; Thread clientThread; public Form1() { InitializeComponent(); //对跨线程的非法错误不检查 Control.CheckForIllegalCrossThreadCalls = false; //客户端IP地址 this.IpAddress_textBox.Text = "127.0.0.1"; //客户端端口号 this.Port_textBox.Text = "6001"; Thread thea = new Thread(resive); thea.Start(); } private void ConnectToServer() { byte[] data = new byte[1024]; //网络地址和服务端口的组合称为端点,IPEndPoint 类表示这个端点 IPEndPoint ipep = new IPEndPoint(IPAddress.Parse(this.IpAddress_textBox.Text), int.Parse(this.Port_textBox.Text)); //初始化Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //将套接字与远程服务器地址相连 try { //连接到服务器 clientSocket.Connect(ipep); } catch (SocketException ex) { MessageBox.Show("connect error: " + ex.Message); return; } } private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { if (IpAddress_textBox.Text == "") { MessageBox.Show("请输入!"); return; } //开启一个子线程,连接到服务器 clientThread = new Thread(new ThreadStart(ConnectToServer)); clientThread.Start(); } private void button2_Click(object sender, EventArgs e) { try { byte[] data = new byte[1024]; //对输入信息进行编码并放到一个字节数组了 data = Encoding.ASCII.GetBytes(txtClient.Text); //向服务器发送信息 clientSocket.Send(data, data.Length, SocketFlags.None); } catch (Exception ex) { MessageBox.Show("error"+ex); } } private void resive() { try { int bytes = 0; byte[] message = new byte[1024]; bytes = clientSocket.Receive(message); MessageBox.Show("lllll"); } catch (Exception ex) { MessageBox.Show("error" + ex); } } private void label3_Click(object sender, EventArgs e) { } } }
grape819 2016-07-04
  • 打赏
  • 举报
回复
在这个应用中,电脑只接收来之工业控制器的数据。
grape819 2016-07-04
  • 打赏
  • 举报
回复
谢谢大家的回复,我还在慢慢消化中。 可能 我描述得不是很清楚,我再详细描述一下我的需求: ●硬件:电脑1台、普通网线1根、工业控制器1台(比如:伺服驱动器) ●背景:当工业控制器驱动伺服电机完成一个工作周期后(只知道伺服驱动器的IP地址和发送数据的端口号,不知道其内部数据发送机制),会产生相应的结果数据:扭矩、速度、角度等。已知通过超级终端(见初贴)可以读取到相应的结果数据,但不便于我二次处理。 ●需求:想自己写一个小程序,通过它去接收工业控制器的结果数据。拿到这个数据后,我再进行别的处理(比如让其显示在窗体应用程序中供操作工查看,这段代码我会写) 这其实是一个工业场合的应用,我不知道如何写出接收数据的代码,还望大家指点迷津,或是给个思路或大体框架,我自己去琢磨。 诚挚感谢~~
grape819 2016-07-04
  • 打赏
  • 举报
回复
以数台电脑作为载体的聊天软件,比如客户端和服务端的代码我会写。
但在这个应用中,由于工业控制器是个死物,只是单纯的发送数据而已,除开可以修改ip和端口外,在通讯层面就没有可操作的地方了。
不知道电脑和工业控制器(或者多台控制器)之间,谁是服务端,谁是客户端?
grape819 2016-07-02
  • 打赏
  • 举报
回复
谢谢版主,学习中...
  • 打赏
  • 举报
回复
虽然这是一个“幼儿园的”例子,但是说明了一些基本的底层概念: 服务器要监听 IPAddress.Any,服务器要等待客户端接入(AcceptTcpClient),服务器使用与客户端建立的连接(a)与之双向通讯(可写、也可读),对于支持文本协议的通讯可以使用最基础的 StreamWriter、StreamReader 来处理。 客户端则必服务器端简单一些、更少一个环节。客户端在实例化时直接与服务器建立联系(s),然后对于支持文本协议的通讯可以使用最基础的 StreamWriter、StreamReader 来处理。 其实这很简单。为什么有些代码很复杂?你自己想想。
  • 打赏
  • 举报
回复
这里,按照你的描述,发送信息方是服务器,而客户端则用来接收信息。这里只考虑单机对单机的通讯。
  • 打赏
  • 举报
回复
一个文本式的tcp通讯例子可以这样写
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            ThreadPool.QueueUserWorkItem(h => receive());
            ThreadPool.QueueUserWorkItem(h => send());
            Console.WriteLine("..............................请按任意键退出");
            Console.ReadLine();
        }

        private static void send()
        {
            var s = new TcpListener(IPAddress.Any, port);
            s.Start();
            var a = s.AcceptTcpClient();
            using (var wf = new StreamWriter(a.GetStream()))
            {
                wf.WriteLine("abc");
                wf.WriteLine("你好啊");
                wf.WriteLine("じ✿ゞ");
            }
        }

        private static string host = "127.0.0.1";
        private static int port = 1234;

        /// <summary>
        /// 首先作为客户端连接,然后接收数据
        /// </summary>
        private static void receive()
        {
            var s = new TcpClient(host, port);
            using (var r = new StreamReader(s.GetStream()))
            {
                while (!r.EndOfStream)
                {
                    var message = r.ReadLine();
                    Console.WriteLine(message);
                }
            }
        }
    }
}
我们谈的是比较专业一点的开发。但是对于初学者,其实学的应该是这个。这就好像是幼儿园学的数学跟一个建筑工程师学的数学肯定是不一样的,幼儿园学的数学也是实用的,就像这里的程序也可以用来改一改就去应付。只不过这里的程序是有其适应场合、针对的人群的!
threenewbee 2016-07-02
  • 打赏
  • 举报
回复
如果超级终端可以,那么说明它是一个标准的telnet服务器端,你可以参考 http://blog.csdn.net/foart/article/details/6833815

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧