summaryrefslogtreecommitdiff
path: root/src/hal/classicladder/classicladder.c
blob: ab7fdb1338f80da59882a8eaae7c322adf343cf1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/* Classic Ladder Project */
/* Copyright (C) 2001-2007 Marc Le Douarain */
/* http://www.multimania.com/mavati/classicladder */
/* http://www.sourceforge.net/projects/classicladder */
/* February 2001 */
/* ------------------------------ */
/* The main for Linux Application */
/* ------------------------------ */
/* This library is free software; you can redistribute it and/or */
/* modify it under the terms of the GNU Lesser General Public */
/* License as published by the Free Software Foundation; either */
/* version 2.1 of the License, or (at your option) any later version. */

/* This library is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU */
/* Lesser General Public License for more details. */

/* You should have received a copy of the GNU Lesser General Public */
/* License along with this library; if not, write to the Free Software */
/* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

// This is an adaption of classicladder V7.124 For EMC
// most of this file is very different from the original
// it uses RTAPI realtime code and HAL code for memory allocation and input/output.
// this adaptation was started Jan 2008 by Chris Morley  

#ifdef MODULE
#include <linux/string.h>
#else
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#endif

#include "hal.h"
#include "classicladder.h"
#include "global.h"
#include "files_project.h"
#include "calc.h"
#include "vars_access.h"
#include "manager.h"
#include "calc_sequential.h"
#include "files_sequential.h"
#include "config.h"
// #include "hardware.h"
#include "socket_server.h"
#include "socket_modbus_master.h"

#if !defined( MODULE )
#include "classicladder_gtk.h"
#include "manager_gtk.h"
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <signal.h>
#include <getopt.h>
#ifdef __WIN32__
#include <windows.h>
#else
#include <pthread.h>
#endif
#ifdef __XENO__
#include <sys/mman.h>
#endif
#endif

#ifdef GTK_INTERFACE
#include <gtk/gtk.h>
#endif

#ifdef MAT_CONNECTION
#include "../../lib/plc.h"
#endif

int cl_remote;
int nogui = 0,modmaster=0,modslave=0,pathswitch=0;;
int ModbusServerPort = 9502; // Standard "502" requires root privileges...
int CyclicThreadRunning = 0;
char  *NewPath;

void ClassicLadderEndOfAppli( void )
{
	CyclicThreadRunning = 0;
	if(modmaster)  {   CloseSocketModbusMaster( );   }
	if(modslave)   {   CloseSocketServer( );   }

}

void display_help (void)
{
	printf("\nClassicLadder v"CL_RELEASE_VER_STRING"\n"CL_RELEASE_DATE_STRING"\n"
	       "Copyright (C) 2001-2004 Marc Le Douarain\nmavati@club-internet.fr\n"
	       "Adapted to EMC\n"
			"\n"
	       "ClassicLadder comes with NO WARRANTY\n"
	       "to the extent permitted by law.\n"
	       "\n"
	       "You may redistribute copies of ClassicLadder\n"
	       "under the terms of the GNU Lesser General Public Licence.\n"
	       "See the file `lesserGPL.txt' for more information.\n");	
	
    printf("This version of Classicladder is adapted for use with EMC and HAL\n"
	       "\nUsage: classicladder [OPTIONS] [PATH]\n"
	       "eg: loadusr -w classicladder  ladtest.clp\n"
	       "eg: loadusr -w classicladder  --nogui ladtest.clp\n"
	       "eg: loadusr -w classicladder  --modmaster ladtest.clp\n"
	       "\n"
	       "   --nogui            do not create a GUI, only load a configuration\n"
	       "   --config=filename  initilize modbus master I/O & load config file-( deprecated- use --modmaster)\n" 
	       "   --modmaster        initilize modbus master I/O ( modbus config is loaded with other objects )\n" 
	       "   --modslave         initilize modbus slave I/O (TCP only- B and W variables accesable\n"
	       "   --modbus_port=portnumber  used for modbus slave using TCP ( ethernet )\n"
	       "   --debug            sets the RTAPI debuglevel for printing debug messages\n"
	       "Please also note that the classicladder realtime module must be loaded first\n"
	       "eg: loadrt classicladder_rt    for default number of ladder objects\n"  
			    );
	hal_exit(compId); // add for emc
	exit(0);
}



void process_options (int argc, char *argv[])
{
	int error = 0;
	for (;;)
	{
		int option_index = 0;
		static const char *short_options = "c:";
		static const struct option long_options[] = {
			{"nogui", no_argument, 0, 'n'},
			{"config", required_argument, 0, 'c'},
			{"modmaster",no_argument,0,'m'},
			{"modslave",no_argument,0,'s'},
			{"debug",no_argument,0,'d'},
			{"modbus_port", required_argument, 0, 'p'},
			{"newpath", required_argument, 0, 'f'},
			{0, 0, 0, 0},
		};

		int c = getopt_long(argc, argv, short_options,
				    long_options, &option_index);
		if (c == EOF)
			break;

		switch (c)
		{
			
			case 'n':
				nogui = 1;
				break;
			case 'c':
				read_config (optarg);
				modmaster=1;
				break;
			case 'm':
				modmaster=1;
				break;
			case 's':
				modslave=1;
				break; 
			case 'd':
				rtapi_set_msg_level(RTAPI_MSG_ALL);
				break;
			case 'p':
				ModbusServerPort = atoi( optarg );
				break;
			case 'f':
				NewPath = ( optarg );
				pathswitch=1;
				break;
			case '?':
				error = 1;
				break;
		}
	}

	if (error)
		display_help ();

	if ((argc - optind) != 0)
		VerifyDirectorySelected (argv[optind]);
}
//for EMC: do_exit
static void do_exit(int unused) {
		hal_exit(compId);
		printf("ERROR CLASSICLADDER-   Error intializing classicladder user module.\n");
		exit(0);
}
void DoPauseMilliSecs( int Time )
{
	struct timespec time;
	time.tv_sec = 0;
	if (Time>=1000) 
		       {
			time.tv_sec=Time/1000;
			Time=Time%1000;
		       }
	time.tv_nsec = Time*1000000;
	nanosleep( &time, NULL );
	//usleep( Time*1000 );
}
void StopRunIfRunning( void )
{
	if (InfosGene->LadderState==STATE_RUN)
	{
		InfosGene->LadderStoppedToRunBack = TRUE;
		InfosGene->LadderState = STATE_STOP;
		while( InfosGene->UnderCalculationPleaseWait==TRUE )
		{
			DoPauseMilliSecs( 100 );
		}
	}
}
void RunBackIfStopped( void )
{
	if ( InfosGene->LadderStoppedToRunBack )
	{
		InfosGene->LadderState = STATE_RUN;
		InfosGene->LadderStoppedToRunBack = FALSE;
	}
}
// after processing options and intiallising HAL, MODBUS and registering shared memory
// the main function is divided into  NOGUI true or NOGUI FALSE
// The difference between them is mostly about checking if a program has already been loaded (only when GUI is to be shown)
// if rungs are used and a ladder program was not specified on the command line
// then a program is already in memory so we dont re initallize memory we just start the GUI

int main( int   argc, char *argv[] )
{
	int used=0, NumRung;
	static int old_level ;
	old_level = rtapi_get_msg_level();
	compId=hal_init("classicladder"); //emc
	if (compId<0) return -1; //emc
	signal(SIGTERM,do_exit); //emc
	InitModbusMasterBeforeReadConf( );
	if (ClassicLadder_AllocAll())
	{
		char ProjectLoadedOk=TRUE;		
		process_options (argc, argv);
		if (nogui==TRUE)
		{
			rtapi_print("INFO CLASSICLADDER-   No ladder GUI requested-Realtime runs till HAL closes.\n");
			ClassicLadder_InitAllDatas( );
			ProjectLoadedOk = LoadProjectFiles( InfosGene->CurrentProjectFileName  );
			if (pathswitch){   strcpy( InfosGene->CurrentProjectFileName, NewPath );   }
			InfosGene->LadderState = STATE_RUN;
			ClassicLadder_FreeAll(TRUE);
			hal_ready(compId);
			hal_exit(compId);	
			return 0; 
		} else {	
						
				for(NumRung=0;NumRung<NBR_RUNGS;NumRung++)   {   if ( RungArray[NumRung].Used ) used++;   }
				if((used==0) || ( (argc - optind) != 0) )
					    {	
						ClassicLadder_InitAllDatas( );
						ProjectLoadedOk = LoadProjectFiles( InfosGene->CurrentProjectFileName );
						InitGtkWindows( argc, argv );
						UpdateAllGtkWindows();
						if (pathswitch){   strcpy( InfosGene->CurrentProjectFileName, NewPath );   }
						UpdateWindowTitleWithProjectName( );
						MessageInStatusBar( ProjectLoadedOk?"Project loaded and running":"Project failed to load...");
						if (!ProjectLoadedOk) 
						{  
							   ClassicLadder_InitAllDatas( );   
							   if (modmaster) {    PrepareModbusMaster( );    }
						}
					    }else{
							   InitGtkWindows( argc, argv );
							   UpdateAllGtkWindows();
							   if (pathswitch){   strcpy( InfosGene->CurrentProjectFileName, NewPath );   }
							   UpdateWindowTitleWithProjectName( );
							   MessageInStatusBar("GUI reloaded with existing ladder program");
							   if (modmaster) {    PrepareModbusMaster( );    }
							} 
							
				if (modslave)         {   InitSocketServer( 0/*UseUdpMode*/, ModbusServerPort/*PortNbr*/);  }
				InfosGene->LadderState = STATE_RUN;
				hal_ready(compId);
				gtk_main();
				rtapi_print("INFO CLASSICLADDER-   Ladder GUI closed. Realtime runs till HAL closes\n");
				ClassicLadder_FreeAll(TRUE);
				hal_exit(compId);
				return 0;
			}		
	}
	 rtapi_print("ERROR CLASSICLADDER-   Ladder memory allocation error\n");
	ClassicLadder_FreeAll(TRUE);
	rtapi_set_msg_level(old_level);
	hal_exit(compId);		
	return 0;
}