Home > Net >  How to implement a c # reading data from the TCP protocol Ethernet front-end ports from the industri
How to implement a c # reading data from the TCP protocol Ethernet front-end ports from the industri

Time:10-21

I am a novice, want to pass the c # write a small program to achieve reading data from the Ethernet front-end ports from industrial Ethernet controller,

Information is known, I through the super terminal after the following Settings (industrial controller set the port number of the same), can receive the arrival of the data of the industrial controller,


Trouble help look at the code should be how to write? thank you

CodePudding user response:

If the super terminal can, so that it is a standard Telnet server side, you can refer to
http://blog.csdn.net/foart/article/details/6833815

CodePudding user response:

Thank the moderator, the study...

CodePudding user response:

A textual TCP communications example you can write the
 using System; 
Using System. IO;
Using System.Net;
Using System.Net.Sockets;
Using System. The Threading;

The namespace ConsoleApplication1
{
Class Program
{
The static void Main (string [] args)
{
ThreadPool. QueueUserWorkItem (h=& gt; The receive ());
ThreadPool. QueueUserWorkItem (h=& gt; The send ());
Console. WriteLine ("... Please press any key to exit the ");
The Console. ReadLine ();
}

Private static void the send ()
{
Var s=new TcpListener (IPAddress. Any port).
S.S tart ();
Var a=s.A cceptTcpClient ();
Using (var wf=new StreamWriter (al-qeada etStream ()))
{
Wf. WriteLine (" ABC ");
Wf. WriteLine (" hello ");
Wf. WriteLine (" じ? ゞ ");
}
}

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

///& lt; Summary>
///as a client connection first, and then receive data
///& lt;/summary>
Private static void the receive ()
{
Var s=new TcpClient (host, port);
Using var (r=new StreamReader (s.G etStream ()))
{
while (! R.E ndOfStream)
{
Var message=r.R eadLine ();
Console. WriteLine (message);
}
}
}
}
}


We're talking about a bit more professional development, but for a beginner, learning should be the actually, it's like the kindergarten learn mathematics with a structural engineer to learn math is certainly different, kindergarten to learn math is useful, as the program can also be used to changed to deal with, the program here is just has to adapt to the situation, in view of the crowd!

CodePudding user response:

Here, according to your description, send information is the server, the client is used to receive information, here only consider single machine for single communication,

CodePudding user response:

Although this is an example "kindergarten", but illustrates some basic underlying concepts:

Server to monitor IPAddress. Any server to wait for client access (AcceptTcpClient), the client and server to use established connection (a) and the two-way communication (can write, also can read), to support text protocol communication can use the most basic StreamWriter, StreamReader to deal with,

Client is will server simpler, less a link, the client at instantiation to establish contact directly with the server (s), and then to support text protocol communication can use the most basic StreamWriter, StreamReader to deal with,

It's really simple, why some code is very complicated? You think about it,

CodePudding user response:

Thank you for your reply, I also in slowly digesting,
May be I can't describe very clearly, I again with a detailed description of my needs:

Low hardware: 1, general cable 1 root, industrial controller 1 (such as servo drive)
Low background: when industrial controller drives the servo motor to complete a working cycle (only know servo driver's IP address and port to send data, don't know its internal data sending mechanism), will produce the results of the corresponding data, torque, speed, Angle, etc., known by the super terminal (see early post) can read data to the corresponding results, but the secondary processing is not convenient to me,
Low demand: want to write a small program, through which to receive the results of the industrial controller data, after obtaining the data, I to other processing (such as make it displayed in the form in the application for the operator to view, I'll write this code)

It is an industrial application, I don't know how to write code, receive data still hope everybody a labyrinth, or give a thought or a general framework, myself wondering,

Sincere thank you ~ ~

CodePudding user response:

In this application, computer of the industrial controller only receive data,

CodePudding user response:

With several computer as a carrier of chat software, such as the client and server code I will write,
But in the application, with the industrial controller is an inanimate object, just pure to send data, but you can change the IP and port, where the communication level, there is no operational,
Don't know the computer and industrial controller (or more than one controller), who is the service side, who is the client?

CodePudding user response:

Using System;
Using System. Collections. Generic;
Using System.Com ponentModel;
Using System. The Data;
Using System. Drawing;
using System.Linq;
Using System. The Text;
using System.Threading.Tasks;
Using System. Windows. Forms;
Using System.Net;
Using System.Net.Sockets;
Using System. The Threading;



The namespace client
{
Public partial class Form1: Form
{
The Socket clientSocket;
Thread clientThread;
Public _click ()
{
InitializeComponent ();
//for illegal error across threads do not check
Control. CheckForIllegalCrossThreadCalls=false;

//the client IP address
This. IpAddress_textBox. Text="127.0.0.1";

//the client port
This. Port_textBox. Text="6001";
Thread Thea=new Thread (resive);
Thea. Start ();
}
Private void ConnectToServer ()
{
Byte [] data=https://bbs.csdn.net/topics/new byte [1024].

//the combination of network address and service port called endpoint, IPEndPoint class represents the endpoint
IPEndPoint ipep=new IPEndPoint (IPAddress. Parse (this) IpAddress_textBox) Text), int. J Parse (this) Port_textBox) Text));

//initialize the Socket
ClientSocket=new Socket (AddressFamily. InterNetwork, SocketType Stream, ProtocolType. Tcp);

//the socket is connected to a remote server address
Try
{
//to connect to the server
ClientSocket. Connect (ipep);
}
The catch (SocketException ex)
{
MessageBox. Show (" the 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 (" please enter!" );
return;
}

//open a child thread, connect to the server
ClientThread=new Thread (new ThreadStart (ConnectToServer));
ClientThread. Start ();
}

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  •  Tags:  
  • C #
  • Related