FinsUDP
Samples
A PC can read and write PLC memory values by sending FINS commands to the PLC via Ethernet
Here, DM values are read using FINS/UDP
| PLC IP address | 192.168.250.1 |
| PC IP address | 192.168.250.48 |
| Contents of the send command | Read 10 words from DM100 |
| DM100 | 0001 |
|---|---|
| DM101 | 0002 |
| DM102 | 0003 |
| DM103 | 0004 |
| DM104 | 0005 |
| DM105 | 0006 |
| DM106 | 0007 |
| DM107 | 0008 |
| DM108 | 0009 |
| DM109 | 000A |
The sample code below shows synchronous and asynchronous communication in C#
Creating the form (placing buttons, etc.) is omitted, so place buttons and so on as needed
The sample contains no error handling; add error handling as appropriate
Synchronous communication
With synchronous communication, after sending a command it waits for the response
The receive timeout is 1000 ms, so if there is no response within 1 second it stops waiting
private void UdpFinsMessage()
{
System.Net.IPAddress LocalIP = System.Net.IPAddress.Parse("192.168.250.48"); // PC IP address
System.Net.IPEndPoint LocalEP = new System.Net.IPEndPoint(LocalIP, 9600);
System.Net.Sockets.UdpClient udp = new System.Net.Sockets.UdpClient(LocalEP);
udp.Client.ReceiveTimeout = 1000;
byte[] cmd = new byte[18];
// ----------------- FINS header
cmd[0] = 0x80; // ICF
cmd[1] = 0x00; // RSV
cmd[2] = 0x02; // GCT
cmd[3] = 0; // DNA destination network address
cmd[4] = 1; // DA1 destination node address (match the last octet of the PLC IP address)
cmd[5] = 0; // DA2 destination unit address
cmd[6] = 0; // SNA source network address
cmd[7] = 48; // SA1 source node address (match the last octet of the PC IP address)
cmd[8] = 0; // SA2 source unit address
cmd[9] = 1; // SID identifier, any value 00-FF
// ----------------- FINS command
cmd[10] = 0x01; // MRC read command 0101
cmd[11] = 0x01; // SRC
cmd[12] = 0x82; // MemoryType = DM
cmd[13] = 0x00; // ReadAddress = 100CH
cmd[14] = 0x64;
cmd[15] = 0x00;
cmd[16] = 0x00; // ReadSize = 10CH
cmd[17] = 0x0A;
udp.Send(cmd, cmd.Length, "192.168.250.1", 9600);
System.Net.IPEndPoint TargetIp = null;
byte[] rcv = udp.Receive(ref TargetIp);
textBox1.AppendText(BitConverter.ToString(rcv) + "\r\n");
// Expected response
// C0-00-02-00-30-00-00-01-00-01-01-01-00-00-00-01-00-02-00-03-00-04-00-05-00-06-00-07-00-08-00-09-00-0A
// ~~~~~~~|~~~~~~~~~~~~~~~~~~~~~ ~~|~~ ~~|~~ ~~~~~~|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// +FINS header | | +-- read values=000100020003000400050006000700080009000A
// | +-- normal completion
// +--read command
udp.Close();
}
Explanation
Lines 3-5
System.Net.IPAddress LocalIP = System.Net.IPAddress.Parse("192.168.250.48"); // PC IP address
System.Net.IPEndPoint LocalEP = new System.Net.IPEndPoint(LocalIP, 9600);
System.Net.Sockets.UdpClient udp = new System.Net.Sockets.UdpClient(LocalEP);
Create a UDP client specifying the PC's IP address and UDP port number
Line 6
udp.Client.ReceiveTimeout = 1000;
Set the receive timeout to 1 second (1000 ms)
If no timeout is set, executing UDP Receive waits forever
Lines 8-28
byte[] cmd = new byte[18];
// ----------------- FINS header
cmd[0] = 0x80; // ICF
~
cmd[17] = 0x0A;
Set the FINS command in a byte array
Here a command that reads 10 words from DM100 is set
For the contents of FINS commands, see FINS Commands
Line 30
udp.Send(cmd, cmd.Length, "192.168.250.1", 9600);
Send the FINS command in the cmd byte array, specifying the destination host name or IP address and port number
Lines 32-33
System.Net.IPEndPoint TargetIp = null;
byte[] rcv = udp.Receive(ref TargetIp);
Wait with the Receive method until data is received
The received data is a byte array containing the response to the FINS command
If nothing is received within the timeout set on line 6, an error occurs
Line 35
textBox1.AppendText(BitConverter.ToString(rcv) + "\r\n");
Show the received data in the text box
If the read succeeds, the string written as the expected response on line 37 is shown in the text box
Asynchronous communication
With asynchronous communication, processing ends once the command is sent (SEND)
An event is raised when received data arrives, and the data is captured then
System.Net.Sockets.UdpClient U;
private void button2_Click(object sender, EventArgs e)
{
UdpFinsMessageASync();
}
private void UdpFinsMessageASync()
{
if (U==null)
{
System.Net.IPEndPoint EP = new System.Net.IPEndPoint(System.Net.IPAddress.Any, 9600);
U = new System.Net.Sockets.UdpClient(EP);
U.BeginReceive(ReceiveCallbak, U);
}
byte[] cmd = new byte[18];
// ----------------- FINS header
cmd[0] = 0x80; // ICF
cmd[1] = 0x00; // RSV
cmd[2] = 0x02; // GCT
cmd[3] = 0; // DNA destination network address
cmd[4] = 1; // DA1 destination node address (match the last octet of the PLC IP address)
cmd[5] = 0; // DA2 destination unit address
cmd[6] = 0; // SNA source network address
cmd[7] = 48; // SA1 source node address (match the last octet of the PC IP address)
cmd[8] = 0; // SA2 source unit address
cmd[9] = 1; // SID identifier, any value 00-FF
// ----------------- FINS command
cmd[10] = 0x01; // MRC read command 0101
cmd[11] = 0x01; // SRC
cmd[12] = 0x82; // MemoryType = DM
cmd[13] = 0x00; // ReadAddress = 100CH
cmd[14] = 0x64;
cmd[15] = 0x00;
cmd[16] = 0x00; // ReadSize = 10CH
cmd[17] = 0x0A;
// Resume receiving
U.BeginSend(cmd, cmd.Length, "192.168.250.1", 9600, SendCallback, U);
}
private void SendCallback(IAsyncResult ar)
{
System.Net.Sockets.UdpClient udp = (System.Net.Sockets.UdpClient)ar.AsyncState;
try
{
udp.EndSend(ar);
}
catch (System.Net.Sockets.SocketException ex)
{
Console.WriteLine(ex.Message);
}
catch (ObjectDisposedException ex)
{
Console.WriteLine("Already Closed");
}
}
private delegate void AddStringDelegete(byte[] str);
private void AddText(byte[] msg)
{
if (msg == null)
return;
textBox1.AppendText(BitConverter.ToString(msg) + "\r\n");
}
private void ReceiveCallbak(IAsyncResult ar)
{
System.Net.Sockets.UdpClient udp = (System.Net.Sockets.UdpClient)ar.AsyncState;
System.Net.IPEndPoint remoteEP = null;
byte[] receiveBytes = udp.EndReceive(ar, ref remoteEP);
System.Net.IPAddress receiveAddress = remoteEP.Address;
AddStringDelegete dlg = new AddStringDelegete(AddText);
this.Invoke(dlg, new object[] { receiveBytes });
U.BeginReceive(ReceiveCallbak, U);
}
What is asynchronous communication?
With synchronous communication,
Receive is executed after Send to wait for the received data, and while waiting the application is locked (stuck waiting) and cannot update the screen or handle keyboard or mouse input
With asynchronous communication,
1. BeginReceive is executed in advance so that a specified method is called when data is received
2. Sending is done with BeginSend, after which processing ends for now
Because sending and receiving are handled separately, the application is not locked
When received data arrives, the method specified in BeginReceive is executed
NOTE
There are other ways, such as using a separate thread for synchronous communication, but this explains asynchronous communication on a single thread
Explanation
Line 1
System.Net.Sockets.UdpClient U;
Create an instance of UdpClient
Lines 3-6
private void button2_Click(object sender, EventArgs e)
{
UdpFinsMessageASync();
}
Clicking Button2 on the form executes the UdpFinsMessageASync method
Lines 10-15
if (U==null)
{
System.Net.IPEndPoint EP = new System.Net.IPEndPoint(System.Net.IPAddress.Any, 9600);
U = new System.Net.Sockets.UdpClient(EP);
U.BeginReceive(ReceiveCallbak, U);
}
If the UdpClient instance is null, set the EndPoint and create it with new
Then receive asynchronously with BeginReceive on line 14
When data is received, the ReceiveCallbak method is executed
Lines 17-37
byte[] cmd = new byte[18];
// ----------------- FINS header
cmd[0] = 0x80; // ICF
~
cmd[17] = 0x0A;
Set the FINS command in a byte array
Here a command that reads 10 words from DM100 is set
For the contents of FINS commands, see FINS Commands
Line 40
U.BeginSend(cmd, cmd.Length, "192.168.250.1", 9600, SendCallback, U);
Send data asynchronously
After sending, the SendCallback method is executed
Lines 43-59
private void SendCallback(IAsyncResult ar)
{
System.Net.Sockets.UdpClient udp = (System.Net.Sockets.UdpClient)ar.AsyncState;
try
{
udp.EndSend(ar);
}
catch (System.Net.Sockets.SocketException ex)
{
Console.WriteLine(ex.Message);
}
catch (ObjectDisposedException ex)
{
Console.WriteLine("Already Closed");
}
}
Finish processing after sending the data
If there is an error, it is written to the console on line 53
If it is already closed, a comment is written to the console on line 57 and it ends
Lines 61-68
private delegate void AddStringDelegete(byte[] str);
private void AddText(byte[] msg)
{
if (msg == null)
return;
textBox1.AppendText(BitConverter.ToString(msg) + "\r\n");
}
Prepare a delegate for showing messages in the text box
The string passed to AddStringDelegete is appended to the text box
This is because ReceiveCallbak, which follows, runs on a separate thread, so the data received in ReceiveCallbak is passed to the form's text box through the delegate
Lines 70-81
private void ReceiveCallbak(IAsyncResult ar)
{
System.Net.Sockets.UdpClient udp = (System.Net.Sockets.UdpClient)ar.AsyncState;
System.Net.IPEndPoint remoteEP = null;
byte[] receiveBytes = udp.EndReceive(ar, ref remoteEP);
System.Net.IPAddress receiveAddress = remoteEP.Address;
AddStringDelegete dlg = new AddStringDelegete(AddText);
this.Invoke(dlg, new object[] { receiveBytes });
U.BeginReceive(ReceiveCallbak, U);
}
When data is received, ReceiveCallbak is executed as specified by BeginReceive on line 14
EndReceive on line 74 stores the received data in the byte array receiveBytes
Line 77 creates AddStringDelegete and line 78 passes the received data to it with Invoke
Before exiting, BeginReceive is executed again to continue receiving asynchronously
Related pages
- FINS Message Communication
- FINS Commands
- FINS Addresses
- FINS Command Examples
- Comparison of FINS/UDP and FINS/TCP
- FinsTCP
Download
The source introduced here can be downloaded from GitHub
