Click here to Skip to main content
15,881,882 members
Articles / Desktop Programming / Win32

Fatek and Facon PLCs Communication Protocol Serial Interface

Rate me:
Please Sign up or sign in to vote.
4.88/5 (13 votes)
24 Jul 2011CPOL9 min read 133.2K   16.9K   26   42
Easily Surpass any OPC Component and use a user friendly .NET based class to interface Fatek and Facon PLC Series

Introduction

If you're tired of using some complex non-real-time COM/COM+ components (as I am ;D) that forces you to connect Fatek PLC series and are interested in an easy understanding, open source, real real-time .NET based object, you've just selected the right article.

This article introduces a purely code-based protocol implementation, what you can easily add into your project ensures you a safe reliable RS232/485 PC-PLC communication and surpass any OPC Servers which are presented in a tremendous priced; (The last OPC server I’ve searched was about 350$)!!

Note 1: There is no difference you’re intended to implement Ethernet, RS232/485 or Modem, the base protocol is applied in the object I’ll introduce. For MoModbusmplementation and address mapping, there is a general document you can download from the article or search it in Fatek Web Site (www.fatek.com).

1 Background

OOP programming and using an accurate implementation in real-time hardware dependant is something I've done once in DeltaDTB PID Controller.

2 Wiring

The first step of establishing an exact communication is the exact wiring, for the project we’ve considered, the old popular RS232 (2-2, 3-3, 5-5) is the thing answered properly (look below the picture).

FATEKandFACONCommProtocol/FatekWiring.JPG

3 Basic Definition

3-1 Master and Slave

FATEK PLC is defined as slaves in the communication with peripheral devices that are always defined as masters when communicate with FATEK PLC. All the peripheral devices send the message when they communicate with FATEK PLC and it responds when receiving the message from masters.

FATEKandFACONCommProtocol/MasterSlave.JPG

3-2 The Communication Message Format of FATEK PLC

There are 6 data columns in the FATEK PLC communication format including command (master) and response (slave) message.

FATEKandFACONCommProtocol/CommFormat.JPG

  1. Start code (STX): The hexadecimal code of the STX in ASCII code is 02H. The start characters are all STX in command and response message. The receiving site can determine the data start code with STX.
  2. The station number of slave: The station numbers are hexadecimal two-number value. There is only maa ster station and are 255 slave stations in the PLC communication frame. Every slave station has the only number from 1~FEH. (if the station No. is 0, it means the master can send cothe mmand to all slaves) When the master wants to send cothe mmand to one or all (Station number =0) it accords the station number assignment. The slave will send its own station No. when it send rethe sponse message to mathe ster.
  3. Command code: The command number is two numbers of hexadecimal systems. It is the action which the master wants the slave to execute. For example, to read or write the status of discrete, force setting, run, stop… The command number which is received from mathe ster is also included in response message when slave sends the response message.
  4. Data information: The data information contains 0 (no data) ~500 ASCII character. The data in this column is to assign the address or value for reading or writing. The beginning of this data information contains the error code in the response message. In normal conditions (no error happened), the error code must be 0 (30H) in the beginning and then follow the responding status or value in the response message. When erthe ror happens, it will be the error code instead of 0 (30H) and it will not follow the data information.
  5. Checksum: Checksum checks the hexadecimal value of ASCII code in the previous 1~4 columns and produces one checksum value in one byte length (two hexadecimal value 00~FF) with “LRC” (Longitudinal Redundancy Check) method. This message will be checked with the same way at the receiving side when the message is received. When the two check values are the same, it means the data transferred correctly. If the two check values are different, there are some errors that happened. The calculation of LRthe C method is to add all the hexadecimal value (8 bits length) of ASCII code and ignore to carry the number to keep the check value at 8 bits length.
  6. End code (ETX): The hexadecimal code of EXT code of ASCII is 03H. The EXT code of either command or response is all ETX. When the receiving side receives the ETX code, it means the data transmission terminated and starts to process command or data.

Note 2: Format of sending and receiving methe ssage are the same.

Note 3: Data column in some commands may not exist, and checksum will impose on previous 1~3 columns.

Note 4: The default value of stthe ation number for PLC is all 1. The station number cannot be amended in the net; It can be changed or amended through FP-07C or Winproladder.

