examples/CANOpenShell/CANOpenShell.c
author Edouard Tisserant
Wed, 06 Mar 2013 11:39:35 +0900
changeset 772 7cb6e71d6331
parent 751 d005211221c7
permissions -rwxr-xr-x
can_peak_linux link sequence modifed, thx to chenxi
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};
751
d005211221c7 Changed call to sleep function because it gets redefined when using RTAI_LXRT
mwildbolz
parents: 664
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
{
751
d005211221c7 Changed call to sleep function because it gets redefined when using RTAI_LXRT
mwildbolz
parents: 664
diff changeset
    67
#ifdef USE_RTAI
d005211221c7 Changed call to sleep function because it gets redefined when using RTAI_LXRT
mwildbolz
parents: 664
diff changeset
    68
	sleep(second);
d005211221c7 Changed call to sleep function because it gets redefined when using RTAI_LXRT
mwildbolz
parents: 664
diff changeset
    69
#else
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    70
	SLEEP(second);
751
d005211221c7 Changed call to sleep function because it gets redefined when using RTAI_LXRT
mwildbolz
parents: 664
diff changeset
    71
#endif
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    72
}
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    73
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    74
/* Ask a slave node to go in operational mode */
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
    75
void StartNode(UNS8 nodeid)
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
    76
{
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
    77
	masterSendNMTstateChange(CANOpenShellOD_Data, nodeid, NMT_Start_Node);
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    78
}
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    79
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    80
/* Ask a slave node to go in pre-operational mode */
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
    81
void StopNode(UNS8 nodeid)
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
    82
{
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
    83
	masterSendNMTstateChange(CANOpenShellOD_Data, nodeid, NMT_Stop_Node);
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    84
}
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    85
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    86
/* Ask a slave node to reset */
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
    87
void ResetNode(UNS8 nodeid)
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
    88
{
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
    89
	masterSendNMTstateChange(CANOpenShellOD_Data, nodeid, NMT_Reset_Node);
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    90
}
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    91
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    92
/* 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
    93
void DiscoverNodes()
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    94
{
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
    95
	printf("Wait for Slave nodes bootup...\n\n");
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
    96
	ResetNode(0x00);
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
    97
}
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
    98
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
    99
int get_info_step = 0;
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   100
/* Callback function that check the read SDO demand */
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   101
void CheckReadInfoSDO(CO_Data* d, UNS8 nodeid)
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   102
{
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   103
	UNS32 abortCode;
568
84fb13d1b3d2 fix some minor bugs
greg
parents: 559
diff changeset
   104
	UNS32 data=0;
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   105
	UNS32 size=64;
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   106
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   107
	if(getReadResultNetworkDict(CANOpenShellOD_Data, nodeid, &data, &size, &abortCode) != SDO_FINISHED)
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   108
		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
   109
	else
84fb13d1b3d2 fix some minor bugs
greg
parents: 559
diff changeset
   110
	{
84fb13d1b3d2 fix some minor bugs
greg
parents: 559
diff changeset
   111
		/* Display data received */
84fb13d1b3d2 fix some minor bugs
greg
parents: 559
diff changeset
   112
		switch(get_info_step)
84fb13d1b3d2 fix some minor bugs
greg
parents: 559
diff changeset
   113
		{
84fb13d1b3d2 fix some minor bugs
greg
parents: 559
diff changeset
   114
			case 1:
84fb13d1b3d2 fix some minor bugs
greg
parents: 559
diff changeset
   115
					printf("Device type     : %x\n", data);
84fb13d1b3d2 fix some minor bugs
greg
parents: 559
diff changeset
   116
					break;
84fb13d1b3d2 fix some minor bugs
greg
parents: 559
diff changeset
   117
			case 2:
84fb13d1b3d2 fix some minor bugs
greg
parents: 559
diff changeset
   118
					printf("Vendor ID       : %x\n", data);
84fb13d1b3d2 fix some minor bugs
greg
parents: 559
diff changeset
   119
					break;
84fb13d1b3d2 fix some minor bugs
greg
parents: 559
diff changeset
   120
			case 3:
84fb13d1b3d2 fix some minor bugs
greg
parents: 559
diff changeset
   121
					printf("Product Code    : %x\n", data);
84fb13d1b3d2 fix some minor bugs
greg
parents: 559
diff changeset
   122
					break;
84fb13d1b3d2 fix some minor bugs
greg
parents: 559
diff changeset
   123
			case 4:
84fb13d1b3d2 fix some minor bugs
greg
parents: 559
diff changeset
   124
					printf("Revision Number : %x\n", data);
84fb13d1b3d2 fix some minor bugs
greg
parents: 559
diff changeset
   125
					break;
84fb13d1b3d2 fix some minor bugs
greg
parents: 559
diff changeset
   126
		}
84fb13d1b3d2 fix some minor bugs
greg
parents: 559
diff changeset
   127
	}
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   128
	/* Finalize last SDO transfer with this node */
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   129
	closeSDOtransfer(CANOpenShellOD_Data, nodeid, SDO_CLIENT);
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   130
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   131
	GetSlaveNodeInfo(nodeid);
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   132
}
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   133
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   134
/* 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
   135
void GetSlaveNodeInfo(UNS8 nodeid)
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   136
{
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   137
		switch(++get_info_step)
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   138
		{
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   139
			case 1: /* Get device type */
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   140
				printf("##################################\n");
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   141
				printf("#### Informations for node %x ####\n", nodeid);
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   142
				printf("##################################\n");
