You are on page 1of 10

Learn PLC communication from basic!!

BASICS OF SERIAL COMMUNICATION


1. Overview of Serial Communication
 What is Serial Communication?
 [FOR YOUR REFERENCE]
Devices that can be Used for Serial Communications.

Control device Measurement instrument


e.g. PLC e.g. Displacement sensor (GT2 Series)

Measured data

Set value
(Such as upper or lower limit)

2. Mechanism of Serial Communication


 What Send/Receive Mode is Used?
 What Operation Mode is Used?
 What Commands/Responses are Sent and Received?
 [FOR YOUR REFERENCE]
What is ASCII Code? PLC Displacement sensor
Command
Send measured
value.

- Measured
value is
-9.9999.
- Error Response

3. Points to note for Communication Settings


 What are the Important Points to note for Communication Settings When Connecting to
Serial Communication Devices?

Baud rate is the communication speed


for serial communication.

Please check baud rate


setting!
1. Overview of Serial Communication
What is Serial Communication?

Overview of Serial Communication


In FA, a combination of control device and measurement instrument is generally used for controlling. Serial
communication is established to execute control by incorporating data measured by a measurement instrument,
or changing set values of a measurement instrument from a controlling unit. Other than data exchange, serial
communication enables external control of devices such as exchanging error information or changing to an upper
and a lower limit. The advantages of of having many available devices and simplicity in settings makes serial
communication the most widely used communication system currently.

Control device Measurement instrument


e.g. PLC e.g. Displacement sensor (GT2 Series)

Measured data

Set value
(Such as upper or lower limit)

What is Serial Communication?


In simple terms, Serial Communication is a communication system that sends and receives data by matching settings
such as baud rate between the communication devices.

In this document, send-receive mode/operation mode/command structure are described, followed by


explanations of setting methods with general communication devices.

[FOR YOUR REFERENCE]

What types of devices can be used for serial communication?


Communication devices that can be used for serial communications

In order to exchange data between control device and measurement instrument, there are many measurement instruments that
support serial communication.

 Typical KEYENCE products that support serial communication

Measurement instrument Displacement sensor Barcode reader


(LK-G Series) (GT2 Series) (SR Series)

Other than KEYENCE products, there are many devices such as scales, power meters, inverters, temperature controllers, etc. that
can be used for serial communication.

2
2. Mechanism of Serial Communication
What Send/Receive Mode is Used?

Data Send/Receive Mode


There are two types of data send/receive modes for serial communication: “Command/Response mode”
which has command sending device and corresponding device that receives the commands and sends back the
response; “one-way only mode” which does not need to send commands, just like bar code readers, these devices
continuously send information whenever data is updated.

 Command/Response Mode
When a command is sent, the receiving device returns a response.

PLC Displacement sensor


Command
Send measured
value.

- Measured
value is
-9.9999.
- Error Response

 One-way Only Mode


Data is sent by a device without a command.

PLC Barcode readers

- It is ABCD.
Data

Not only serial communication devices, but also PLC has data exchange system (operation mode). The following
page will describe a PLC data exchange system (operation mode).

3
2. Mechanism of Serial Communication
What Operation Mode is Used?

Introduction of Operation Mode


In order to establish serial communication, it is necessary to set up an operation mode suitable for each serial
device. This time, operation modes for KEYENCE’s serial communication units (KV-L20V) are introduced.

 Some of Operation Modes for KV-L20V

Type Mode Description and use


For higher-level link KV mode Higher-level link Used for reading/writing DM values from high-level component (PC)

With ladder diagram program, operates send/receive data and send/


Free transmission mode
receive relay to exchange data with external devices
For communication with
external devices With PROTOCOL STUDIO, defines send/receive commands
PROTOCOL STUDIO mode (Communication Macro) and manages send/receive timing by ladder
diagram program to execute data exchange with external devices

Proper Use of Operation Mode


There are two main types of operation modes for serial communication unit KV-L20V as follows:

 For Higher-Level Link: used for communications with various measurement instruments
[Explanation] A mode in which commands that can be used in PLC are sent from PC in advance and PLC automatically
sends back the responses. Programs are created by a PC, PLC’s ladder diagram program is not required.

Serial communication device PLC


e.g. PC