4-Symbols and Commands Description

4-1 The Communication Error Code Table

FATEKandFACONCommProtocol/ErrorTable.JPG

4-2 The Classification and Assignment of Components

The main function of PLC communication is to read and write the status or value inside PLC components. Concerning the discrete and register which are available for read and write and address assignment are as shown in the following table:

FATEKandFACONCommProtocol/Symbols.JPG

Note 5: The discrete status (X, Y, M, S) can combine 16 or 32 continuous status as the 16-bit or 32-bit register, such as the above table WX???? or DWX????, but ???? should be multiple of 8.

Note 6: It needs 5 characters when assigning the discrete address and 6 characters when assigning the 16-bit register address and 7 characters to assign the 32-bit register address.

Note 7: The address boundary of components in the above table is the largest for FACON PLC. Users should notice the valid address and attribution of each PLC components. (Ex. The boundary for X?Y address is 0000~0255; for S is 0000~0999 of FBE-PLC). If exceeding the boundary of valthe id address, PLC will reply error code “A” (illegal address), and will not execute that command.

4-3 Communication Commands

FATEKandFACONCommProtocol/OperationDescription.JPG

Note 8: The message of discrete status is represented by one character (1 means ON, 0 means OFF) and the data of 16-bit register uses 4 characters to represent the value of one WORD (0000H~FFFFH).

Note 9: The data of 32-bit register is DW (two continuous Words), it has to use 8 characters to represent its data. If the component is 32-the bit register, the component has to be treated as 2W. For example, in command code 46 and 47, they can process 64 16-bit components and only process 32 32-bit components.

Note 10: In the command code 48 and 49, the message length is the total of discrete and word. They cannot exceed 64W (command 48) and 32W (command 49). As one point is increased, its total words will decrease one word. It is the same in the other hand. Because the message length of 32-the bit component uses 2 words, it will be less 2 words or point when increase one 32-bit component. For example, the message length of command 48 is 1~64W. If it read 20 “32-bit components”, its message will occupy 40 words and remain 24W available for disthe crete or 16-bit register.

Note 11: The operation (read and write) of continuous discrete or register is not only one component and the numbers are continuous so that you don’t need to assign their components number during your assignment. You just only need to appoint the start number and how many components (N). Its operating object can only being one of discrete or register and cannot be operated randomly.

Note 12: The random operating objects can read or write several discrete and register. As their number is not continuous, you have to appoint their number and allow operating discrete and register randomly.

Note 13: The Save and Load operation saves all the program area of PLC to disk or loads to PLC. The maximum data transferring in one communication is 64 words so that it will take times of communication to save or load to complete.

5 Software

5-1 Fields

  • byte_Time: Indicates the time each byte takes to be transferred from PC to PLC or vice versa.
  • Is_alive: Indicates whether PLC is available or not, set to “False” as default.
  • Is_open: Indicates whether the comm. Port has been obtained and opened or not, set to “False” as default.
  • Link: The private serial comm object set to “Null” as default.
  • Str_discrete_area: A string array contains discrete area symbols (“X”,”Y”,...).
  • Str_register_area: A string array contains continuous area symbols (“R”,”D”,…).
  • Unit_addres : contains unithe t address, set to 1 as default.

5-2 Properties

  • Alive: Private property Gets/Sets if PLC is available or not.
  • ByteTime: Read only property indicates the time a byte takes to be transferred from plc to PC and vice versa.
  • CommSetting: Sets/Gets the Link properties. Please note when connecting, setthe ting is prohibited.
  • IsAlive: Gets whether PLC is alive or not.
  • IsOpen: Gets whether linthe k (comm. Port) is opened or not.
  • Opened: Private property Gets/Sets if Comm Port is opened or not.
  • UnitAddress: Gets/Sets PLC unit address.

