How to use Ethernet to RS232 Converter on PLC application?
This article about Ethernet to RS232 converter using USR-TCP232-T.
This PLC application using modbus communication: Serial Modbus RTU
The advantages of using Ethernet communication: Multiple PLC network with Serial Modbus RTU and long distance communication
Video of the testing USR-TCP232 and PLC:
Hardware:
1. USR-TCP232-T module
2. Serial TTL to RS232 Male Connector
3. RS232 PLC Cable
4. PLC with Modbus Support
5. Power Supply DC 3.3V, or use Converter to DC3.3V
6. USB to TTL Serial for USR-TCP232-T Setup
Hardware Connections:
Download Project File:
1. Project File for Microsoft Visual C#, click here
2. PLC Ladder Programming for Siemens PLC, click here
3. USR-TCP232-T Manual, click here
4. Setup Software for USR-TCP232, click here
Video about USR TCP232 Setup:
Microsoft Visual C#:
This article about Ethernet to RS232 converter using USR-TCP232-T.
This PLC application using modbus communication: Serial Modbus RTU
The advantages of using Ethernet communication: Multiple PLC network with Serial Modbus RTU and long distance communication
Video of the testing USR-TCP232 and PLC:
Hardware:
1. USR-TCP232-T module
2. Serial TTL to RS232 Male Connector
3. RS232 PLC Cable
4. PLC with Modbus Support
5. Power Supply DC 3.3V, or use Converter to DC3.3V
6. USB to TTL Serial for USR-TCP232-T Setup
Hardware Connections:
Download Project File:
1. Project File for Microsoft Visual C#, click here
2. PLC Ladder Programming for Siemens PLC, click here
3. USR-TCP232-T Manual, click here
4. Setup Software for USR-TCP232, click here
Video about USR TCP232 Setup:
Microsoft Visual C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Net.Sockets;
namespace USR_TCP232
{
public partial class Form1 : Form
{
byte L0 = 0;
byte L1 = 0;
byte L2 = 0;
byte L3 = 0;
byte L4 = 0;
byte L5 = 0;
byte L6 = 0;
byte L7 = 0;
public Form1()
{
InitializeComponent();
}
byte[] Func03(byte
Slave_Address, int Starting_Address, int Number_of_Points)
{
//Function Code 03 / Read Holding Registers
//Function 3 request is always 8 bytes
byte[] message = new byte[8];
byte Function = 3;
//index0 = Slave Address
message[0] = Slave_Address;
//index1 = Function
message[1] = Function;
//index2 = Starting Address Hi
message[2] = (byte)(Starting_Address
>> 8);
//index3 = Starting Address Lo
message[3] = (byte)Starting_Address;
//index4 = Number of Points Hi
message[4] = (byte)(Number_of_Points
>> 8);
//index5 = Number of Points Lo
message[5] = (byte)Number_of_Points;
// CRC (Cyclical Redundancy Check) Calculation
ushort CRC = 0xFFFF;
byte CRCHi = 0xFF;
byte CRCLo = 0xFF;
ushort CRCLSB;
for (int i = 0; i
< (message.Length) - 2; i++)
{
CRC = (ushort)(CRC ^ message[i]);
for (int
j = 0; j < 8; j++)
{
CRCLSB = (ushort)(CRC & 0x0001);
CRC = (ushort)((CRC >> 1) &
0x7FFF);
if (CRCLSB == 1)
CRC = (ushort)(CRC ^ 0xA001);
}
}
CRCHi = (byte)((CRC >> 8) &
0xFF);
CRCLo = (byte)(CRC & 0xFF);
//index6 = CRC Lo
message[message.Length - 2] = CRCLo;
//index7 = CRC Hi
message[message.Length - 1] = CRCHi;
return message;
}
byte[] Func16(byte
Slave_Address, int Starting_Address, short[] values)
{
//FUCTION 16
byte Function = 16;
int NumberofRegisters = values.Length;
byte Byte_Count;
Byte_Count = (byte)(NumberofRegisters *
2);
byte[] message = new byte[9 + 2 * NumberofRegisters];
//index0 = Slave Address
message[0] = Slave_Address;
//index1 = Function
message[1] = Function;
//index2 = Starting Address Hi
message[2] = (byte)(Starting_Address
>> 8);
//index3 = Starting Address Lo
message[3] = (byte)Starting_Address;
//index4 = Number of Registers Hi
message[4] = (byte)(NumberofRegisters
>> 8);
//index5 = Number of Registers Lo
message[5] = (byte)NumberofRegisters;
//index6 = Byte Count
message[6] = Byte_Count;
for (int i = 0; i
< NumberofRegisters; i++)
{
//Data Hi, index7 and index9
message[7 + 2 * i] = (byte)(values[i]
>> 8);
//Data Lo, index8 and index10
message[8 + 2 * i] = (byte)(values[i]);
}
// CRC (Cyclical Redundancy Check) Calculation
ushort CRC = 0xFFFF;
byte CRCHi = 0xFF;
byte CRCLo = 0xFF;
ushort CRCLSB;
for (int i = 0; i
< (message.Length) - 2; i++)
{
CRC = (ushort)(CRC ^ message[i]);
for (int
j = 0; j < 8; j++)
{
CRCLSB = (ushort)(CRC & 0x0001);
CRC = (ushort)((CRC
>> 1) & 0x7FFF);
if (CRCLSB == 1)
CRC = (ushort)(CRC ^ 0xA001);
}
}
CRCHi = (byte)((CRC >> 8) &
0xFF);
CRCLo = (byte)(CRC & 0xFF);
//index11= CRC Lo
message[message.Length - 2] = CRCLo;
//index12 = CRC Hi
message[message.Length - 1] = CRCHi;
return message;
}
private bool
CRCResponseCheck(byte[] message)
{
//CRC Response Check:
byte[] CRC = new byte[2];
ushort CRCFull = 0xFFFF;
byte CRCHigh = 0xFF, CRCLow = 0xFF;
ushort CRCLSB;
for (int i = 0; i
< (message.Length) - 2; i++)
{
CRCFull = (ushort)(CRCFull ^
message[i]);
for (int
j = 0; j < 8; j++)
{
CRCLSB = (ushort)(CRCFull & 0x0001);
CRCFull = (ushort)((CRCFull >> 1)
& 0x7FFF);
if (CRCLSB == 1)
CRCFull = (ushort)(CRCFull ^ 0xA001);
}
}
CRCHigh = (byte)((CRCFull >> 8)
& 0xFF);
CRCLow = (byte)(CRCFull & 0xFF);
if (CRCLow == message[message.Length - 2] &&
CRCHigh == message[message.Length - 1])
return true;
else
return false;
}
private static bool IPCheck(string
IPServer, int PortServer, int TimeOutConnectMS)
{
Socket socket = null;
try
{
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.DontLinger, false);
IAsyncResult result =
socket.BeginConnect(IPServer, PortServer, null,
null);
bool success =
result.AsyncWaitHandle.WaitOne(TimeOutConnectMS, true);
return socket.Connected;
}
catch
{
return false;
}
finally
{
if (null
!= socket)
socket.Close();
}
}
private void
timer1_Tick(object sender, EventArgs e)
{
if (!IPCheck(ip_server.Text, Convert.ToInt32(port_server.Text),
500))
{
Status.Text = "IP=" +
ip_server.Text + " and Port=" +
port_server.Text + " Not Ready";
return;
}
try
{
TcpClient IPClient = new TcpClient(ip_server.Text,
Convert.ToInt32(port_server.Text));
IPClient.SendTimeout = 100;
IPClient.ReceiveTimeout = 100;
NetworkStream NS =
IPClient.GetStream();
NS.ReadTimeout = 100;
NS.WriteTimeout = 100;
byte[] readBuffer;
short[] values;
int numberOfBytesRead;
values = new short[1];
values[0] = (short)((L0 * 1) + (L1 *
2));
values[0] += (short)((L2 * 4) + (L3 *
8));
values[0] += (short)((L4 * 16) + (L5 *
32));
values[0] += (short)((L6 * 64) + (L7 * 128));
byte[] ModbusSend = Func16(1, 0,
values);
try
{
NS.Write(ModbusSend, 0,
ModbusSend.Length);
readBuffer = new byte[IPClient.ReceiveBufferSize];
numberOfBytesRead =
NS.Read(readBuffer, 0, readBuffer.Length);
if (readBuffer[1] == 16)
{
if (CRCResponseCheck(readBuffer)) Status.Text = "Modbus Run";
}
}
catch
{
Status.Text = "Modbus Error";
NS.Close();
IPClient.Close();
return;
}
byte[] ModbusRead = Func03(1, 0, 1);
NS.Write(ModbusRead, 0, ModbusRead.Length);
readBuffer = new byte[IPClient.ReceiveBufferSize];
numberOfBytesRead = NS.Read(readBuffer, 0, readBuffer.Length);
if (readBuffer[1] == 3)
{
if (CRCResponseCheck(readBuffer))
{
byte registers = (byte)(readBuffer[2]
/ 2);
values = new short[registers];
for (int i = 0; i
< registers; i++)
{
values[i] =
readBuffer[2 * i + 3];
values[i]
<<= 8;
values[i] +=
readBuffer[2 * i + 4];
if ((values[i] & 1) == 1) { button0.Image = Properties.Resources.on; button0.Tag = 1; } else { button0.Image = Properties.Resources.off; button0.Tag = 0; }
if ((values[i] & 2) == 2) { button1.Image =
Properties.Resources.on; button1.Tag = 1; } else { button1.Image = Properties.Resources.off; button1.Tag = 0; }
if ((values[i] & 4) == 4) { button2.Image =
Properties.Resources.on; button2.Tag = 1; } else { button2.Image = Properties.Resources.off; button2.Tag = 0; }
if
((values[i] & 8) == 8) { button3.Image = Properties.Resources.on; button3.Tag = 1; } else
{ button3.Image = Properties.Resources.off;
button3.Tag = 0; }
if ((values[i] & 16) == 16) { button4.Image =
Properties.Resources.on; button4.Tag = 1; } else { button4.Image = Properties.Resources.off; button4.Tag = 0; }
if ((values[i] & 32) == 32) { button5.Image =
Properties.Resources.on; button5.Tag = 1; } else { button5.Image = Properties.Resources.off; button5.Tag = 0; }
if ((values[i] & 64) == 64) { button6.Image =
Properties.Resources.on; button6.Tag = 1; } else { button6.Image = Properties.Resources.off; button6.Tag = 0; }
if ((values[i] & 128) == 128) { button7.Image =
Properties.Resources.on; button7.Tag = 1; } else { button7.Image = Properties.Resources.off; button7.Tag = 0; }
}
}
else
{
Status.Text = "Modbus CRC Error";
}
}
NS.Close();
IPClient.Close();
}
catch (Exception
err)
{
//Status.Text = err.Message;
Status.Text = err.StackTrace;
}
}
private void
button0_Click(object sender, EventArgs e)
{
if (Convert.ToUInt16(button0.Tag)
== 0) { L0 = 1; } else { L0 = 0; }
button0.Image = null;
}
private void
button1_Click(object sender, EventArgs e)
{
if (Convert.ToUInt16(button1.Tag)
== 0) { L1 = 1; } else { L1 = 0; }
button1.Image = null;
}
private void
button2_Click(object sender, EventArgs e)
{
if (Convert.ToUInt16(button2.Tag)
== 0) { L2 = 1; } else { L2 = 0; }
button2.Image = null;
}
private void
button3_Click(object sender, EventArgs e)
{
if (Convert.ToUInt16(button3.Tag)
== 0) { L3 = 1; } else { L3 = 0; }
button3.Image = null;
}
private void
button4_Click(object sender, EventArgs e)
{
if (Convert.ToUInt16(button4.Tag)
== 0) { L4 = 1; } else { L4 = 0; }
button4.Image = null;
}
private void
button5_Click(object sender, EventArgs e)
{
if (Convert.ToUInt16(button5.Tag)
== 0) { L5 = 1; } else { L5 = 0; }
button5.Image = null;
}
private void
button6_Click(object sender, EventArgs e)
{
if (Convert.ToUInt16(button6.Tag)
== 0) { L6 = 1; } else { L6 = 0; }
button6.Image = null;
}
private void
button7_Click(object sender, EventArgs e)
{
if (Convert.ToUInt16(button7.Tag)
== 0) { L7 = 1; } else { L7 = 0; }
button7.Image = null;
}
}
}
Labels:
Ethernet RS232
Modbus
ModBus Communication
ModBus RTU
ModBus RTU Communication
PLC Application
PLC Application Project
USR-TCP232
Ethernet RS232
Modbus
ModBus Communication
ModBus RTU
ModBus RTU Communication
PLC Application
PLC Application Project
USR-TCP232