Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

PROCEEDINGS OF SPIE

SPIEDigitalLibrary.org/conference-proceedings-of-spie

Data communication between


Panasonic PLC and PC using
SerialPort control in C#.NET
environment

Gao, Ting, Gan, Xiaochuan, Ma, Liqun

Ting Gao, Xiaochuan Gan, Liqun Ma, "Data communication between


Panasonic PLC and PC using SerialPort control in C#.NET environment,"
Proc. SPIE 9446, Ninth International Symposium on Precision Engineering
Measurement and Instrumentation, 94463H (6 March 2015); doi:
10.1117/12.2181373

Event: International Symposium on Precision Engineering Measurement and


Instrumentation, 2014, Changsha/Zhangjiajie, China

Downloaded From: https://www.spiedigitallibrary.org/conference-proceedings-of-spie on 15 Feb 2022 Terms of Use: https://www.spiedigitallibrary.org/terms-of-use


Data communication between Panasonic PLC and PC using SerialPort
control in C#.NET environment
Ting Gao ∗a, Xiaochuan Gana, Liqun Maa
a
Changcheng Institute of Metrology & Measurement, 1066 Post Box, Beijing, P.R.China 10095;

ABSTRACT

With the gradual promotion of Microsoft .NET platform, C# as an object-oriented programming language based on the
platform has been widely used. Therefore, more attention is concentrated on how to achieve the communication between
Panasonic PLC and PC efficiently and fast in C#.NET environment. In this paper, a method of using SerialPort control
which could be used for achieving communication between PLC and PC is introduced. Meanwhile, the reason of
abnormal thread when displayed the receiving data in form is analyzed and the programming method to solve the
problem of thread safety is designed. Achieving the communication of Panasonic PLC and PC in C#.NET environment
can give full play to the advantages of the .NET framework. It is practical, easy communication, high reliability and can
combine with other measurement and calibration procedures effectively and conveniently. Configuration software is
expensive and can only communicate with PLC separately, but these shortcomings can be solved in C#.NET
environment. A well-designed user interface realized real-time monitoring of PLC parameters and achieved management
and control integration. The experiment show that this method of data transfer is accurate and the program’ running is
stable.
Keywords: Panasonic PLC, C#.NET, SerialPort

1. INTRODUCTION
In the process of modern industrial measurement, the integration of measurement and control is the most effective means
to realize industrial automation. In this respect, measuring and testing technology will play a more important role and get
greater development. In the control system, more and more people using PLC as the controller, which is more convenient,
especially in the aspect of position, speed and torque. The Panasonic PLC, with its perfect function, small size and low
price, are increasingly applicated in the modern industry control system[1]. But it also has the disadvantages of poor data
management ability, can’t provide humanized interface etc. Therefore, we need to make up for these shortcomings and
give full play to its performance with the help of communication between PLC and PC. Communication between PC and
PLC is realized in two ways, which are configuration software and high level language. Though configuration software
is convenient, we need to pay the high cost of software, and secondary development is limited. In the software
development industry today, combined the visual operation of VB with the efficiency of C++, C# becomes the preferred
language of .NET development with powerful operation ability and innovative language features[2]. In many
measurement and control system, we need to combine other procedures developed for C# with PLC control, to make the
system not only can collect and store data in time, but also for advanced data processing, to achieve the integration of
measure, control and management and better complete the operation. Therefore, it’s very important to realize the
communication of Panasonic PLC and PC in the C#.NET environment.

2. PANASONIC PLC COMMUNICATION PROTOCOL


2.1 Communication process
Panasonic PLC communicates with PC through RS232. Data transmits in the form of ASCII[3]. First, the computer sends
command, PLC responds appropriately to the command. Then computer receives the respond. Its communication sent
format is like this.

∗ gaoting2008@163.com; phone +86-18610296328

Ninth International Symposium on Precision Engineering Measurement and Instrumentation


edited by Jiubin Tan, Xianfang Wen Proc. of SPIE Vol. 9446, 94463H · © 2015 SPIE
CCC code: 0277-786X/15/$18 doi: 10.1117/12.2181373

Proc. of SPIE Vol. 9446 94463H-1