5-3 Important Methods

  • Close: Closes the comm. port and ends the communication.
  • Error: Generates error string based on input error code.
  • GetChecksum_byte: Creates Checksum code from given byte array.
  • Getchecksum_str: Creates Checksum code from given string.
  • Loopback: Verifies the rectitude of the connection properties and line, set unit address and functionality of PLC.
  • New: Creates a new instance.
  • Open: Opens the comm. port and gets ready to start communication.
  • Read_Continuous: Returns continuous respective registers.
  • Read_DetailedStatus: Returns the detailed status of PLC.
  • Read_Discrete_EnableStatus: Returns the status of Enable/Disable of Continuous Discrete Registers.
  • Read_GistStatus: Unveils Gist Status of PLC.
  • Set_Continuous: Writes data to specific continuous register(s).
  • Set_Discrete: Sets Discrete DO/Relays.
  • Set_Mode: Sets PLC working mode.
  • Status: Unveils PLC Status based on given Response Structure.
  • Test: Verify whether PLC is available or not.

How to Use

FATEKandFACONCommProtocol/Fatek-01.PNG

VB.NET
...
Dim f_err As Fatek_Error_Code = New Fatek_Error_Code
Dim en_arr As Boolean() = f_plc.Read_Discrete_EnabledStatus
			(Fatek_Discrete_Area.X, 11, 0, f_err)
