Introduction

If you're tired of using some complex non-real-time COM/COM+ components (as I'm ;D) that force 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$)!!

Note1: There is no difference you’re intended to implement Ethernet, RS232/485 or Modem, the base protocol is the applied in the object I’ll introduce. For ModBus implementation and address mapping, there is a general document you can download it 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 communicate with FATEK PLC and its respond when receive 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 No. 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 want to send command to one or all (Station No. =0) it accords the station No. assignment. The slave will send its own station No. when it send response message to master.

3-Command code: The command No. is two numbers of hexadecimal systems. It is the action which the master wants slave to execute. For example, to read or write the status of discrete, force setting, run, stop… The command No. which is received from master is also included in response message when slave send 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 condition (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 happened, it will be the error code instead of 0 (30H) and it will not follow the data information.

5-Checksum: Checksum check the hexadecimal value of ASCII code in the previous 1~4 columns and produce 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 error 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 No. for PLC is all 1. The station No. 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 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 assign the discrete address and 6 characters when assign the 16-bit register address and 7 characters to assign the 32-bit register address.

Note 7: The address boundary of components in 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 exceed 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 increase one point, 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

a. byte_Time : Indicates the time each byte takes to be transferred from PC to PLC or vice versa.

b. Is_alive : indicates whether PLC is available or not, set to “False” as default.

c. Is_open: Indicates whether the comm. Port has been obtained and opened or not, set to “False” as default.

d.Link: The private serial comm object set to “Null” as default.

e. Str_discrete_area: a string array contains discrete area symbols (“X”,”Y”,...).

f. Str_register_area: a string array contains continuous area symbols (“R”,”D”,…).

g. Unit_addres : contains unit address, set to 1 as default.

5-2- Properties:

a. Alive: Private property Gets/Sets if PLC is available or not.

b. ByteTime: Read only property indicates the time a byte takes to be transferred from plc to PC and vice versa.

c. CommSetting: Sets/Gets the Link properties. Please note when connecting, setting is prohibited.

d. IsAlive: Gets whether PLC is alive or not.

e. IsOpen: Gets whether link (comm. Port) is opened or not.

f. Opened: Private property Gets/Sets if Comm Port is opened or not.

g. UnitAddress: Gets/Sets PLC unit address.

5-3-Important Methods

a. Close: Closes the comm. port and ends the communication.

b. Error: Generates error string based on input error code.

c. GetChecksum_byte: Creates Checksum code from given byte array.

d. Getchecksum_str: Creates Checksum code from given string.

e. Loopback: Verifies the rectitude of the connection properties and line, set unit address and functionality of PLC.

f. New: Creates new instance.

g. Open: Opens the comm. port and be ready to start communication.

h. Read_Continuous: Returns continuous respective registers.

i. Read_DetailedStatus: Returns detailed status of PLC.

j. Read_Discrete_EnableStatus: Returns the status of Enable/Disable of Continuous Discrete Registers.

k. Read_GistStatus: Unveils Gist Status of PLC.

l. Set_Continuous: Writes data to specific continuous register(s).

m. Set_Discrete: Sets Discrete DO/Relays.

n. Set_Mode: Sets PLC working mode.

o. Status: Unveils PLC Status based on given Response Structure.

p. Test: Verify whether PLC is available or not.

How to use

a. Add fatek class to your project.

b.Import fatek class into your form/module/class.

c.Create a variable in your form/class/module as:

Private f_plc As Fatek.FatekPLC

d. 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()

e. 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)

f. 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)

g. 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)

h. 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)

i. Setting internal relay (M00000) :

Dim f_err As Fatek_Error_Code = f_plc.Set_Discrete(Fatek_Discrete_Area.M, Fatek_DIO_Mode.Set, 0)

j. Getting the enabled status of DI (X0~X10) :

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)

k. 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)

l. 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)

m. 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)

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

推荐.NET配套的通用数据层ORM框架:CYQ.Data 通用数据层框架
新浪微博粉丝精灵,刷粉丝、刷评论、刷转发、企业商家微博营销必备工具"