Skip to content

Commit 93c8eff

Browse files
authored
Merge pull request #21 from mac-can/development
Release Candidate 3 for Version 0.3
2 parents 999a737 + f7dce4d commit 93c8eff

File tree

14 files changed

+606
-221
lines changed

14 files changed

+606
-221
lines changed

Examples/Python/CANAPI.py

Lines changed: 89 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@
5252
Interface API for various CAN interfaces from different
5353
vendors running under multiple operating systems.
5454
55-
$Author: makemake $
55+
$Author: quaoar $
5656
57-
$Rev: 1274 $
57+
$Rev: 1278 $
5858
"""
5959
from ctypes import *
6060
import platform
@@ -69,7 +69,7 @@
6969

7070
# CAN API V3 - Python Wrapper
7171
#
72-
CAN_API_V3_PYTHON = {'major': 0, 'minor': 2, 'patch': 2}
72+
CAN_API_V3_PYTHON = {'major': 0, 'minor': 3, 'patch': 1}
7373

7474
# CAN Identifier Ranges
7575
#
@@ -122,6 +122,13 @@
122122
(49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64): c_uint8(0xF)
123123
}
124124

125+
# CAN Acceptance Filter: (code ^ id) & mask == 0
126+
#
127+
CANACC_CODE_11BIT = 0x000 # mask for 11-bit acceptance code
128+
CANACC_MASK_11BIT = 0x000 # mask for 11-bit acceptance mask
129+
CANACC_CODE_29BIT = 0x00000000 # mask for 29-bit acceptance code
130+
CANACC_MASK_29BIT = 0x00000000 # mask for 29-bit acceptance mask
131+
125132
# CAN 2.0 Predefined Bit-rates (as index acc. CiA)
126133
#
127134
CANBTR_INDEX_1M = c_int32(0) # bit-rate: 1000 kbit/s
@@ -638,6 +645,70 @@ def bitrate(self):
638645
print('+++ exception: {}'.format(e))
639646
raise
640647

648+
def filter11bit(self, code, mask):
649+
"""
650+
sets a 11-bit filter for the CAN controller.
651+
652+
:param code: 11-bit code for the filter (or None)
653+
:param mask: 11-bit mask for the filter (or None)
654+
:return: result, code, mask
655+
result: 0 if successful, or a negative value on error
656+
code: 11-bit code for the filter
657+
mask: 11-bit mask for the filter
658+
"""
659+
try:
660+
# set the 11-bit filter (if code or mask are not None)
661+
if code is not None or mask is not None:
662+
__filter = c_uint64(0)
663+
if code is not None:
664+
__filter.value = code << 32
665+
if mask is not None:
666+
__filter.value |= mask & 0xFFFFFFFF
667+
result = self.__m_library.can_property(self.__m_handle, 42, byref(__filter), 8)
668+
if result < 0:
669+
return int(result), None, None
670+
# get the 11-bit filter
671+
__value = c_uint64(0)
672+
result = self.__m_library.can_property(self.__m_handle, 40, byref(__value), 8)
673+
if result < 0:
674+
return int(result), None, None
675+
return int(result), int(__filter.value >> 32), int(__filter.value & 0xFFFFFFFF)
676+
except Exception as e:
677+
print('+++ exception: {}'.format(e))
678+
raise
679+
680+
def filter29bit(self, code, mask):
681+
"""
682+
sets a 29-bit filter for the CAN controller.
683+
684+
:param code: 29-bit code for the filter (or None)
685+
:param mask: 29-bit mask for the filter (or None)
686+
:return: result, code, mask
687+
result: 0 if successful, or a negative value on error
688+
code: 29-bit code for the filter
689+
mask: 29-bit mask for the filter
690+
"""
691+
try:
692+
# set the 29-bit filter (if code or mask are not None)
693+
if code is not None or mask is not None:
694+
__filter = c_uint64(0)
695+
if code is not None:
696+
__filter.value = code << 32
697+
if mask is not None:
698+
__filter.value |= mask & 0xFFFFFFFF
699+
result = self.__m_library.can_property(self.__m_handle, 43, byref(__filter), 8)
700+
if result < 0:
701+
return int(result), None, None
702+
# get the 29-bit filter
703+
__value = c_uint64(0)
704+
result = self.__m_library.can_property(self.__m_handle, 41, byref(__value), 8)
705+
if result < 0:
706+
return int(result), None, None
707+
return int(result), int(__filter.value >> 32), int(__filter.value & 0xFFFFFFFF)
708+
except Exception as e:
709+
print('+++ exception: {}'.format(e))
710+
raise
711+
641712
def hardware(self):
642713
"""
643714
retrieves the hardware version of the CAN controller
@@ -780,6 +851,20 @@ def len2dlc(length):
780851
else:
781852
print('>>> can.status() >>> 0x{:02X}'.format(status.byte))
782853

