CODESYS - the IEC 61131-3 automation software

Welcome to the official CODESYS Forum by 3S-Smart Software Solutions GmbH | A member of the CODESYS Group
Deutsche Version English version russian version 
It is currently Sun Oct 20, 2019 3:29 pm

All times are UTC+01:00




Post new topic  Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Tue Jul 10, 2012 2:11 pm 
Offline

Joined: Sat Jul 07, 2012 9:59 pm
Posts: 2
Hello,

I am currently working on a TCP/IP implementation in CoDeSys V3, whereas the IEC61131-3 program has to be the server. For whatever reason, it is not possible to receive any data from the point of view of TCP client, even though a working connection could be established.
I am using the NetBaseServices.

Please refer to the following code:

Code:
---HEADER---
PROGRAM TCP_Client
VAR
   bFirst: BOOL := TRUE;
   My_Data: ARRAY[0..4] OF BYTE := [0,1,2,3,4];
 
   NBS_IP_Adress : NBS.IP_ADDR;
   
   TCPClient : NBS.TCP_Client;
   TCPClient_xDone : BOOL;
   TCPClient_xBusy : BOOL;
   TCPClient_eError : NBS.ERROR;
   TCPClient_xActive : BOOL;
   TCPClient_hConn : NBS.CAA.HANDLE;
   
   TCPServer : NBS.TCP_Server;
   TCPServer_xDone : BOOL;
   TCPServer_xBusy : BOOL;
   TCPServer_eError : NBS.ERROR;
   TCPServer_xError : BOOL;
   TCPServer_hServer : NBS.CAA.HANDLE;
   
   TCPConnection : NBS.TCP_Connection;
   TCPConnection_xDone: BOOL;
   TCPConnection_eError : NBS.ERROR;
   TCPConnection_xActive : BOOL;
   
   TCPWrite : NBS.TCP_Write;
   TCPWrite_xDone : BOOL;
   TCPWrite_xBusy : BOOL;
   TCPWrite_eError : NBS.ERROR;
   TCPWrite_xError : BOOL;
END_VAR

---PROGRAM---
IF bFirst THEN
   bFirst := FALSE;
   
   // CoDeSys Automation Alliance (CAA) Interfaces - NetBaseServices (NBS)
   NBS_IP_Adress.sAddr := '192.168.178.39';
   TCPServer(
      xEnable := TRUE,
      ipAddr := NBS_IP_Adress,
      uiPort := 61002,
      xDone => TCPServer_xDone,
      xBusy => TCPServer_xBusy,
      eError => TCPServer_eError,
      xError => TCPServer_xError,
   );

END_IF

My_Data[0] := My_Data[0]+1;
My_Data[1] := My_Data[1]+1;
My_Data[2] := My_Data[2]+1;
My_Data[3] := My_Data[3]+1;
My_Data[4] := My_Data[4]+1;

TCPConnection(
   xEnable := TRUE,
   hServer := TCPServer.hServer,
   xDone => TCPConnection_xDone,
   eError => TCPConnection_eError,
   xActive => TCPConnection_xActive      
);
IF TCPConnection.xDone THEN
   TCPWrite(
      xExecute := TRUE,
      hConnection := TCPConnection.hConnection,
      pData := ADR(My_Data),
      szSize := SIZEOF(My_Data),
      xDone => TCPWrite_xDone,
      xBusy => TCPWrite_xBusy,
      xError => TCPWrite_xError,
      eError => TCPWrite_eError
   );   
END_IF


Do you have an idea what is wrong with my implementation?
Thanks in advance for your kind support.

Regards,
Robert


Top
   
PostPosted: Wed Jul 11, 2012 6:21 am 
Offline
Frequent User
Frequent User
User avatar

Joined: Tue Jun 06, 2006 9:03 am
Posts: 410
I'm not 100% sure, but here some things that might help:
- Always call the TCPServer FB (not just the first scan) with xEnable TRUE.
- Enable the TCPConnection when the TCPServer handle is not invalid or 0.
- Execute the TCPWrite FB when the TCPConnection is Active (not when it is done)
- An Execute input of the FB is a trigger to start it, therefore the data is only send once in your application. Replace the TRUE for a puls generator or something similar.


Top
   
PostPosted: Wed Jul 11, 2012 4:53 pm 
Offline

Joined: Sat Jul 07, 2012 9:59 pm
Posts: 2
Hey TimvH,

"Always call the TCPServer FB (not just the first scan) with xEnable TRUE" helped me out!

Thank you for your help!


Top
   
PostPosted: Sun Jul 29, 2012 7:46 pm 
Offline
User avatar

Joined: Tue Aug 30, 2011 1:21 am
Posts: 96
I would recommend using the OsCat Network Library it has been tested and validated. You can use the IP_CONTROL FB's to implement you own TCP SERVER.

You have to be signed in to download. This has been updated for CoDeSys V3.

http://www.oscat.de/community/index.php ... 784.0.html

_________________
The Original SoMachine Ninja


Top
   
PostPosted: Thu Dec 20, 2012 10:34 am 
Offline

Joined: Thu Nov 29, 2012 9:58 am
Posts: 2
Hello Robinston,

I tried your code (and I ran TCPServer always, as it was recommended, not only in the first run), but in my TCPWrite FB, I get "Invalid Handle", even if I try the other recommended issues, posted by TimvH.

Did you only deleted the IF bfirst THEN-Loop - or did you some other changes in your code until it worked?

Thanks in advance for your kind help.

Best Regards
Ruwen


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 5 posts ] 

All times are UTC+01:00


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Limited