...
  1. Add fatek class to your project.
  2. Import fatek class into your form/module/class.
  3. Create a variable in your form/class/module as:
    VB.NET
    ...
    Private f_plc As Fatek.FatekPLC
    ...
  4. Initialize the object and start the communication:
    VB.NET
    ...
    f_plc = New Fatek.FatekPLC(New System.IO.Ports.SerialPort
    	("COM1", 115200, IO.Ports.Parity.Even, 7, IO.Ports.StopBits.One))
    f_plc.Open()
    ...
  5. For checking the plc status:

    FATEKandFACONCommProtocol/Fatek-00.PNG

    FATEKandFACONCommProtocol/Fatek-01.PNG

    VB.NET
    ...
    Dim f_error As Fatek_Error_Code
    Dim f_response As Fatek_PLC_GistStatus = f_plc.Read_GistStatus(f_error)
    ...
  6. For getting plc detailed status:

    FATEKandFACONCommProtocol/Fatek-01.PNG

    VB.NET
    ...
    Dim f_response As Fatek_PLC_DetailedStatus = New Fatek_PLC_DetailedStatus
    Dim f_error As Fatek_Error_Code = New Fatek_Error_Code
    f_response = f_plc.Read_DetailedStatus(f_error)
    ...
  7. For setting PLC into run mode:

    FATEKandFACONCommProtocol/Fatek-01.PNG

    VB.NET
    ...
    Dim e_code As Fatek_Error_Code = New Fatek_Error_Code
    e_code = f_plc.Set_Mode(Fatek_PLC_Mode.Run)
    ...
  8. For setting PLC into stop mode:

    FATEKandFACONCommProtocol/Fatek-01.PNG

    VB.NET
    ...
    Dim e_code As Fatek_Error_Code = New Fatek_Error_Code
    e_code = f_plc.Set_Mode(Fatek_PLC_Mode.Stopped)
    ...
  9. Setting internal relay (M00000):
    VB.NET
    ...
    Dim f_err As Fatek_Error_Code = 
    	f_plc.Set_Discrete(Fatek_Discrete_Area.M, Fatek_DIO_Mode.Set, 0)
    ...
  10. Getting the enabled status of DI (X0~X10) :
  11. Getting the enabled status of DO (Y0~Y7) :

    FATEKandFACONCommProtocol/Fatek-01.PNG

    VB.NET
    ...
    Dim f_err As Fatek_Error_Code = New Fatek_Error_Code
    Dim en_arr As Boolean() = f_plc.Read_Discrete_Status(Fatek_Discrete_Area.Y, 8, 0, f_err)
    ...
  12. Reading Counter/Timer 0~10 :
    VB.NET
    ...
    Dim f_err As Fatek_Error_Code = New Fatek_Error_Code
    Dim short_arr As Short() = f_plc.Read_Continuous
    		(Fatek_Register_Area.CTR, 11, 0, Fatek_Data_Type.Short, f_err)
    ...
  13. Setting Counter/Timer 5~15:
    VB.NET
    ...
    Dim f_err As Fatek_Error_Code = f_plc.Set_Continuous(New Short() 
    {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 5, Fatek_Register_Area.CTR)
    ...
  14. Testing Loop Back :
    VB.NET
    ...
    Dim err_code As Fatek_Error_Code = New Fatek_Error_Code
    Dim bln As Boolean = f_plc.LoopBack(err_code)
    ...

Acknowledgments

It's my appreciation to thank Prof. Kamalaldin Farzaneh, who is not only my manager, but also my teacher who has let me test, fall and rise again.

History

  • First Release: Tuesday 19 July 2011
  • Updated: Wednesday 20 July 2011
  • Updated: Sunday 24 July 2011, Labview 7.0 Implementation added

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Engineer
Sweden Sweden
Embedded System Researcher

Comments and Discussions

 
QuestionData information interpretation Pin
Fernando Balseca Acosta17-Nov-21 5:24
Fernando Balseca Acosta17-Nov-21 5:24 
QuestionI am Unable to use your Project with Labview 2016 Pin
Member 1454603830-Jul-19 23:44
Member 1454603830-Jul-19 23:44 
QuestionC# version Pin
Ar.Gorgin10-May-18 22:26
Ar.Gorgin10-May-18 22:26 
Questionplc to vb Pin
Member 130667853-Apr-17 11:05
Member 130667853-Apr-17 11:05 
Questionhow to open password protected file without knowing true password Pin
Member 1287914430-Nov-16 4:08
Member 1287914430-Nov-16 4:08 
QuestionEthernet communication Pin
Azhorel13-Sep-16 4:44
Azhorel13-Sep-16 4:44 
QuestionFatek PLC Fbs-20ma and vb.net connection Pin
Member 1264764226-Jul-16 18:48
Member 1264764226-Jul-16 18:48 
AnswerRe: Fatek PLC Fbs-20ma and vb.net connection Pin
Azhorel13-Sep-16 4:45
Azhorel13-Sep-16 4:45 
QuestionMr. Mahdi Mansouri - Delphi Application for Fatek Pin
Arsisman20066-Jul-16 5:19
Arsisman20066-Jul-16 5:19 
Praisethanks Pin
Member 1257351020-Jun-16 3:37
Member 1257351020-Jun-16 3:37 
GeneralRe: thanks Pin
Mahdi Mansouri26-Jun-16 20:37
Mahdi Mansouri26-Jun-16 20:37 
Questiongreat job Pin
Member 122486069-Mar-16 12:24
Member 122486069-Mar-16 12:24 
Questioncontinue read numeric data registers from FATEK PLC Pin
Member 1218789022-Dec-15 17:29
Member 1218789022-Dec-15 17:29 
QuestionI have plc fatk fbe-20ma and fatek fbs-40mc and want to connect them by rs 485 in high speed connction Pin
Member 1177389417-Jun-15 13:01
Member 1177389417-Jun-15 13:01 
Questiona question? Pin
kuakman6-Apr-15 7:50
kuakman6-Apr-15 7:50 
QuestionThank you so much + request Pin
Yaron Shahrabani14-Apr-14 3:45
Yaron Shahrabani14-Apr-14 3:45 
QuestionEthernet Communication Pin
Salih HORTUM3-Dec-13 9:15
Salih HORTUM3-Dec-13 9:15 
QuestionCommunnitation Fatek-PC via ethetnet Pin
kiewlak1-Nov-13 10:13
kiewlak1-Nov-13 10:13 
AnswerRe: Communnitation Fatek-PC via ethetnet Pin
Member 1264764226-Jul-16 18:03
Member 1264764226-Jul-16 18:03 
QuestionError in Data line, Data corrupted! Pin
mdvel31-May-13 11:21
mdvel31-May-13 11:21 
Questionset register Pin
Nima Heydarzadeh27-May-13 14:05
Nima Heydarzadeh27-May-13 14:05 
Questionvideo Pin
alvindedi5-Feb-13 2:19
alvindedi5-Feb-13 2:19 
GeneralGreat job ! Pin
Samjoke28-Jan-13 0:22
Samjoke28-Jan-13 0:22 
GeneralRe: Great job ! Pin
Mahdi Mansouri15-Jun-15 18:14
Mahdi Mansouri15-Jun-15 18:14 
QuestionQuick question Pin
Enes Gundogmus9-Jan-13 22:42
Enes Gundogmus9-Jan-13 22:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.