Click here to Skip to main content
Click here to Skip to main content

Fatek and Facon PLCs Communication Protocol Serial Interface

, 24 Jul 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
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 ;o (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 ModBus implementation 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).

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.

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.

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 master 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 command to all slaves) When the master wants to send command to one or all (Station number =0) it accords the station number assignment. The slave will send its own station No. when it send response message to master.
  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 master 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 error 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 LRC 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 message 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 station 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

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:

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 valid address, PLC will reply error code “A” (illegal address), and will not execute that command.

4-3 Communication Commands

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-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-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 discrete 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 unit 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, setting is prohibited.
  • IsAlive: Gets whether PLC is alive or not.
  • IsOpen: Gets whether link (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 new instance.
  • Open: Opens the comm. port and gets ready to start communication.
  • Read_Continuous: Returns continuous respective registers.
  • Read_DetailedStatus: Returns 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

  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:
    ...
    Private f_plc As Fatek.FatekPLC
    ...
  4. Initialize the object and start the communication:
    ...
    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:

    Fatek-00.PNG

    Fatek-01.PNG

    ...
    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:

    Fatek-01.PNG

    ...
    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:

    Fatek-01.PNG

    ...
    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:

    Fatek-01.PNG

    ...
    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):
    ...
    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. Fatek-01.PNG

    ...
    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)
    ...
  12. Getting the enabled status of DO (Y0~Y7) :

    Fatek-01.PNG

    ...
    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)
    ...
  13. Reading Counter/Timer 0~10 :
    ...
    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)
    ...
  14. Setting Counter/Timer 5~15:
    ...
    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)
    ...
  15. Testing Loop Back :
    ...
    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)

Share

About the Author

Mahdi Mansouri
Engineer
Malaysia Malaysia
PhD Candidate in Biomedical Eng., Control
M.Eng Electronics, Control
B.Eng Computer, Hardware
That's all ;D !

Comments and Discussions

 
QuestionThank you so much + request PinmemberMember 1074800814-Apr-14 4:45 
QuestionEthernet Communication PinmemberSalih HORTUM3-Dec-13 10:15 
QuestionCommunnitation Fatek-PC via ethetnet Pinmemberkiewlak1-Nov-13 11:13 
QuestionError in Data line, Data corrupted! Pinmembermdvel31-May-13 12:21 
Questionset register PinmemberNima Heydarzadeh27-May-13 15:05 
Questionvideo Pinmemberalvindedi5-Feb-13 3:19 
GeneralGreat job ! PinmemberSamjoke28-Jan-13 1:22 
QuestionQuick question PinmemberEnes Gundogmus9-Jan-13 23:42 
Hello,
Thanks for great article. But I dont know anything about PLC's. I just know that they are logical controllers. And I have a friend that works with these things. Can I operate a functional PLC with your library?
AnswerRe: Quick question PinmemberMahdi Mansouri10-Jan-13 3:19 
GeneralGreat Article Pinmembersals_25200623-Dec-12 11:08 
GeneralRe: Great Article PinmemberMahdi Mansouri10-Jan-13 3:20 
QuestionFaconServer library Pinmemberkavehmoh22-Oct-12 10:57 
AnswerRe: FaconServer library PinmemberMahdi Mansouri27-Oct-12 18:46 
GeneralMy vote of 5 Pinmembermrknk833-May-12 19:48 
QuestionDjokata PinmemberDjokata25-Mar-12 13:16 
Question.net Socket Pinmembersinanug29-Feb-12 5:08 
AnswerRe: .net Socket PinmemberMahdi Mansouri13-Mar-12 3:05 
QuestionConverting VB Codes to C# Pinmembermozdogan14-Oct-11 21:12 
AnswerRe: Converting VB Codes to C# Pinmembermozdogan15-Oct-11 22:21 
AnswerRe: Converting VB Codes to C# PinmemberMahdi Mansouri13-Mar-12 3:07 
QuestionCM25 PinmemberMember 419087627-Jul-11 21:46 
AnswerRe: CM25 PinmemberMahdi Mansouri28-Jul-11 3:30 
Question[My vote of 2] Formatting PinmvpRichard MacCutchan24-Jul-11 5:09 
AnswerRe: [My vote of 2] Formatting PinmemberMahdi Mansouri24-Jul-11 19:25 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141216.1 | Last Updated 25 Jul 2011
Article Copyright 2011 by Mahdi Mansouri
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid