Help in setting up COM 3 for serial communications onboard the AC800M
I have recently started using PLCs and am busy porting my code from my microcontroller environment to the PLC environment. I have succesfully been able to get analogue inputs (4-20mA), digital inputs and digital outputs working.
I however now need to communmicate with a GSM module via RS232. I have looked in the help folder and have discovered that one can use the serial library (SerialCommLib) to set up a port for my own self-defined serial protocol. As far as I know, one can use COM3 as a general purpose RS232 port.
The first problem I am facing is that the SerialCommLib functions seem to only be working in FBD and not in structured text. I prefer to code in structured text as I am use to programming in C. Is there any way I can import these funcitons?
I have tried to set up the port (COM 3), with the SerialConnect function block, but I don't think I have chosen the correct channel as I do not get an Id returned from the funciton block. The status variable is always -6811.
Can someone please help me with this? Perhaps there is some sample code which I can look at to get my code up and running. Thanks in advance for any help.
Voted best answer
Sure thing Willians, I accomplished the task as follows:
The first thing to ensure is that you have the "SerialHwLib" in your project. This is a hardware libary that allows you to add a Serial Protocol to your project. To insert the libary go to ECU -> Libraries -> Right click on Hardware and insert the "SerialHwLib" into your project.
You must now connect this hardware library under your "Controllers" tab. To this proceed as follows: Go to Controllers -> PLC_1(...) -> Right click on Connect Library and point to the "SerialHwLib" that you added earlier.
Now that we have the "SerialHwLib" library we can configure our hardware port. The only COM port which we can configure on our PLC is COM3. Double click on "3 Com" under Controllers -> Hardware AC 800M. In the settings tab, configure the COM port as you would normally do by selecting the correct baud rate and other parameters.
We must insert a unit under "3 Com" now. To do this right click on "3 Com" and click on Insert Unit. In the tree that appears click Libaries in Project -> SerialHwLib -> Hardware types -> SerialProtocol. Give your SerialProtocol and appropiate name.
This concludes the hardware setup, the next thing to do is the software setup. To do this we must add the "SerialCommLib" to our project. Do this by right clicking in "Libaries" under ECU; and insert "SerialCommLib". Under Applications, don't forget to connect the SerialCommLib under "Connected Libaries".
The following code can be used now to create a connection and setup up the port; I have used structured text language to setup the COM port. Please read my previous post on how I added the function blocks to my code:
(*Make a connection to the serial port that we want*)
SerialConnect_1( En_C := En_C,
Channel := Channel,
Valid => Valid,
Error => Error,
Status => Status,
Id := COM3_Ch_Id );
(*Setup the parameters for our serial port*)
(*Only do the setup if the SerialConnect returns as valid*)
SerialSetup_1( Id := COM3_Ch_Id,
Req := Valid,
ChSumType := 0,
ChSumLength := 1,
ChSumStart := 1,
ChSumStop := 1,
EnLineEditor := false,
EnEchoChar := false,
Done => Done,
Error => Error,
Status => Status,
ParError => ParError );
The variables above were defined as follows:
En_C bool true
Channel string[16] '0.3'
Valid bool
Error bool retain
Status dint retain 1
COM3_Ch_Id Comm_Channel_Serial
Done bool retain false
ParError bool retain
RS232_Info string[140] 'Hello World :)'
The following code can be used to send a message via the COM port:
SerialWrite_1( Id := COM3_Ch_Id,
Req := Done,
EndChar := 10,
Done => Done,
Error => Error,
Status => Status,
Sd := RS232_Info,
ParError => ParError );
Answers
Edit:
I figured out that to add the function block in structured text; you first have to declare it under "Function Blocks". I will continue playing around with the different funciton blocks and will report back if I make any progress.
Add new comment