Command

Response

 For Communication With External Device: used for reading data from PCs, etc.
[Explanation] A mode in which PLC creates and sends protocol/command corresponding to serial
communication the devices, or creates program to receive commands sent from serial devices.

PLC Serial communication device


e.g. Displacement sensor

Command

Response

When connecting to a measurement instrument, “free transmission mode” and “PROTOCOL


STUDIO mode” for external communication devices create unique commands for connecting
device are used.

Now, what types of Command/Response are sent/received between the devices?


The following page will describe the send and receive Commands/Responses.

4
2. Mechanism of Serial Communication
What Commands/Responses are Sent and Received?

Examples of Commands/Responses
With the Commands/Responses mode that sends/receives commands and responses, usually the command receiving
side will specify device-specific commands, therefore, the sending device needs to create and send commands that
are suitable for the receiving device.
In the case of KEYENCE displacement sensor – GT2 Series, when “M0” command is received, the measured values
are sent back as responses from all sensor amplifiers.

 Example: read measured values from GT2 Series. (Please refer to a manual of each device for details of commands)
Command
PLC M 0 CR LF GT2 Series
(Displacement sensor)
Send measured value.

Measured value is
-9.9999.

Response
M 0 – 9 . 9 9 9 9 CR LF

Command M 0 CR LF
Send measured value.

"Send measured (End)


value."

Response Measured value is M 0 – 9 . 9 9 9 9 CR LF


-9.9999.
"Measured value "-9.9999" (End)
is as follows:"

Now, in the following page, data composition typical to serial communication devices is introduced.

[FOR YOUR REFERENCE]

What is ASCII Code? Upper 4 bits

Meaning of ASCII Code

It is a scheme to replace characters such as alphabets, numbers, symbols


with 1 byte numerical data.

Example of ASCII Code

Using the ASCII Code Chart (the table on the right), let’s convert “M” to a
Lower
numerical data. 4 bits
From “M” in the ASCII Code Chart, take vertical and horizontal directions
to find numbers assigned for the line and column.

ASCII Code Expression by


hexadecimal conversion
M $ 4D

ASCII Code Chart

5
2. Mechanism of Serial Communication
What is the Composition of Command/Response?

Composition of Command/Response
Commands for serial communication are communication device-specific. It is necessary for PLC to send data based
on the protocol* of the receiving communication device. Details of protocol are described in a manual of serial
communication device.
* Protocol: Rules for device-to-device communication.

Example of a Protocol Composition


Example of a protocol composition Protocol for serial communication is composed of as follows:

Header Station Number Data Length Data Check Code Delimiter

1. Header: Data that indicate the start of serial communication data


[Example] “STX” (02), “ACK” (06), “NAK” (15) (ASII Codes in parenthetic)

2. Station Number: Data that specify device number


3. Data Length: Data that specify command length
4. Data: Command data that read measured values and write set values
5. Check Code: Error detection data that check occurrences of data corruption due to noise, etc.;
(please refer to FOR YOUR REFERENCE in the following page)

6. Delimiter: Data that indicate the end of serial communication data


[Example] “ETX” (03), “CR” (0D), “CR” (0D) + “LF” (0A) (ASII Codes in parenthetic)

Please note that depending on the device, some settings may not be necessary for these items.

Protocol of KEYENCE GT2 Series


The following shows the protocol of KEYENCE displacement sensor GT2 Series.
Protocol is device-specific, so please refer to a manual for each device.

 Read all data from all sensor amplifi ers (MO command)
Command

M O CR LF

Response
Data of the sensor amplifier with Data of the sensor amplifier with Data of the sensor amplifier with
M O ....... CR LF
ID:00 ID:01 the last ID No.
, , , ,

For the protocol of GT2 Series, there are No Header, Station Number, Data Length or Check Code, therefore,
it is necessary to establish communication based on this protocol.

In order to establish serial communication, not only sending commands normally but communication settings such
as the baud rate of the two devices must match. In “3. Points for Communication Settings”, necessary parameters to
be set for serial communication are introduced.

6
2. Mechanism of Serial Communication
[FOR YOUR REFERENCE]

What is Check Code?


Overview of Check Code

Check code is an error detection data to check occurrences of data corruption due to noise, etc. in communication.

