Skip to content

Commit

Permalink
Add optional parameter for a second NIC name for redundancy mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
bnjmnp committed Oct 22, 2023
1 parent 321f2f3 commit b81c5e8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
8 changes: 5 additions & 3 deletions examples/basic_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ class Device:


class BasicExample:
def __init__(self, ifname):
def __init__(self, ifname, ifname_red):
self._ifname = ifname
self._ifname_red = ifname_red
self._pd_thread_stop_event = threading.Event()
self._ch_thread_stop_event = threading.Event()
self._actual_wkc = 0
Expand Down Expand Up @@ -119,7 +120,7 @@ def _pdo_update_loop(self):
print("stopped")

def run(self):
self._master.open(self._ifname)
self._master.open(self._ifname, self._ifname_red)

if not self._master.config_init() > 0:
self._master.close()
Expand Down Expand Up @@ -226,10 +227,11 @@ def __init__(self, message):
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Example code for PySOEM.")
parser.add_argument("iface", type=str, help="ID of the network adapter used.")
parser.add_argument("ifname_red", nargs="?", type=str, help="Optional: ID of the second network adapter used (for redundancy).")
args = parser.parse_args()

try:
BasicExample(args.iface).run()
BasicExample(args.iface, args.ifname_red).run()
except BasicExampleError as err:
print(f"{os.path.basename(__file__)} failed: {err.message}")
sys.exit(1)
19 changes: 17 additions & 2 deletions pysoem/cpysoem.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ cdef extern from "ethercat.h":

DEF EC_MAXBUF = 16
DEF EC_MAXMBX = 1486
DEF EC_BUFSIZE = 1518

ec_adaptert* ec_find_adapters()

Expand All @@ -34,6 +35,8 @@ cdef extern from "ethercat.h":
ctypedef float float32
ctypedef double float64
ctypedef uint8 ec_mbxbuft[EC_MAXMBX]

ctypedef uint8 ec_bufT[EC_BUFSIZE]

ctypedef struct ec_timet:
uint32 sec
Expand Down Expand Up @@ -82,10 +85,21 @@ cdef extern from "ethercat.h":
# from nicdrv.h

ctypedef struct ec_stackT:
pass
int *sock
ec_bufT *(*txbuf) #[EC_MAXBUF]
int *(*txbuflength) #[EC_MAXBUF]
ec_bufT *tempbuf
ec_bufT *(*rxbuf) #[EC_MAXBUF]
int *(*rxbufstat) #[EC_MAXBUF]
int *(*rxsa) #[EC_MAXBUF]

ctypedef struct ecx_redportt:
pass
ec_stackT stack
int sockhandle
ec_bufT *rxbuf #[EC_MAXBUF]
int *rxbufstat #[EC_MAXBUF]
int *rxsa #[EC_MAXBUF]
ec_bufT tempinbuf

ctypedef struct ecx_portt:
pass
Expand Down Expand Up @@ -285,6 +299,7 @@ cdef extern from "ethercat.h":
char **Name #[EC_MAXOELIST][EC_MAXNAME+1]

int ecx_init(ecx_contextt* context, char* ifname)
int ecx_init_redundant(ecx_contextt *context, ecx_redportt *redport, const char *ifname, char *if2name)
void ecx_close(ecx_contextt *context)
int ecx_config_init(ecx_contextt *context, uint8 usetable)
int ecx_config_map_group(ecx_contextt *context, void *pIOmap, uint8 group)
Expand Down
9 changes: 7 additions & 2 deletions pysoem/pysoem.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -207,17 +207,22 @@ cdef class CdefMaster:
self._settings.sdo_read_timeout = &self.sdo_read_timeout
self._settings.sdo_write_timeout = &self.sdo_write_timeout

def open(self, ifname):
def open(self, ifname, ifname_red=None):
"""Initialize and open network interface.
Args:
ifname(str): Interface name. (see find_adapters)
ifname_red(:obj:`str`, optional): Interface name of the second network interface card for redundancy.
Put to None if not used.
Raises:
ConnectionError: When the specified interface dose not exist or
you have no permission to open the interface
"""
ret_val = cpysoem.ecx_init(&self._ecx_contextt, ifname.encode('utf8'))
if ifname_red is None:
ret_val = cpysoem.ecx_init(&self._ecx_contextt, ifname.encode('utf8'))
else:
ret_val = cpysoem.ecx_init_redundant(&self._ecx_contextt, &self._ecx_redport, ifname.encode('utf8'), ifname_red.encode('utf8'))
if ret_val == 0:
raise ConnectionError('could not open interface {}'.format(ifname))

Expand Down

0 comments on commit b81c5e8

Please sign in to comment.