examples/CANOpenShell/CANOpenShell.c
author 'Gr?gory Tr?lat <gregory.trelat@lolitech.fr>'
Wed, 28 Oct 2009 13:49:00 +0100
changeset 613 e9fce24923fc
parent 605 f91ee161b3a1
child 664 a03f0aa7d219
permissions -rwxr-xr-x
changes merged
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
     1
/*
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
     2
This file is part of CanFestival, a library implementing CanOpen Stack.
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
     3
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
     4
Copyright (C): Edouard TISSERANT and Francis DUPIN
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
     5
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
     6
See COPYING file for copyrights details.
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
     7
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
     8
This library is free software; you can redistribute it and/or
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
     9
modify it under the terms of the GNU Lesser General Public
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    10
License as published by the Free Software Foundation; either
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    11
version 2.1 of the License, or (at your option) any later version.
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    12
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    13
This library is distributed in the hope that it will be useful,
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    14
but WITHOUT ANY WARRANTY; without even the implied warranty of
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    16
Lesser General Public License for more details.
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    17
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    18
You should have received a copy of the GNU Lesser General Public
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    19
License along with this library; if not, write to the Free Software
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    20
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    21
*/
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    22
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    23
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    24
#if defined(WIN32) && !defined(__CYGWIN__)
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    25
	#include <windows.h>
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    26
	#define CLEARSCREEN "cls"
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    27
	#define SLEEP(time) Sleep(time * 1000)
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    28
#else
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    29
	#include <unistd.h>
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    30
	#include <stdio.h>
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    31
	#include <string.h>
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    32
	#include <stdlib.h>
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    33
	#define CLEARSCREEN "clear"
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    34
	#define SLEEP(time) sleep(time)
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    35
#endif
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    36
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    37
//****************************************************************************
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    38
// INCLUDES
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    39
#include "canfestival.h"
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    40
#include "CANOpenShell.h"
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    41
#include "CANOpenShellMasterOD.h"
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    42
#include "CANOpenShellSlaveOD.h"
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    43
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    44
//****************************************************************************
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    45
// DEFINES
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    46
#define MAX_NODES 127
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    47
#define cst_str4(c1, c2, c3, c4) ((((unsigned int)0 | \
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    48
                                    (char)c4 << 8) | \
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    49
                                   (char)c3) << 8 | \
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    50
                                  (char)c2) << 8 | \
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    51
                                 (char)c1
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    52
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    53
#define INIT_ERR 2
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    54
#define QUIT 1
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    55
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    56
//****************************************************************************
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    57
// GLOBALS
605
f91ee161b3a1 -add LeaveMutex to avoid canopenshell deadlock when call NodeInit
greg
parents: 568
diff changeset
    58
char BoardBusName[31];
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    59
char BoardBaudRate[5];
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    60
s_BOARD Board = {BoardBusName, BoardBaudRate};
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
    61
CO_Data* CANOpenShellOD_Data;
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    62
char LibraryPath[512];
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    63
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    64
/* Sleep for n seconds */
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    65
void SleepFunction(int second)
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    66
{
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    67
	SLEEP(second);
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    68
}
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    69
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    70
/* Ask a slave node to go in operational mode */
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
    71
void StartNode(UNS8 nodeid)
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
    72
{
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
    73
	masterSendNMTstateChange(CANOpenShellOD_Data, nodeid, NMT_Start_Node);
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    74
}
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    75
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    76
/* Ask a slave node to go in pre-operational mode */
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
    77
void StopNode(UNS8 nodeid)
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
    78
{
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
    79
	masterSendNMTstateChange(CANOpenShellOD_Data, nodeid, NMT_Stop_Node);
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    80
}
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    81
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    82
/* Ask a slave node to reset */
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
    83
void ResetNode(UNS8 nodeid)
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
    84
{
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
    85
	masterSendNMTstateChange(CANOpenShellOD_Data, nodeid, NMT_Reset_Node);
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    86
}
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    87
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    88
/* Reset all nodes on the network and print message when boot-up*/
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
    89