Downloaded From: https://www.spiedigitallibrary.org/conference-proceedings-of-spie on 15 Feb 2022
Terms of Use: https://www.spiedigitallibrary.org/terms-of-use
2.2 Communication sent format
Command information:
AD AD Command Text code BCC BCC
% # CR
(H) (L) code 1 (H) (L)
Reply information (normal):
AD AD Response Text code BCC BCC
% $ CR
(H) (L) code 1 (H) (L)
Reply information(error occurred):
AD AD Error code Error code BCC BCC
% ! CR
(H) (L) (H) (L) (H) (L)
2.3 Communication sent format specification
Among them,”%” is the starting symbol, marking the beginning of each frame message. ”CR” is the ending symbol,
marking the end of each frame message. ”AD” is each PLC station address, represented by two hexadecimal numbers.
Such as “01” represents the first PLC. ”#”,”$”,”!” marking the type of the frame message, for which the corresponding
as follows: command information, response information (normal), response information (error). ”BCC” is check code,
represented by two hexadecimal numbers. Its initial value is “0”.Then bitwise exclusive OR from the initial bit to the
frame message in each byte.

3. THE REALIZATION OF THE COMMUNICATION PROGRAMMING


In the past, the software of communication with industrial PLC in Windows environment is MSCOMM control in VB6.0,
but in .NET has been upgraded to SerialPort control. SerialPort has been significantly optimized than the previous
MSCOMM. With this control you can easily achieve serial communication[4]. The system flow chart of serial
communication between Panasonic PLC and PC in C#.NET environment is shown in figure 2.1

Begin

Port initialization and control property set

Open the serial communication port

N
Send and receive data
Y

Close the serial communication port

Finish
Fig 1. system flow chart
3.1 Nameplace
The namespace of SerialPort class is SerialPort.IO.Ports. This new framework can not only access to the serial port on
your computer, but also communicate with serial devices. If you want to use SeriaiPort class, you should add “using
Sytem.IO.Ports;” in the front of the program.
3.2 The parameters of the serial port setting
First, you should get the serial port name of the current computer in the form load and display in comboBox control.
string[] portNames = SerialPort.GetPortNames();

Proc. of SPIE Vol. 9446 94463H-2


Downloaded From: https://www.spiedigitallibrary.org/conference-proceedings-of-spie on 15 Feb 2022
Terms of Use: https://www.spiedigitallibrary.org/terms-of-use
//Get the current computer serial port name array
foreach (string name in portNames)
{
comboBox1.Items.Add(name);
}
if (portNames.Count() != 0)
{
comboBox1.SelectedIndex = 0; // The first serial display in the comboBox
}
After selected serial communication port name in comboBox, set the serial port parameters and open the serial port.
serialPort1.PortName = comboBox1.SelectedItem.ToString(); // Set the port name
serialPort1.BaudRate = 9600; // Set the serial baud rate
serialPort1.Parity = Parity.Odd; // Set parity checking protocol
serialPort1.DataBits = 8; // Set the length of standard data bits of each byte
serialPort1.StopBits = StopBits.One; // Set the number of standard stop bits of each byte
serialPort1.ReadBufferSize = 100; // Set the size of the input buffer
serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);
// Add data receive events
serialPort1.Open(); // Open the serial port
3.3 Send data
According to Panasonic PLC communication protocol to read the data in the data register DT32712 for example. It’s
instruction code is "%01#RDD3271232713" and the corresponding hexadecimal ASCII is "25 30 31 23 52 44 44 33 32
37 31 32 33 32 37 31 33". After each ASCII character XOR, the check code is "54". Data transfer through
serialPort.Write(byte[] buffer,int offset,int count). Part of the send data program is shown below:
string strSend = “%01#RDD3271232713”; // Instruction data to be transmitted
byte t = 0;
byte[] bSend = Encoding.ASCII.GetBytes(strSend);
for (int i = 0; i < strSend.Length; i++) //XOR for instruction data, calculate the check code
{
t ^= bSend [i];
}
string strBCC = t.ToString("X2");
string strSend1 = strSend + strBCC + (char)13;
byte[] dytesSend = Encoding.ASCII.GetBytes(strSend1);
serialPort1.Write(dytesSend, 0, dytesSend.Length); // Send instructions
3.4 Receive data
Panasonic PLC receives the command of PC, responses and the result is sent to the relevant response data input buffer,
triggering DataReceived events. In the event of an DataReceived,PLC reads input data buffer by serialPort1. Read
(byte [] buffer, int offset, int count) function. Read input buffer data and set the while loop, when the number of bytes of
the receive buffer at constant exit the loop and then read data. PLC is ready to receive the next buffer data after receive
the buffer is empty.
Panasonic PLC sent response results to the data input buffer, triggering DataReceived events. In the DataReceived event
we use serialPort1. Read (byte [] buffer, int offset, int count) to read the data in input data buffer. In communication
process we need to note that when we directly displayed the processed data which receives from serialPort object in the
form of text controls you will find the system throws an exception thread. That's because the serialPort DataReceived
event is triggered on the auxiliary thread, rather than the main thread which the form controls in. According to MSDN
technical manuals, to refresh the displayed data of related components in the form, you must use Invoke to inspire
Delegate to access the UI components in the auxiliary thread that DataRecived triggered[5]. It will implement these
operations on the appropriate thread and achieve thread-safety calls. Its implementation schematic is shown in Figure 2.2.