Check code Check code


Data transfer
Data A Data A

Computation Computation
Add

A B

A B Data normal

A B Data error

Types of Check Code

There are several types of check code according to the difficulty level of the computation method or reliability.
Typical computation methods are as follows:
· Checksum *Please refer to a creation example below

· LRC (horizontal parity)


· CRC-CCITT
· CRC-16

Example of creating check codes

From “ABCDE” in the ASCII Code, compute checksum*.


*A checksum is a method to add data by 1 byte (character) in specifi c range

Hexadecimal
ASCII Code number

"A" 41
+ "B" 42
+ "C" 43
+ "D" 44
+ "E" 45

14F Check code is 4F (Hexadecimal number)

A higher byte 1 is truncated since a check code of 1 byte is used in this example.

7
2. Mechanism of Serial Communication
[FOR YOUR REFERENCE]

Check of Command/Response
Check Method for Command/Response

There may be occasions that the communication with serial communication devices can not be properly established
even when sending a command after confirming a protocol. In such cases, it is necessary to check what types
of data are exchanged between devices. A general check method is to connect a protocol analyser* between
communication devices. However, using a serial communication unit (KV-L20V) enables data exchange check
without a protocol analyser.
* Protocol analyser … a device to analyse communication data

Check Screen for KV-L20V

When using the serial communication unit (KV-L20V), Commands and Responses can be checked by computers.

 Screen for check when using the serial communication unit (KV-L20V)

Command (KV-L20V  GT2 Series)

Response (GT2 Series  KV-L20V)

Command Normal
ending

Communication
Command Delimiter

Response

Communication Measured data Delimiter


Command

8
3. Points for Communication Settings
What Settings are Necessary When Connecting to Serial Communication
Devices?

Communication Settings for Connecting with


Serial Communication Devices
In order to establish the communication with serial communication devices, the serial communication settings of the
two devices must match. Since communication settings are specified by specification of the device, it is necessary to
change them so that two devices to be connected have same settings.

Items for Communication Settings


In this section, six items to be set for establishing communication are introduced.

[FOR YOUR REFERENCE]


As checkboxes are prepared, when you complete a setting of each item, put a check in the checkbox.

There are RS-232C and RS-422/485 interfaces that support serial devices.
Please check interface!
 Difference between RS-232C and RS-422/485
RS-232C PLC
Communication
(1:1 communication only)
distance:
up to 15 m

PLC Communication distance: up to 1200 m


RS-422/485
(1:N communication available)

Baud rate is communication speed for serial communication.


Please check baud rate setting!
 A case of different baud rate setting
Data Data
X
X
X
X
X
X

Higher baud rate Lower baud rate

When baud rates are different, some data cannot be received and normal data transmission is not possible.
*Also in the case of higher baud rate of receiving side, you cannot establish communication.

9
3. Points for Communication Settings
What Settings are Necessary to Connect with Serial Communication Devices?

Data bit length specifi es the number of bytes a datum is composed of.
Please select data bit length from either “7 bit” or “8 bit”!

Start bit indicates the beginning and end of data.


Please confirm if there is a start bit (1 bit) or not!

Stop bit indicates the end of data.


Please select stop bit from either “1 bit” or “2 bit”!

Parity is a bit to detect data corruptions due to noise, etc.


Please select parity from either “even parity” or “odd parity”!
 In the case of even parity
[Even parity] In the case of having an even number of 1 (ON) in a given set of data bits, it will be 1 (ON), and an odd
number, it will be 0 (OFF).

There are four ONs in a datum.


ON
 4: Even number
OFF Parity bit ON

1 2 3 4

When the above six communication settings are matched between the two devices, communication
is ready. Since preparation for serial communication is completed, please create commands and start
sending them to serial communication devices.

www.keyence.com

Copyright
Copyyrig (c) (c)
ight 20102010
(c KEYENCE CORPORATION.
KEYENCE CORPORAll rights
PORA reserved.
ATION
TIO . All rights rese
erve
ved
d. KVSerial-WW-EN1102-E 1091-2 E 600
6008887 Printed in Japan
apan KVSerial-WW-EN1102-E 1091-2 E 600887
* 6 0 0 8 8 7 *

You might also like