void DiscoverNodes()
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    90
{
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    91
	printf("Wait for Slave nodes bootup...\n\n");
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
    92
	ResetNode(0x00);
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
    93
}
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
    94
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
    95
int get_info_step = 0;
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    96
/* Callback function that check the read SDO demand */
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    97
void CheckReadInfoSDO(CO_Data* d, UNS8 nodeid)
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    98
{
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    99
	UNS32 abortCode;
568
84fb13d1b3d2 fix some minor bugs
greg
parents: 559
diff changeset
   100
	UNS32 data=0;
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   101
	UNS32 size=64;
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   102
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   103
	if(getReadResultNetworkDict(CANOpenShellOD_Data, nodeid, &data, &size, &abortCode) != SDO_FINISHED)
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   104
		printf("Master : Failed in getting information for slave %2.2x, AbortCode :%4.4x \n", nodeid, abortCode);
568
84fb13d1b3d2 fix some minor bugs
greg
parents: 559
diff changeset
   105
	else
84fb13d1b3d2 fix some minor bugs
greg
parents: 559
diff changeset
   106
	{
84fb13d1b3d2 fix some minor bugs
greg
parents: 559
diff changeset
   107
		/* Display data received */
84fb13d1b3d2 fix some minor bugs
greg
parents: 559
diff changeset
   108
		switch(get_info_step)
84fb13d1b3d2 fix some minor bugs
greg
parents: 559
diff changeset
   109
		{
84fb13d1b3d2 fix some minor bugs
greg
parents: 559
diff changeset
   110
			case 1:
84fb13d1b3d2 fix some minor bugs
greg
parents: 559
diff changeset
   111
					printf("Device type     : %x\n", data);
84fb13d1b3d2 fix some minor bugs
greg
parents: 559
diff changeset
   112
					break;
84fb13d1b3d2 fix some minor bugs
greg
parents: 559
diff changeset
   113
			case 2:
84fb13d1b3d2 fix some minor bugs
greg
parents: 559
diff changeset
   114
					printf("Vendor ID       : %x\n", data);
84fb13d1b3d2 fix some minor bugs
greg
parents: 559
diff changeset
   115
					break;
84fb13d1b3d2 fix some minor bugs
greg
parents: 559
diff changeset
   116
			case 3:
84fb13d1b3d2 fix some minor bugs
greg
parents: 559
diff changeset
   117
					printf("Product Code    : %x\n", data);
84fb13d1b3d2 fix some minor bugs
greg
parents: 559
diff changeset
   118
					break;
84fb13d1b3d2 fix some minor bugs
greg
parents: 559
diff changeset
   119
			case 4:
84fb13d1b3d2 fix some minor bugs
greg
parents: 559
diff changeset
   120
					printf("Revision Number : %x\n", data);
84fb13d1b3d2 fix some minor bugs
greg
parents: 559
diff changeset
   121
					break;
84fb13d1b3d2 fix some minor bugs
greg
parents: 559
diff changeset
   122
		}
84fb13d1b3d2 fix some minor bugs
greg
parents: 559
diff changeset
   123
	}
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   124
	/* Finalize last SDO transfer with this node */
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   125
	closeSDOtransfer(CANOpenShellOD_Data, nodeid, SDO_CLIENT);
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   126
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   127
	GetSlaveNodeInfo(nodeid);
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   128
}
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   129
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   130
/* Retrieve node informations located at index 0x1000 (Device Type) and 0x1018 (Identity) */
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   131
void GetSlaveNodeInfo(UNS8 nodeid)
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   132
{
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   133
		switch(++get_info_step)
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   134
		{
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   135
			case 1: /* Get device type */
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   136
				printf("##################################\n");
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   137
				printf("#### Informations for node %x ####\n", nodeid);
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   138
				printf("##################################\n");
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   139
				readNetworkDictCallback(CANOpenShellOD_Data, nodeid, 0x1000, 0x00, 0, CheckReadInfoSDO);
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   140
				break;
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   141
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   142
			case 2: /* Get Vendor ID */
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   143
				readNetworkDictCallback(CANOpenShellOD_Data, nodeid, 0x1018, 0x01, 0, CheckReadInfoSDO);
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   144
				break;
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   145
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   146
			case 3: /* Get Product Code */
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   147
				readNetworkDictCallback(CANOpenShellOD_Data, nodeid, 0x1018, 0x02, 0, CheckReadInfoSDO);
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   148
				break;
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   149
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   150
			case 4: /* Get Revision Number */
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   151
				readNetworkDictCallback(CANOpenShellOD_Data, nodeid, 0x1018, 0x03, 0, CheckReadInfoSDO);
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   152
				break;
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   153
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   154
			case 5: /* Print node info */
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   155
				get_info_step = 0;
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   156
		}
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   157
}
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   158
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   159
/* Callback function that check the read SDO demand */
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   160
void CheckReadSDO(CO_Data* d, UNS8 nodeid)
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   161
{
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   162
	UNS32 abortCode;
568
84fb13d1b3d2 fix some minor bugs
greg
parents: 559
diff changeset
   163
	UNS32 data=0;
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   164
	UNS32 size=64;
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   165
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   166
	if(getReadResultNetworkDict(CANOpenShellOD_Data, nodeid, &data, &size, &abortCode) != SDO_FINISHED)
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   167
		printf("\nResult : Failed in getting information for slave %2.2x, AbortCode :%4.4x \n", nodeid, abortCode);
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   168
	else
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   169
		printf("\nResult : %x\n", data);
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   170
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   171
	/* Finalize last SDO transfer with this node */
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   172
	closeSDOtransfer(CANOpenShellOD_Data, nodeid, SDO_CLIENT);
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   173
}
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   174
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   175
/* Read a slave node object dictionary entry */
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   176
void ReadDeviceEntry(char* sdo)
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   177
{
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   178
	int ret=0;
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   179
	int nodeid;
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   180
	int index;
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   181
	int subindex;
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   182
	int datatype = 0;
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   183
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   184
	ret = sscanf(sdo, "rsdo#%2x,%4x,%2x", &nodeid, &index, &subindex);
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   185
	if (ret == 3)
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   186
	{
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   187
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   188
		printf("##################################\n");
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   189
		printf("#### Read SDO                 ####\n");
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   190
		printf("##################################\n");
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   191
		printf("NodeId   : %2.2x\n", nodeid);
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   192
		printf("Index    : %4.4x\n", index);
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   193
		printf("SubIndex : %2.2x\n", subindex);
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   194
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   195
		readNetworkDictCallback(CANOpenShellOD_Data, (UNS8)nodeid, (UNS16)index, (UNS8)subindex, (UNS8)datatype, CheckReadSDO);
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   196
	}
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   197
	else
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   198
		printf("Wrong command  : %s\n", sdo);
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   199
}
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   200
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   201
/* Callback function that check the write SDO demand */
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   202
void CheckWriteSDO(CO_Data* d, UNS8 nodeid)
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   203
{
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   204
	UNS32 abortCode;
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   205
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   206
	if(getWriteResultNetworkDict(CANOpenShellOD_Data, nodeid, &abortCode) != SDO_FINISHED)
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   207
		printf("\nResult : Failed in getting information for slave %2.2x, AbortCode :%4.4x \n", nodeid, abortCode);
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   208
	else
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   209
		printf("\nSend data OK\n");
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   210
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   211
	/* Finalize last SDO transfer with this node */
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   212
	closeSDOtransfer(CANOpenShellOD_Data, nodeid, SDO_CLIENT);
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   213
}
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   214
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   215
/* Write a slave node object dictionnary entry */
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   216
void WriteDeviceEntry(char* sdo)
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   217
{
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   218
	int ret=0;
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   219
	int nodeid;
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   220
	int index;
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   221
	int subindex;
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   222
	int size;
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   223
	int data;
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   224
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   225
	ret = sscanf(sdo, "wsdo#%2x,%4x,%2x,%2x,%x", &nodeid , &index, &subindex, &size, &data);
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   226
	if (ret == 5)
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   227
	{
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   228
		printf("##################################\n");
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   229
		printf("#### Write SDO                ####\n");
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   230
		printf("##################################\n");
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   231
		printf("NodeId   : %2.2x\n", nodeid);
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   232
		printf("Index    : %4.4x\n", index);
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   233
		printf("SubIndex : %2.2x\n", subindex);
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   234
		printf("Size     : %2.2x\n", size);
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   235
		printf("Data     : %x\n", data);
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   236
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   237
		writeNetworkDictCallBack(CANOpenShellOD_Data, nodeid, index, subindex, size, 0, &data, CheckWriteSDO);
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   238
	}
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   239
	else
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   240
		printf("Wrong command  : %s\n", sdo);
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   241
}
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   242
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   243
void CANOpenShellOD_post_SlaveBootup(CO_Data* d, UNS8 nodeid)
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   244
{
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   245
	printf("Slave %x boot up\n", nodeid);
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   246
}
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   247
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   248
/***************************  CALLBACK FUNCTIONS  *****************************************/
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   249
void CANOpenShellOD_initialisation(CO_Data* d)
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   250
{
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   251
	printf("Node_initialisation\n");
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   252
}
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   253
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   254
void CANOpenShellOD_preOperational(CO_Data* d)
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   255
{
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   256
	printf("Node_preOperational\n");
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   257
}
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   258
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   259
void CANOpenShellOD_operational(CO_Data* d)
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   260
{
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   261
	printf("Node_operational\n");
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   262
}
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   263
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   264
void CANOpenShellOD_stopped(CO_Data* d)
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   265
{
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   266
	printf("Node_stopped\n");
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   267
}
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   268
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   269
void CANOpenShellOD_post_sync(CO_Data* d)
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   270
{
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   271
	//printf("Master_post_sync\n");
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   272
}
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   273
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   274
void CANOpenShellOD_post_TPDO(CO_Data* d)
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   275
{
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   276
	//printf("Master_post_TPDO\n");
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   277
}
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   278
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   279
/***************************  INITIALISATION  **********************************/
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   280
void Init(CO_Data* d, UNS32 id)
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   281
{
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   282
	if(Board.baudrate)
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   283
	{
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   284
		/* Init node state*/
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   285
		setState(CANOpenShellOD_Data, Initialisation);
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   286
	}
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   287
}
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   288
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   289
/***************************  CLEANUP  *****************************************/
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   290
void Exit(CO_Data* d, UNS32 nodeid)
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   291
{
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   292
	if(strcmp(Board.baudrate, "none"))
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   293
	{
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   294
		/* Reset all nodes on the network */
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   295
		masterSendNMTstateChange(CANOpenShellOD_Data, 0 , NMT_Reset_Node);
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   296
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   297
		/* Stop master */
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   298
		setState(CANOpenShellOD_Data, Stopped);
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   299
	}
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   300
}
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   301
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   302
int NodeInit(int NodeID, int NodeType)
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   303
{
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   304
	if(NodeType)
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   305
		CANOpenShellOD_Data = &CANOpenShellMasterOD_Data;
568
84fb13d1b3d2 fix some minor bugs
greg
parents: 559
diff changeset
   306
	else
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   307
		CANOpenShellOD_Data = &CANOpenShellSlaveOD_Data;
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   308
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   309
	/* Load can library */
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   310
	LoadCanDriver(LibraryPath);
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   311
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   312
	/* Define callback functions */
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   313
	CANOpenShellOD_Data->initialisation = CANOpenShellOD_initialisation;
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   314
	CANOpenShellOD_Data->preOperational = CANOpenShellOD_preOperational;
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   315
	CANOpenShellOD_Data->operational = CANOpenShellOD_operational;
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   316
	CANOpenShellOD_Data->stopped = CANOpenShellOD_stopped;
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   317
	CANOpenShellOD_Data->post_sync = CANOpenShellOD_post_sync;
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   318
	CANOpenShellOD_Data->post_TPDO = CANOpenShellOD_post_TPDO;
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   319
	CANOpenShellOD_Data->post_SlaveBootup=CANOpenShellOD_post_SlaveBootup;
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   320
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   321
	/* Open the Peak CANOpen device */
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   322
	if(!canOpen(&Board,CANOpenShellOD_Data)) return INIT_ERR;
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   323
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   324
	/* Defining the node Id */
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   325
	setNodeId(CANOpenShellOD_Data, NodeID);
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   326
	/* Start Timer thread */
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   327
	StartTimerLoop(&Init);
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   328
	return 0;
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   329
}
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   330
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   331
void help_menu(void)
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   332
{
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   333
	printf("   MANDATORY COMMAND (must be the first command):\n");
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   334
	printf("     load#CanLibraryPath,channel,baudrate,nodeid,type (0:slave, 1:master)\n");
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   335
	printf("\n");
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   336
	printf("   NETWORK: (if nodeid=0x00 : broadcast)\n");
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   337
	printf("     ssta#nodeid : Start a node\n");
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   338
	printf("     ssto#nodeid : Stop a node\n");
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   339
	printf("     srst#nodeid : Reset a node\n");
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   340
	printf("     scan : Reset all nodes and print message when bootup\n");
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   341
	printf("     wait#seconds : Sleep for n seconds\n");
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   342
	printf("\n");
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   343
	printf("   SDO: (size in bytes)\n");
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   344
	printf("     info#nodeid\n");
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   345
	printf("     rsdo#nodeid,index,subindex : read sdo\n");
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   346
	printf("        ex : rsdo#42,1018,01\n");
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   347
	printf("     wsdo#nodeid,index,subindex,size,data : write sdo\n");
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   348
	printf("        ex : wsdo#42,6200,01,01,FF\n");
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   349
	printf("\n");
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   350
	printf("   Note: All numbers are hex\n");
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   351
	printf("\n");
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   352
	printf("     help : Display this menu\n");
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   353
	printf("     quit : Quit application\n");
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   354
	printf("\n");
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   355
	printf("\n");
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   356
}
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   357
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   358
int ExtractNodeId(char *command) {
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   359
	int nodeid;
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   360
	sscanf(command, "%2x", &nodeid);
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   361
	return nodeid;
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   362
}
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   363
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   364
int ProcessCommand(char* command)
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   365
{
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   366
	int ret = 0;
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   367
	int sec = 0;
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   368
	int NodeID;
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   369
	int NodeType;
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   370
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   371
	EnterMutex();
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   372
	switch(cst_str4(command[0], command[1], command[2], command[3]))
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   373
	{
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   374
		case cst_str4('h', 'e', 'l', 'p') : /* Display Help*/
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   375
					help_menu();
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   376
					break;
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   377
		case cst_str4('s', 's', 't', 'a') : /* Slave Start*/
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   378
					StartNode(ExtractNodeId(command + 5));
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   379
					break;
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   380
		case cst_str4('s', 's', 't', 'o') : /* Slave Stop */
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   381
					StopNode(ExtractNodeId(command + 5));
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   382
					break;
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   383
		case cst_str4('s', 'r', 's', 't') : /* Slave Reset */
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   384
					ResetNode(ExtractNodeId(command + 5));
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   385
					break;
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   386
		case cst_str4('i', 'n', 'f', 'o') : /* Retrieve node informations */
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   387
					GetSlaveNodeInfo(ExtractNodeId(command + 5));
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   388
					break;
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   389
		case cst_str4('r', 's', 'd', 'o') : /* Read device entry */
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   390
					ReadDeviceEntry(command);
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   391
					break;
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   392
		case cst_str4('w', 's', 'd', 'o') : /* Write device entry */
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   393
					WriteDeviceEntry(command);
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   394
					break;
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   395
		case cst_str4('s', 'c', 'a', 'n') : /* Display master node state */
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   396
					DiscoverNodes();
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   397
					break;
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   398
		case cst_str4('w', 'a', 'i', 't') : /* Display master node state */
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   399
					ret = sscanf(command, "wait#%d", &sec);
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   400
					if(ret == 1) {
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   401
						LeaveMutex();
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   402
						SleepFunction(sec);
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   403
						return 0;
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   404
					}
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   405
					break;
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   406
		case cst_str4('q', 'u', 'i', 't') : /* Quit application */
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   407
					LeaveMutex();
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   408
					return QUIT;
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   409
		case cst_str4('l', 'o', 'a', 'd') : /* Library Interface*/
605
f91ee161b3a1 -add LeaveMutex to avoid canopenshell deadlock when call NodeInit
greg
parents: 568
diff changeset
   410
					ret = sscanf(command, "load#%100[^,],%30[^,],%4[^,],%d,%d",
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   411
							LibraryPath,
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   412
							BoardBusName,
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   413
							BoardBaudRate,
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   414
							&NodeID,
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   415
							&NodeType);
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   416
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   417
					if(ret == 5)
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   418
					{
605
f91ee161b3a1 -add LeaveMutex to avoid canopenshell deadlock when call NodeInit
greg
parents: 568
diff changeset
   419
						LeaveMutex();
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   420
						ret = NodeInit(NodeID, NodeType);
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   421
						return ret;
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   422
					}
605
f91ee161b3a1 -add LeaveMutex to avoid canopenshell deadlock when call NodeInit
greg
parents: 568
diff changeset
   423
					else
f91ee161b3a1 -add LeaveMutex to avoid canopenshell deadlock when call NodeInit
greg
parents: 568
diff changeset
   424
					{
f91ee161b3a1 -add LeaveMutex to avoid canopenshell deadlock when call NodeInit
greg
parents: 568
diff changeset
   425
						printf("Invalid load parameters\n");
f91ee161b3a1 -add LeaveMutex to avoid canopenshell deadlock when call NodeInit
greg
parents: 568
diff changeset
   426
					}
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   427
					break;
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   428
		default :
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   429
					help_menu();
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   430
	}
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   431
	LeaveMutex();
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   432
	return 0;
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   433
}
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   434
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   435
/****************************************************************************/
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   436
/***************************  MAIN  *****************************************/
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   437
/****************************************************************************/
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   438
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   439
int main(int argc, char** argv)
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   440
{
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   441
	extern char *optarg;
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   442
	char command[200];
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   443
	char* res;
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   444
	int ret=0;
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   445
	int sysret=0;
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   446
	int i=0;
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   447
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   448
	/* Print help and exit immediatly*/
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   449
	if(argc < 2)
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   450
	{
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   451
		help_menu();
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   452
		exit(1);
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   453
	}
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   454
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   455
	/* Init stack timer */
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   456
	TimerInit();
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   457
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   458
	/* Strip command-line*/
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   459
	for(i=1 ; i<argc ; i++)
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   460
	{
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   461
		if(ProcessCommand(argv[i]) == INIT_ERR) goto init_fail;
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   462
	}
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   463
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   464
	/* Enter in a loop to read stdin command until "quit" is called */
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   465
	while(ret != QUIT)
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   466
	{
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   467
		// wait on stdin for string command
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   468
		res = fgets(command, sizeof(command), stdin);
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   469
		sysret = system(CLEARSCREEN);
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   470
		ret = ProcessCommand(command);
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   471
		fflush(stdout);
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   472
	}
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   473
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   474
	printf("Finishing.\n");
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   475
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   476
	// Stop timer thread
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   477
	StopTimerLoop(&Exit);
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   478
568
84fb13d1b3d2 fix some minor bugs
greg
parents: 559
diff changeset
   479
	/* Close CAN board */
84fb13d1b3d2 fix some minor bugs
greg
parents: 559
diff changeset
   480
	canClose(CANOpenShellOD_Data);
84fb13d1b3d2 fix some minor bugs
greg
parents: 559
diff changeset
   481
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   482
init_fail:
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   483
	TimerCleanup();
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   484
	return 0;
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   485
}
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   486