Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support sub-second timeouts #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions canopen/can-if.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,17 @@ can_socket_open(char *interface)

int
can_socket_open_timeout(char *interface, unsigned int timeout_sec)
{
struct timeval tv= {timeout_sec,0};

return can_socket_open_timeval(interface, tv);
}

int
can_socket_open_timeval(char *interface, struct timeval tv)
{
struct sockaddr_can addr;
struct ifreq ifr;
struct timeval tv= {timeout_sec,0};
int sock, bytes_read;

// create CAN socket
Expand All @@ -49,9 +56,9 @@ can_socket_open_timeout(char *interface, unsigned int timeout_sec)
fprintf(stderr, "Error: Failed to create socket.\n");
return -1;
}
// set a timeoute for read
if (timeout_sec != 0)

// set a timeout for read
if (tv.tv_sec != 0 || tv.tv_usec != 0)
{
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv));
}
Expand All @@ -62,11 +69,10 @@ can_socket_open_timeout(char *interface, unsigned int timeout_sec)
addr.can_family = AF_CAN;
addr.can_ifindex = ifr.ifr_ifindex;
bind(sock, (struct sockaddr*)&addr, sizeof(addr)); // XXX Add check

return sock;
}


int
can_socket_close(int socket)
{
Expand Down
1 change: 1 addition & 0 deletions canopen/can-if.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

int can_socket_open(char *interface);
int can_socket_open_timeout(char *interface, unsigned int timeout_sec);
int can_socket_open_timeval(char *interface, struct timeval timeout);
int can_socket_close(int socket);

int can_filter_node_set(int socket, uint8_t node);
Expand Down
14 changes: 12 additions & 2 deletions python/pycanopen/CANopen.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,22 +88,32 @@ def __str__(self):
data_str = " ".join(["%.2x" % (x,) for x in self.data.data])
return "CANopen Frame: RTR=%d FC=0x%.2x ID=0x%.2x [len=%d] %s" % (self.rtr, self.function_code, self.id, self.data_len, data_str)

class timeval(Structure):
_fields_ = [("tv_sec", c_long),
("tv_usec", c_long)]

class CANopen:

def __init__(self, interface="can0", timeout_sec=0):
"""
Constructor for CANopen class. Optionally takes an interface
name for which to bind a socket to. Defaults to interface "can0"
"""
self.sock = libcanopen.can_socket_open_timeout(interface.encode('ascii'), timeout_sec)
tv = timeval()
tv.tv_sec = int(timeout_sec)
tv.tv_usec = int((timeout_sec - tv.tv_sec) * 1e06)
self.sock = libcanopen.can_socket_open_timeval(interface.encode('ascii'), tv)

def open(self, interface, timeout_sec=0):
"""
Open a new socket. If open socket already exist, close it first.
"""
if self.sock:
self.close()
self.sock = libcanopen.can_socket_open_timeout(interface.encode('ascii'), timeout_sec)
tv = timeval()
tv.tv_sec = int(timeout_sec)
tv.tv_usec = int((timeout_sec - tv.tv_sec) * 1e06)
self.sock = libcanopen.can_socket_open_timeval(interface.encode('ascii'), tv)

def close(self):
"""
Expand Down