854+
# set acceptance filter
855+
code = CANACC_CODE_11BIT
856+
mask = CANACC_MASK_11BIT
857+
print('>>> can.filter11bit(0x{:03X}, 0x{:03X})'.format(code, mask))
858+
res, code, mask = can.filter11bit(code=code, mask=mask)
859+
if res < CANERR_NOERROR:
860+
print('+++ error: can.filter11bit returned {}'.format(res))
861+
code = CANACC_CODE_29BIT
862+
mask = CANACC_MASK_29BIT
863+
print('>>> can.filter29bit(0x{:08X}, 0x{:08X})'.format(code, mask))
864+
res, code, mask = can.filter29bit(code=code, mask=mask)
865+
if res < CANERR_NOERROR:
866+
print('+++ error: can.filter29bit returned {}'.format(res))
867+
783868
# start the CAN controller
784869
if bitRate.index > 0: # FIXME: Expected type 'int', got 'c_int32[int]' instead
785870
print('>>> can.start([{},[{},{},{},{},{}],[{},{},{},{},])'.format(bitRate.btr.frequency,
@@ -831,5 +916,5 @@ def len2dlc(length):
831916
# have a great time
832917
print('Bye, bye!')
833918

834-
# * $Id: CANAPI.py 1274 2024-04-21 17:34:21Z makemake $ *** (c) UV Software, Berlin ***
919+
# * $Id: CANAPI.py 1278 2024-04-23 08:34:36Z quaoar $ *** (c) UV Software, Berlin ***
835920
#

Examples/Python/can_recv.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
#
1111
def sigterm(signo, frame):
1212
print()
13-
# print('>>> can.kill()')
14-
# result = can.kill()
15-
# if result < 0:
16-
# print('+++ error: can.kill returned {}'.format(result))
13+
print('>>> can.kill()')
14+
result = can.kill()
15+
if result < 0:
16+
print('+++ error: can.kill returned {}'.format(result))
1717
print('>>> can.exit()')
1818
result = can.exit()
1919
if result < 0:
@@ -69,6 +69,20 @@ def sigterm(signo, frame):
6969
else:
7070
print('>>> can.status() >>> 0x{:02X}'.format(status.byte))
7171

72+
# set acceptance filter
73+
code = CANACC_CODE_11BIT
74+
mask = CANACC_MASK_11BIT
75+
print('>>> can.filter11bit(0x{:03X}, 0x{:03X})'.format(code, mask))
76+
res, code, mask = can.filter11bit(code=code, mask=mask)
77+
if res < CANERR_NOERROR:
78+
print('+++ error: can.filter11bit returned {}'.format(res))
79+
code = CANACC_CODE_29BIT
80+
mask = CANACC_MASK_29BIT
81+
print('>>> can.filter29bit(0x{:08X}, 0x{:08X})'.format(code, mask))
82+
res, code, mask = can.filter29bit(code=code, mask=mask)
83+
if res < CANERR_NOERROR:
84+
print('+++ error: can.filter29bit returned {}'.format(res))
85+
7286
# start the CAN controller
7387
if int(bitRate.index) > 0: # FIXME: Expected type 'int', got 'c_int32[int]' instead
7488
print('>>> can.start([{},[{},{},{},{},{}],[{},{},{},{},])'.format(bitRate.btr.frequency,

Examples/Python/can_send.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
#
1212
def sigterm(signo, frame):
1313
print()
14-
# print('>>> can.kill()')
15-
# result = can.kill()
16-
# if result < 0:
17-
# print('+++ error: can.kill returned {}'.format(result))
14+
print('>>> can.kill()')
15+
result = can.kill()
16+
if result < 0:
17+
print('+++ error: can.kill returned {}'.format(result))
1818
print('>>> can.exit()')
1919
result = can.exit()
2020
if result < 0:

Libraries/CANAPI/Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ ifeq ($(current_OS),Linux) # linux - libuvcanpcb.so
135135

136136
LIBRARY = libuvcanpcb
137137

138-
SONAME = $(LIBRARY).so
138+
SONAME = $(LIBRARY).so.1
139139
TARGET = $(SONAME).$(VERSION)
140140
STATIC = $(LIBRARY).a
141141

@@ -225,8 +225,8 @@ ifeq ($(current_OS),Darwin)
225225
$(RM) $(INSTALL)/$(LIBRARY).dylib ; $(LN) $(INSTALL)/$(TARGET) $(INSTALL)/$(LIBRARY).dylib
226226
endif
227227
ifeq ($(current_OS),Linux)
228-
$(RM) $(INSTALL)/$(SONAME) ; ln -s $(INSTALL)/$(TARGET) $(INSTALL)/$(SONAME)
229-
$(RM) $(INSTALL)/$(LIBRARY).so ; ln -s $(INSTALL)/$(SONAME) $(INSTALL)/$(LIBRARY).so
228+
$(RM) $(INSTALL)/$(SONAME) ; $(LN) $(INSTALL)/$(TARGET) $(INSTALL)/$(SONAME)
229+
$(RM) $(INSTALL)/$(LIBRARY).so ; $(LN) $(INSTALL)/$(SONAME) $(INSTALL)/$(LIBRARY).so
230230
endif
231231

232232

Libraries/PeakCAN/Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ ifeq ($(current_OS),Linux) # Linux - libpeakcan.so
136136

137137
LIBRARY = libpeakcan
138138

139-
SONAME = $(LIBRARY).so
139+
SONAME = $(LIBRARY).so.1
140140
TARGET = $(SONAME).$(VERSION)
141141
STATIC = $(LIBRARY).a
142142

@@ -227,8 +227,8 @@ ifeq ($(current_OS),Darwin)
227227
$(RM) $(INSTALL)/$(LIBRARY).dylib ; $(LN) $(INSTALL)/$(TARGET) $(INSTALL)/$(LIBRARY).dylib
228228
endif
229229
ifeq ($(current_OS),Linux)
230-
$(RM) $(INSTALL)/$(SONAME) ; ln -s $(INSTALL)/$(TARGET) $(INSTALL)/$(SONAME)
231-
$(RM) $(INSTALL)/$(LIBRARY).so ; ln -s $(INSTALL)/$(SONAME) $(INSTALL)/$(LIBRARY).so
230+
$(RM) $(INSTALL)/$(SONAME) ; $(LN) $(INSTALL)/$(TARGET) $(INSTALL)/$(SONAME)
231+
$(RM) $(INSTALL)/$(LIBRARY).so ; $(LN) $(INSTALL)/$(SONAME) $(INSTALL)/$(LIBRARY).so
232232
endif
233233

234234

Utilities/can_moni/Sources/Options.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ struct SOptions {
6868
uint32_t m_u32Mask;
6969
} m_StdFilter, m_XtdFilter;
7070
char* m_szExcludeList;
71+
#if (CAN_TRACE_SUPPORTED != 0)
72+
enum {
73+
eTraceOff,
74+
eTraceBinary,
75+
eTraceLogger,
76+
eTraceVendor
77+
} m_eTraceMode;
78+
#endif
7179
bool m_fListBitrates;
7280
bool m_fListBoards;
7381
bool m_fTestBoards;

Utilities/can_moni/Sources/Options_p.cpp

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ static const char* c_szWarranty = CAN_MONI_WARRANTY;
5555
static const char* c_szLicense = CAN_MONI_LICENSE;
5656
static const char* c_szBasename = CAN_MONI_PROGRAM;
5757
static const char* c_szInterface = "(unknown)";
58-
static const char* c_szExcludeList = "~0x00-0x7FF";
5958

6059
SOptions::SOptions() {
6160
// to have default bus speed from bit-timing index
@@ -77,12 +76,14 @@ SOptions::SOptions() {
7776
m_StdFilter.m_u32Mask = CANACC_MASK_11BIT;
7877
m_XtdFilter.m_u32Code = CANACC_CODE_29BIT;
7978
m_XtdFilter.m_u32Mask = CANACC_MASK_29BIT;
80-
m_szExcludeList = (char*)c_szExcludeList;
79+
m_szExcludeList = (char*)NULL;
80+
#if (CAN_TRACE_SUPPORTED != 0)
81+
m_eTraceMode = SOptions::eTraceOff;
82+
#endif
8183
m_fListBitrates = false;
8284
m_fListBoards = false;
8385
m_fTestBoards = false;
8486
m_fVerbose = false;
85-
m_fVerbose = false;
8687
m_fExit = false;
8788
}
8889

@@ -110,6 +111,9 @@ int SOptions::ScanCommanline(int argc, const char* argv[], FILE* err, FILE* out)
110111
int optFmtWrap = 0;
111112
#endif
112113
int optExclude = 0;
114+
#if (CAN_TRACE_SUPPORTED != 0)
115+
int optTraceMode = 0;
116+
#endif
113117
int optListBitrates = 0;
114118
int optListBoards = 0;
115119
int optTestBoards = 0;
@@ -154,6 +158,7 @@ int SOptions::ScanCommanline(int argc, const char* argv[], FILE* err, FILE* out)
154158
{"wraparound", required_argument, 0, 'w'},
155159
{"exclude", required_argument, 0, 'x'},
156160
{"script", required_argument, 0, 's'},
161+
{"trace", required_argument, 0, 'y'},
157162
{"list-bitrates", optional_argument, 0, 'l'},
158163
#if (OPTION_CANAPI_LIBRARY != 0)
159164
{"list-boards", optional_argument, 0, 'L'},
@@ -179,9 +184,9 @@ int SOptions::ScanCommanline(int argc, const char* argv[], FILE* err, FILE* out)
179184
#endif
180185
// (2) scan command-line for options
181186
#if (OPTION_CANAPI_LIBRARY != 0)
182-
while ((opt = getopt_long(argc, (char * const *)argv, "b:vp:m:t:i:d:a:w:x:s:lLTh", long_options, NULL)) != -1) {
187+
while ((opt = getopt_long(argc, (char * const *)argv, "b:vp:m:t:i:d:a:w:x:s:y:lLTh", long_options, NULL)) != -1) {
183188
#else
184-
while ((opt = getopt_long(argc, (char * const *)argv, "b:vm:t:i:d:a:w:x:s:lLTj:h", long_options, NULL)) != -1) {
189+
while ((opt = getopt_long(argc, (char * const *)argv, "b:vm:t:i:d:a:w:x:s:y:lLTj:h", long_options, NULL)) != -1) {
185190
#endif
186191
switch (opt) {
187192
/* option '--baudrate=<baudrate>' (-b) */
@@ -323,8 +328,7 @@ int SOptions::ScanCommanline(int argc, const char* argv[], FILE* err, FILE* out)
323328
fprintf(err, "%s: illegal argument for option `--error-frames'\n", m_szBasename);
324329
return 1;
325330
}
326-
m_OpMode.byte |= CANMODE_ERR;
327-
331+
m_OpMode.byte |= CANMODE_ERR;
328332
break;
329333
/* option '--no-extended-frames' */
330334
case 'X':
@@ -336,7 +340,7 @@ int SOptions::ScanCommanline(int argc, const char* argv[], FILE* err, FILE* out)
336340
fprintf(err, "%s: illegal argument for option `--no-extended-frames'\n", m_szBasename);
337341
return 1;
338342
}
339-
m_OpMode.byte |= CANMODE_NXTD;
343+
m_OpMode.byte |= CANMODE_NXTD;
340344
break;
341345
/* option '--no-remote-frames' */
342346
case 'R':
@@ -348,7 +352,7 @@ int SOptions::ScanCommanline(int argc, const char* argv[], FILE* err, FILE* out)
348352
fprintf(err, "%s: missing argument for option `--no-remote-frames'\n", m_szBasename);
349353
return 1;
350354
}
351-
m_OpMode.byte |= CANMODE_NRTR;
355+
m_OpMode.byte |= CANMODE_NRTR;
352356
break;
353357
/* option '--code=<11-bit-code>' */
354358
case '1':
@@ -430,6 +434,36 @@ int SOptions::ScanCommanline(int argc, const char* argv[], FILE* err, FILE* out)
430434
}
431435
m_XtdFilter.m_u32Mask = (uint32_t)intarg;
432436
break;
437+
/* option '--trace=(ON|OFF)' (-y) */
438+
#if (CAN_TRACE_SUPPORTED != 0)
439+
case 'y':
440+
if (optTraceMode++) {
441+
fprintf(err, "%s: duplicated option `--trace'\n", m_szBasename);
442+
return 1;
443+
}
444+
if (optarg == NULL) {
445+
fprintf(err, "%s: missing argument for option `--trace'\n", m_szBasename);
446+
return 1;
447+
}
448+
#if (CAN_TRACE_SUPPORTED == 1)
449+
if (!strcasecmp(optarg, "OFF") || !strcasecmp(optarg, "NO") || !strcasecmp(optarg, "n") || !strcasecmp(optarg, "0"))
450+
m_eTraceMode = SOptions::eTraceOff;
451+
else if (!strcasecmp(optarg, "ON") || !strcasecmp(optarg, "YES") || !strcasecmp(optarg, "y") || !strcasecmp(optarg, "1"))
452+
m_eTraceMode = SOptions::eTraceVendor;
453+
#else
454+
if (!strcasecmp(optarg, "BIN") || !strcasecmp(optarg, "BINARY") || !strcasecmp(optarg, "default"))
455+
m_eTraceMode = SOptions::eTraceBinary;
456+
else if (!strcasecmp(optarg, "CSV") || !strcasecmp(optarg, "logger") || !strcasecmp(optarg, "log"))
457+
m_eTraceMode = SOptions::eTraceLogger;
458+
else if (!strcasecmp(optarg, "TRC") || !strcasecmp(optarg, "vendor"))
459+
m_eTraceMode = SOptions::eTraceVendor;
460+
#endif
461+
else {
462+
fprintf(err, "%s: illegal argument for option `--trace'\n", m_szBasename);
463+
return 1;
464+
}
465+
break;
466+
#endif
433467
/* option '--time=(ABS|REL|ZERO)' (-t) */
434468
case 't':
435469
if (optFmtTime++) {
@@ -761,6 +795,9 @@ void SOptions::ShowUsage(FILE* stream, bool args) {
761795
fprintf(stream, " -b, --baudrate=<baudrate> CAN bit-timing in kbps (default=250), or\n");
762796
fprintf(stream, " --bitrate=<bit-rate> CAN bit-rate settings (as key/value list)\n");
763797
fprintf(stream, " -v, --verbose show detailed bit-rate settings\n");
798+
#if (CAN_TRACE_SUPPORTED != 0)
799+
fprintf(stream, " -y, --trace=(ON|OFF) write a trace file (default=OFF)\n");
800+
#endif
764801
#if (CAN_FD_SUPPORTED != 0)
765802
fprintf(stream, " --list-bitrates[=<mode>] list standard bit-rate settings and exit\n");
766803
#else

0 commit comments

Comments
 (0)