664
a03f0aa7d219 CHANGE: SDO block mode added, without CRC support
fbeaulier
parents: 605
diff changeset
   143
				readNetworkDictCallback(CANOpenShellOD_Data, nodeid, 0x1000, 0x00, 0, CheckReadInfoSDO, 0);
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 2: /* Get Vendor ID */
664
a03f0aa7d219 CHANGE: SDO block mode added, without CRC support
fbeaulier
parents: 605
diff changeset
   147
				readNetworkDictCallback(CANOpenShellOD_Data, nodeid, 0x1018, 0x01, 0, CheckReadInfoSDO, 0);
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 3: /* Get Product Code */
664
a03f0aa7d219 CHANGE: SDO block mode added, without CRC support
fbeaulier
parents: 605
diff changeset
   151
				readNetworkDictCallback(CANOpenShellOD_Data, nodeid, 0x1018, 0x02, 0, CheckReadInfoSDO, 0);
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 4: /* Get Revision Number */
664
a03f0aa7d219 CHANGE: SDO block mode added, without CRC support
fbeaulier
parents: 605
diff changeset
   155
				readNetworkDictCallback(CANOpenShellOD_Data, nodeid, 0x1018, 0x03, 0, CheckReadInfoSDO, 0);
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   156
				break;
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   157
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   158
			case 5: /* Print node info */
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   159
				get_info_step = 0;
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   160
		}
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   161
}
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   162
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   163
/* Callback function that check the read SDO demand */
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   164
void CheckReadSDO(CO_Data* d, UNS8 nodeid)
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   165
{
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   166
	UNS32 abortCode;
568
84fb13d1b3d2 fix some minor bugs
greg
parents: 559
diff changeset
   167
	UNS32 data=0;
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   168
	UNS32 size=64;
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   169
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   170
	if(getReadResultNetworkDict(CANOpenShellOD_Data, nodeid, &data, &size, &abortCode) != SDO_FINISHED)
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   171
		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
   172
	else
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   173
		printf("\nResult : %x\n", data);
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   174
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   175
	/* Finalize last SDO transfer with this node */
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   176
	closeSDOtransfer(CANOpenShellOD_Data, nodeid, SDO_CLIENT);
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   177
}
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   178
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   179
/* Read a slave node object dictionary entry */
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   180
void ReadDeviceEntry(char* sdo)
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   181
{
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   182
	int ret=0;
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   183
	int nodeid;
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   184
	int index;
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   185
	int subindex;
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   186
	int datatype = 0;
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   187
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   188
	ret = sscanf(sdo, "rsdo#%2x,%4x,%2x", &nodeid, &index, &subindex);
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   189
	if (ret == 3)
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   190
	{
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   191
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   192
		printf("##################################\n");
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   193
		printf("#### Read SDO                 ####\n");
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   194
		printf("##################################\n");
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   195
		printf("NodeId   : %2.2x\n", nodeid);
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   196
		printf("Index    : %4.4x\n", index);
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   197
		printf("SubIndex : %2.2x\n", subindex);
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   198
664
a03f0aa7d219 CHANGE: SDO block mode added, without CRC support
fbeaulier
parents: 605
diff changeset
   199
		readNetworkDictCallback(CANOpenShellOD_Data, (UNS8)nodeid, (UNS16)index, (UNS8)subindex, (UNS8)datatype, CheckReadSDO, 0);
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   200
	}
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   201
	else
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   202
		printf("Wrong command  : %s\n", sdo);
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   203
}
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   204
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   205
/* Callback function that check the write SDO demand */
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   206
void CheckWriteSDO(CO_Data* d, UNS8 nodeid)
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   207
{
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   208
	UNS32 abortCode;
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   209
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   210
	if(getWriteResultNetworkDict(CANOpenShellOD_Data, nodeid, &abortCode) != SDO_FINISHED)
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   211
		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
   212
	else
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   213
		printf("\nSend data OK\n");
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   214
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   215
	/* Finalize last SDO transfer with this node */
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   216
	closeSDOtransfer(CANOpenShellOD_Data, nodeid, SDO_CLIENT);
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   217
}
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   218
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   219
/* Write a slave node object dictionnary entry */
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   220
void WriteDeviceEntry(char* sdo)
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   221
{
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   222
	int ret=0;
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   223
	int nodeid;
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   224
	int index;
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   225
	int subindex;
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   226
	int size;
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   227
	int data;
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   228
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   229
	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
   230
	if (ret == 5)
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   231
	{
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   232
		printf("##################################\n");
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   233
		printf("#### Write SDO                ####\n");
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   234
		printf("##################################\n");
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   235
		printf("NodeId   : %2.2x\n", nodeid);
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   236
		printf("Index    : %4.4x\n", index);
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   237
		printf("SubIndex : %2.2x\n", subindex);
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   238
		printf("Size     : %2.2x\n", size);
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   239
		printf("Data     : %x\n", data);
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   240
664
a03f0aa7d219 CHANGE: SDO block mode added, without CRC support
fbeaulier
parents: 605
diff changeset
   241
		writeNetworkDictCallBack(CANOpenShellOD_Data, nodeid, index, subindex, size, 0, &data, CheckWriteSDO, 0);
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   242
	}
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   243
	else
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   244
		printf("Wrong command  : %s\n", sdo);
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   245
}
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   246
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   247
void CANOpenShellOD_post_SlaveBootup(CO_Data* d, UNS8 nodeid)
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   248
{
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   249
	printf("Slave %x boot up\n", nodeid);
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   250
}
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   251
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   252
/***************************  CALLBACK FUNCTIONS  *****************************************/
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   253
void CANOpenShellOD_initialisation(CO_Data* d)
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   254
{
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   255
	printf("Node_initialisation\n");
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   256
}
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   257
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   258
void CANOpenShellOD_preOperational(CO_Data* d)
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   259
{
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   260
	printf("Node_preOperational\n");
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   261
}
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   262
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   263
void CANOpenShellOD_operational(CO_Data* d)
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   264
{
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   265
	printf("Node_operational\n");
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   266
}
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   267
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   268
void CANOpenShellOD_stopped(CO_Data* d)
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   269
{
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   270
	printf("Node_stopped\n");
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   271
}
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   272
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   273
void CANOpenShellOD_post_sync(CO_Data* d)
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   274
{
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   275
	//printf("Master_post_sync\n");
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   276
}
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   277
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   278
void CANOpenShellOD_post_TPDO(CO_Data* d)
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   279
{
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   280
	//printf("Master_post_TPDO\n");
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   281
}
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   282
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   283
/***************************  INITIALISATION  **********************************/
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   284
void Init(CO_Data* d, UNS32 id)
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   285
{
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   286
	if(Board.baudrate)
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   287
	{
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   288
		/* Init node state*/
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   289
		setState(CANOpenShellOD_Data, Initialisation);
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   290
	}
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   291
}
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   292
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   293
/***************************  CLEANUP  *****************************************/
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   294
void Exit(CO_Data* d, UNS32 nodeid)
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   295
{
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   296
	if(strcmp(Board.baudrate, "none"))
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   297
	{
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   298
		/* Reset all nodes on the network */
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   299
		masterSendNMTstateChange(CANOpenShellOD_Data, 0 , NMT_Reset_Node);
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   300
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   301
		/* Stop master */
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   302
		setState(CANOpenShellOD_Data, Stopped);
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   303
	}
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   304
}
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   305
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   306
int NodeInit(int NodeID, int NodeType)
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   307
{
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   308
	if(NodeType)
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   309
		CANOpenShellOD_Data = &CANOpenShellMasterOD_Data;
568
84fb13d1b3d2 fix some minor bugs
greg
parents: 559
diff changeset
   310
	else
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   311
		CANOpenShellOD_Data = &CANOpenShellSlaveOD_Data;
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   312
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   313
	/* Load can library */
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   314
	LoadCanDriver(LibraryPath);
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   315
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   316
	/* Define callback functions */
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   317
	CANOpenShellOD_Data->initialisation = CANOpenShellOD_initialisation;
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   318
	CANOpenShellOD_Data->preOperational = CANOpenShellOD_preOperational;
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   319
	CANOpenShellOD_Data->operational = CANOpenShellOD_operational;
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   320
	CANOpenShellOD_Data->stopped = CANOpenShellOD_stopped;
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   321
	CANOpenShellOD_Data->post_sync = CANOpenShellOD_post_sync;
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   322
	CANOpenShellOD_Data->post_TPDO = CANOpenShellOD_post_TPDO;
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   323
	CANOpenShellOD_Data->post_SlaveBootup=CANOpenShellOD_post_SlaveBootup;
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   324
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   325
	/* Open the Peak CANOpen device */
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   326
	if(!canOpen(&Board,CANOpenShellOD_Data)) return INIT_ERR;
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   327
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   328
	/* Defining the node Id */
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   329
	setNodeId(CANOpenShellOD_Data, NodeID);
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   330
	/* Start Timer thread */
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   331
	StartTimerLoop(&Init);
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   332
	return 0;
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   333
}
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   334
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   335
void help_menu(void)
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   336
{
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   337
	printf("   MANDATORY COMMAND (must be the first command):\n");
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   338
	printf("     load#CanLibraryPath,channel,baudrate,nodeid,type (0:slave, 1:master)\n");
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   339
	printf("\n");
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   340
	printf("   NETWORK: (if nodeid=0x00 : broadcast)\n");
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   341
	printf("     ssta#nodeid : Start a node\n");
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   342
	printf("     ssto#nodeid : Stop a node\n");
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   343
	printf("     srst#nodeid : Reset a node\n");
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   344
	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
   345
	printf("     wait#seconds : Sleep for n seconds\n");
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   346
	printf("\n");
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   347
	printf("   SDO: (size in bytes)\n");
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   348
	printf("     info#nodeid\n");
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   349
	printf("     rsdo#nodeid,index,subindex : read sdo\n");
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   350
	printf("        ex : rsdo#42,1018,01\n");
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   351
	printf("     wsdo#nodeid,index,subindex,size,data : write sdo\n");
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   352
	printf("        ex : wsdo#42,6200,01,01,FF\n");
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   353
	printf("\n");
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   354
	printf("   Note: All numbers are hex\n");
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   355
	printf("\n");
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   356
	printf("     help : Display this menu\n");
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   357
	printf("     quit : Quit application\n");
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   358
	printf("\n");
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   359
	printf("\n");
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   360
}
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   361
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   362
int ExtractNodeId(char *command) {
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   363
	int nodeid;
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   364
	sscanf(command, "%2x", &nodeid);
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   365
	return nodeid;
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   366
}
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   367
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   368
int ProcessCommand(char* command)
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   369
{
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   370
	int ret = 0;
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   371
	int sec = 0;
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   372
	int NodeID;
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   373
	int NodeType;
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   374
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   375
	EnterMutex();
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   376
	switch(cst_str4(command[0], command[1], command[2], command[3]))
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   377
	{
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   378
		case cst_str4('h', 'e', 'l', 'p') : /* Display Help*/
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   379
					help_menu();
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   380
					break;
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   381
		case cst_str4('s', 's', 't', 'a') : /* Slave Start*/
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   382
					StartNode(ExtractNodeId(command + 5));
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   383
					break;
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   384
		case cst_str4('s', 's', 't', 'o') : /* Slave Stop */
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   385
					StopNode(ExtractNodeId(command + 5));
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   386
					break;
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   387
		case cst_str4('s', 'r', 's', 't') : /* Slave Reset */
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   388
					ResetNode(ExtractNodeId(command + 5));
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   389
					break;
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   390
		case cst_str4('i', 'n', 'f', 'o') : /* Retrieve node informations */
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   391
					GetSlaveNodeInfo(ExtractNodeId(command + 5));
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   392
					break;
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   393
		case cst_str4('r', 's', 'd', 'o') : /* Read device entry */
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   394
					ReadDeviceEntry(command);
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   395
					break;
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   396
		case cst_str4('w', 's', 'd', 'o') : /* Write device entry */
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   397
					WriteDeviceEntry(command);
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   398
					break;
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   399
		case cst_str4('s', 'c', 'a', 'n') : /* Display master node state */
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   400
					DiscoverNodes();
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   401
					break;
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   402
		case cst_str4('w', 'a', 'i', 't') : /* Display master node state */
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   403
					ret = sscanf(command, "wait#%d", &sec);
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   404
					if(ret == 1) {
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   405
						LeaveMutex();
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   406
						SleepFunction(sec);
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   407
						return 0;
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   408
					}
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   409
					break;
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   410
		case cst_str4('q', 'u', 'i', 't') : /* Quit application */
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   411
					LeaveMutex();
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   412
					return QUIT;
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   413
		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
   414
					ret = sscanf(command, "load#%100[^,],%30[^,],%4[^,],%d,%d",
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   415
							LibraryPath,
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   416
							BoardBusName,
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   417
							BoardBaudRate,
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   418
							&NodeID,
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   419
							&NodeType);
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   420
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   421
					if(ret == 5)
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
						LeaveMutex();
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   424
						ret = NodeInit(NodeID, NodeType);
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   425
						return ret;
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   426
					}
605
f91ee161b3a1 -add LeaveMutex to avoid canopenshell deadlock when call NodeInit
greg
parents: 568
diff changeset
   427
					else
f91ee161b3a1 -add LeaveMutex to avoid canopenshell deadlock when call NodeInit
greg
parents: 568
diff changeset
   428
					{
f91ee161b3a1 -add LeaveMutex to avoid canopenshell deadlock when call NodeInit
greg
parents: 568
diff changeset
   429
						printf("Invalid load parameters\n");
f91ee161b3a1 -add LeaveMutex to avoid canopenshell deadlock when call NodeInit
greg
parents: 568
diff changeset
   430
					}
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   431
					break;
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   432
		default :
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   433
					help_menu();
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   434
	}
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   435
	LeaveMutex();
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   436
	return 0;
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
/****************************************************************************/
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   440
/***************************  MAIN  *****************************************/
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   441
/****************************************************************************/
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   442
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   443
int main(int argc, char** argv)
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   444
{
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   445
	extern char *optarg;
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   446
	char command[200];
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   447
	char* res;
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   448
	int ret=0;
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   449
	int sysret=0;
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   450
	int i=0;
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   451
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   452
	/* Print help and exit immediatly*/
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   453
	if(argc < 2)
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   454
	{
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   455
		help_menu();
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   456
		exit(1);
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   457
	}
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   458
559
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   459
	/* Init stack timer */
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   460
	TimerInit();
8543e7edae4a Remove old entries in visual studio files
greg
parents: 555
diff changeset
   461
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   462
	/* Strip command-line*/
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   463
	for(i=1 ; i<argc ; i++)
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   464
	{
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   465
		if(ProcessCommand(argv[i]) == INIT_ERR) goto init_fail;
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   466
	}
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   467
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   468
	/* Enter in a loop to read stdin command until "quit" is called */
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   469
	while(ret != QUIT)
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   470
	{
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   471
		// wait on stdin for string command
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   472
		res = fgets(command, sizeof(command), stdin);
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   473
		sysret = system(CLEARSCREEN);
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   474
		ret = ProcessCommand(command);
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   475
		fflush(stdout);
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   476
	}
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   477
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   478
	printf("Finishing.\n");
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   479
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   480
	// Stop timer thread
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   481
	StopTimerLoop(&Exit);
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   482
568
84fb13d1b3d2 fix some minor bugs
greg
parents: 559
diff changeset
   483
	/* Close CAN board */
84fb13d1b3d2 fix some minor bugs
greg
parents: 559
diff changeset
   484
	canClose(CANOpenShellOD_Data);
84fb13d1b3d2 fix some minor bugs
greg
parents: 559
diff changeset
   485
555
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   486
init_fail:
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   487
	TimerCleanup();
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   488
	return 0;
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   489
}
ee24dcbd3e64 Add new example : CANOpen shell
greg
parents:
diff changeset
   490