Proc. of SPIE Vol. 9446 94463H-3


Downloaded From: https://www.spiedigitallibrary.org/conference-proceedings-of-spie on 15 Feb 2022
Terms of Use: https://www.spiedigitallibrary.org/terms-of-use
Ttread calls

False
Control.Invoke.Required

True

This.Invoke(new
SetTextCallback(AppendTextBox),readBuffer)

Refresh the UI components display directly

Finish
Fig 2. implementation schematic
Receive data procedures are as follows:
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
int rslength = 0;
bool loop = true; // Judge whether the data of the receive buffer is all received
SerialPortIsReceiving = true; // the bool type of judge whether the serial port is processing data
try
{
Thread.Sleep(30);
while (loop)
{
if (serialPort1.BytesToRead == rslength)
{
loop = false;
}
else
{
rslength = serialPort1.BytesToRead;
// Get the number of bytes of data in the receive buffer
}
Thread.Sleep(30);
}
serialPort1.Read(readBuffer, 0, rslength); // Read the receive buffer data
serialPort1.DiscardInBuffer();//Clear the receive buffer data
rslength = 0;
AppendTextBox(readBuffer);
}
catch { }
finally
{
SerialPortIsReceiving = false;
}
}
delegate void SetTextCallback(byte[] readBuffer);

Proc. of SPIE Vol. 9446 94463H-4


Downloaded From: https://www.spiedigitallibrary.org/conference-proceedings-of-spie on 15 Feb 2022
Terms of Use: https://www.spiedigitallibrary.org/terms-of-use
private void AppendTextBox(byte[] readBuffer)
{
try
{
if (control.InvokeRequired)
//Judge whether the caller thread is the outside of the thread that create control
{
SetTextCallback d = new SetTextCallback(AppendTextBox); // Create a delegate
this.Invoke(d, readBuffer); // Postback change request
}
else
{
string strReceived = Encoding.ASCII.GetString(readBuffer);
}
}
catch { }
}
3.5 Close serial port
C# serial communication involves multi-threading, when other threads are for data processing or UI update, if the simple
forced the closure of the serial interface, software would cause a deadlock. So we set the bool value to estimate whether
the serial port is disposing data or not in the DataReceived event, and when all of the data has been disposed we set the
bool vale to be false. When we get ready for closing the serial port, we need to check if serial port is receiving and
handling the data. And if so we should wait for it until the data is over. Close serial port procedures are as follows:
serialPort1.DataReceived -= serialPort1_DataReceived;
while (SerialPortIsReceiving) // Check whether data is being transmitted
{
Application.DoEvents();
}
serialPort1.Close(); //Close serial port

4. CONCLUSION
In this paper the author described the communication between Panasonic PLC and PC through the analysis of the
Panasonic PLC communication protocol and the argument of data transmission process of SerialPort controls. In this
method designed a good application software platform to control Renishaw XL-80 interferometer and Panasonic
FPGC32TH model PLC in the laser tracker calibration device. By that the correctness and reliability of communication
design method proposed in this paper have been verified.

REFERENCES

[1] Wang Huatang. Application of Japan's Panasonic PLC. Modern Business Trade Industry. Papers 9, 350-
351(2008)
[2] Wang Wenquan. Implementation of Serial Port Communication System Based on SerialPort. Science and
technology square. Papers 5, 21-23(2011)
[3] Panasonic Programmable Controller FPΣ User's Manual [S]
[4] Li Deqi. C # Programming [M]. Beijing: Science Publishers, 2005.
[5] PENG Wei. Serial Communication Programming of Thread-safe Based on .NET Platform,Journal of Hubei
University of Technology, Papers 27(2), 34-39(2012)

Proc. of SPIE Vol. 9446 94463H-5


Downloaded From: https://www.spiedigitallibrary.org/conference-proceedings-of-spie on 15 Feb 2022
Terms of Use: https://www.spiedigitallibrary.org/terms-of-use

You might also like