Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
ebpfCAT
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
karaboDevices
ebpfCAT
Commits
f236798a
Commit
f236798a
authored
2 years ago
by
Martin Teichmann
Browse files
Options
Downloads
Patches
Plain Diff
improve error handling
don't use RuntimeError, define an EtherCatError instead
parent
ab5a0b7b
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
ebpfcat/ethercat.py
+26
-22
26 additions, 22 deletions
ebpfcat/ethercat.py
ebpfcat/scripts.py
+13
-9
13 additions, 9 deletions
ebpfcat/scripts.py
with
39 additions
and
31 deletions
ebpfcat/ethercat.py
+
26
−
22
View file @
f236798a
...
...
@@ -33,6 +33,10 @@ from random import randint
from
socket
import
socket
,
AF_PACKET
,
SOCK_DGRAM
from
struct
import
pack
,
unpack
,
unpack_from
,
calcsize
class
EtherCatError
(
Exception
):
pass
class
ECCmd
(
Enum
):
NOP
=
0
# No Operation
APRD
=
1
# Auto Increment Read
...
...
@@ -285,7 +289,7 @@ class EtherCat(Protocol):
wkc
,
=
unpack
(
"
<H
"
,
data
[
stop
:
stop
+
2
])
if
wkc
==
0
:
future
.
set_exception
(
Runtime
Error
(
"
datagram was not processed
"
))
EtherCat
Error
(
"
datagram was not processed
"
))
else
:
future
.
set_result
(
data
[
start
:
stop
])
dgrams
=
[]
...
...
@@ -367,7 +371,7 @@ class EtherCat(Protocol):
for
i
in
count
(
no
):
try
:
await
self
.
roundtrip
(
ECCmd
.
FPRD
,
i
,
0x10
,
"
H
"
,
0
)
except
Runtime
Error
:
except
EtherCat
Error
:
return
i
# this address is not in use
def
connection_made
(
self
,
transport
):
...
...
@@ -548,7 +552,7 @@ class Terminal:
s
,
error
=
await
self
.
ec
.
roundtrip
(
ECCmd
.
FPRD
,
self
.
position
,
0x0130
,
"
H2xH
"
)
if
error
!=
0
:
raise
Runtime
Error
(
f
"
AL register
{
error
}
"
)
raise
EtherCat
Error
(
f
"
AL register
{
error
}
"
)
async
def
get_error
(
self
):
"""
read the error register
"""
...
...
@@ -620,7 +624,7 @@ class Terminal:
"""
send data to the mailbox
"""
status
,
=
await
self
.
read
(
0x805
,
"
B
"
)
# always using mailbox 0, OK?
if
status
&
8
:
raise
Runtime
Error
(
"
mailbox full, read first
"
)
raise
EtherCat
Error
(
"
mailbox full, read first
"
)
assert
self
.
mbx_out_off
is
not
None
,
"
not send mailbox defined
"
await
self
.
write
(
self
.
mbx_out_off
,
"
HHBB
"
,
datasize
(
args
,
data
),
...
...
@@ -653,10 +657,10 @@ class Terminal:
while
fragments
:
type
,
data
=
await
self
.
mbx_recv
()
if
type
is
not
MBXType
.
COE
:
raise
Runtime
Error
(
f
"
expected CoE package, got
{
type
}
"
)
raise
EtherCat
Error
(
f
"
expected CoE package, got
{
type
}
"
)
coecmd
,
rodcmd
,
fragments
=
unpack
(
"
<HBxH
"
,
data
[:
6
])
if
rodcmd
&
0x7f
!=
odcmd
.
value
+
1
:
raise
Runtime
Error
(
f
"
expected
{
odcmd
.
value
}
, got
{
odcmd
}
"
)
raise
EtherCat
Error
(
f
"
expected
{
odcmd
.
value
}
, got
{
odcmd
}
"
)
ret
.
append
(
data
[
offset
:])
offset
=
6
return
b
""
.
join
(
ret
)
...
...
@@ -670,16 +674,16 @@ class Terminal:
index
,
1
if
subindex
is
None
else
subindex
)
type
,
data
=
await
self
.
mbx_recv
()
if
type
is
not
MBXType
.
COE
:
raise
Runtime
Error
(
f
"
expected CoE, got
{
type
}
"
)
raise
EtherCat
Error
(
f
"
expected CoE, got
{
type
}
"
)
coecmd
,
sdocmd
,
idx
,
subidx
,
size
=
unpack
(
"
<HBHBI
"
,
data
[:
10
])
if
coecmd
>>
12
!=
CoECmd
.
SDORES
.
value
:
if
subindex
is
None
and
coecmd
>>
12
==
CoECmd
.
SDOREQ
.
value
:
return
b
""
# if there is no data, the terminal fails
raise
Runtime
Error
(
raise
EtherCat
Error
(
f
"
expected CoE SDORES (3), got
{
coecmd
>>
12
:
x
}
"
f
"
for
{
index
:
X
}
:
{
9
if
subindex
is
None
else
subindex
:
02
X
}
"
)
if
idx
!=
index
:
raise
Runtime
Error
(
f
"
requested index
{
index
}
, got
{
idx
}
"
)
raise
EtherCat
Error
(
f
"
requested index
{
index
}
, got
{
idx
}
"
)
if
sdocmd
&
2
:
return
data
[
6
:
10
-
((
sdocmd
>>
2
)
&
3
)]
ret
=
[
data
[
10
:]]
...
...
@@ -693,13 +697,13 @@ class Terminal:
1
if
subindex
is
None
else
subindex
)
type
,
data
=
await
self
.
mbx_recv
()
if
type
is
not
MBXType
.
COE
:
raise
Runtime
Error
(
f
"
expected CoE, got
{
type
}
"
)
raise
EtherCat
Error
(
f
"
expected CoE, got
{
type
}
"
)
coecmd
,
sdocmd
=
unpack
(
"
<HB
"
,
data
[:
3
])
if
coecmd
>>
12
!=
CoECmd
.
SDORES
.
value
:
raise
Runtime
Error
(
raise
EtherCat
Error
(
f
"
expected CoE cmd SDORES, got
{
coecmd
}
"
)
if
sdocmd
&
0xe0
!=
0
:
raise
Runtime
Error
(
f
"
requested index
{
index
}
, got
{
idx
}
"
)
raise
EtherCat
Error
(
f
"
requested index
{
index
}
, got
{
idx
}
"
)
if
sdocmd
&
1
and
len
(
data
)
==
7
:
data
=
data
[:
3
+
(
sdocmd
>>
1
)
&
7
]
ret
+=
data
[
3
:]
...
...
@@ -708,7 +712,7 @@ class Terminal:
break
toggle
^=
0x10
if
retsize
!=
size
:
raise
Runtime
Error
(
f
"
expected
{
size
}
bytes, got
{
retsize
}
"
)
raise
EtherCat
Error
(
f
"
expected
{
size
}
bytes, got
{
retsize
}
"
)
return
b
""
.
join
(
ret
)
async
def
sdo_write
(
self
,
data
,
index
,
subindex
=
None
):
...
...
@@ -720,12 +724,12 @@ class Terminal:
index
,
subindex
,
data
)
type
,
data
=
await
self
.
mbx_recv
()
if
type
is
not
MBXType
.
COE
:
raise
Runtime
Error
(
f
"
expected CoE, got
{
type
}
,
{
data
}
{
odata
}
{
index
:
x
}
{
subindex
}
"
)
raise
EtherCat
Error
(
f
"
expected CoE, got
{
type
}
,
{
data
}
{
odata
}
{
index
:
x
}
{
subindex
}
"
)
coecmd
,
sdocmd
,
idx
,
subidx
=
unpack
(
"
<HBHB
"
,
data
[:
6
])
if
idx
!=
index
or
subindex
!=
subidx
:
raise
Runtime
Error
(
f
"
requested index
{
index
}
, got
{
idx
}
"
)
raise
EtherCat
Error
(
f
"
requested index
{
index
}
, got
{
idx
}
"
)
if
coecmd
>>
12
!=
CoECmd
.
SDORES
.
value
:
raise
Runtime
Error
(
f
"
expected CoE SDORES, got
{
coecmd
>>
12
:
x
}
"
)
raise
EtherCat
Error
(
f
"
expected CoE SDORES, got
{
coecmd
>>
12
:
x
}
"
)
else
:
async
with
self
.
mbx_lock
:
stop
=
min
(
len
(
data
),
self
.
mbx_out_sz
-
16
)
...
...
@@ -737,12 +741,12 @@ class Terminal:
data
=
data
[:
stop
])
type
,
data
=
await
self
.
mbx_recv
()
if
type
is
not
MBXType
.
COE
:
raise
Runtime
Error
(
f
"
expected CoE, got
{
type
}
"
)
raise
EtherCat
Error
(
f
"
expected CoE, got
{
type
}
"
)
coecmd
,
sdocmd
,
idx
,
subidx
=
unpack
(
"
<HBHB
"
,
data
[:
6
])
if
coecmd
>>
12
!=
CoECmd
.
SDORES
.
value
:
raise
Runtime
Error
(
f
"
expected CoE SDORES, got
{
coecmd
>>
12
:
x
}
"
)
raise
EtherCat
Error
(
f
"
expected CoE SDORES, got
{
coecmd
>>
12
:
x
}
"
)
if
idx
!=
index
or
subindex
!=
subidx
:
raise
Runtime
Error
(
f
"
requested index
{
index
}
, got
{
idx
}
"
)
raise
EtherCat
Error
(
f
"
requested index
{
index
}
, got
{
idx
}
"
)
toggle
=
0
while
stop
<
len
(
data
):
start
=
stop
...
...
@@ -760,12 +764,12 @@ class Terminal:
1
if
subindex
is
None
else
subindex
,
data
=
d
)
type
,
data
=
await
self
.
mbx_recv
()
if
type
is
not
MBXType
.
COE
:
raise
Runtime
Error
(
f
"
expected CoE, got
{
type
}
"
)
raise
EtherCat
Error
(
f
"
expected CoE, got
{
type
}
"
)
coecmd
,
sdocmd
,
idx
,
subidx
=
unpack
(
"
<HBHB
"
,
data
[:
6
])
if
coecmd
>>
12
!=
CoECmd
.
SDORES
.
value
:
raise
Runtime
Error
(
f
"
expected CoE SDORES
"
)
raise
EtherCat
Error
(
f
"
expected CoE SDORES
"
)
if
idx
!=
index
or
subindex
!=
subidx
:
raise
Runtime
Error
(
f
"
requested index
{
index
}
"
)
raise
EtherCat
Error
(
f
"
requested index
{
index
}
"
)
toggle
^=
0x10
async
def
read_ODlist
(
self
):
...
...
This diff is collapsed.
Click to expand it.
ebpfcat/scripts.py
+
13
−
9
View file @
f236798a
...
...
@@ -6,7 +6,7 @@ from pprint import PrettyPrinter
from
struct
import
unpack
import
sys
from
.ethercat
import
EtherCat
,
Terminal
,
ECCmd
from
.ethercat
import
EtherCat
,
Terminal
,
ECCmd
,
EtherCatError
def
entrypoint
(
func
):
@wraps
(
func
)
...
...
@@ -74,13 +74,17 @@ async def info():
for
k
,
v
in
ret
.
items
():
print
(
f
"
{
k
:
X
}
:
"
)
for
kk
,
vv
in
v
.
entries
.
items
():
print
(
f
"
{
kk
:
X
}
:
{
vv
}
"
)
if
args
.
values
:
r
=
await
vv
.
read
()
if
isinstance
(
r
,
int
):
print
(
f
"
{
r
:
10
}
{
r
:
8
X
}
"
)
else
:
print
(
f
"
{
r
}
"
)
print
(
f
"
{
kk
:
X
}
:
{
vv
}
"
)
if
args
.
values
:
try
:
r
=
await
vv
.
read
()
except
EtherCatError
as
e
:
print
(
f
"
Error
{
e
.
args
[
0
]
}
"
)
else
:
if
isinstance
(
r
,
int
):
print
(
f
"
{
r
:
10
}
{
r
:
8
X
}
"
)
else
:
print
(
f
"
{
r
}
"
)
if
args
.
pdo
:
await
t
.
to_operational
()
await
t
.
parse_pdos
()
...
...
@@ -162,7 +166,7 @@ async def create_test():
for
kk
,
vv
in
v
.
entries
.
items
():
try
:
ret
=
await
t
.
sdo_read
(
v
.
index
,
vv
.
valueInfo
)
except
Runtime
Error
:
except
EtherCat
Error
:
pass
sdo
[
v
.
index
,
vv
.
valueInfo
]
=
ret
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment