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