Skip to content

Commit 0c17400

Browse files
committed
Close device on timeout and retry
1 parent 7d9ee2d commit 0c17400

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

src/ignitech.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -134,25 +134,29 @@ IGN_async_status IGNITECH::read_async (ignitech_t& ignitech_data ) {
134134
static size_t total_read = 0;
135135
static size_t packet_size = IGNITECH_PACKET_SIZE_V88;
136136

137-
if ( time(NULL) > reset_last_read + reset_timeout ) {
138-
// No response for timeout, reset
137+
errno = 0; // Reset upon entry
138+
139+
if ( file_descriptor < 0 ) {
140+
// Bad file descriptor, reset
139141
if ( DEBUG_IGNITECH ) {
140-
perror ( "IGNITECH::read_async: No response from controller within timeout. Resetting.");
142+
perror ( "IGNITECH::read_async: Device not open. Resetting.");
141143
}
142-
// TODO it hasn't proved necessary, but it may be a good idea to close the port and reopen
143-
reset_last_read = time(NULL);
144144
reset();
145145
total_read = 0;
146146
found_header = false;
147147
sent_header = false;
148148
buf[0] = 0;
149149
}
150150

151-
if ( file_descriptor < 0 ) {
152-
// Bad file descriptor, reset
151+
if ( time(NULL) > reset_last_read + reset_timeout ) {
152+
// No response for timeout, reset
153153
if ( DEBUG_IGNITECH ) {
154-
perror ( "IGNITECH::read_async: Device not open. Resetting.");
154+
perror ( "IGNITECH::read_async: No response from controller within timeout. Resetting.");
155155
}
156+
// TODO it hasn't proved necessary, but it may be a good idea to close the port and reopen
157+
reset_last_read = time(NULL);
158+
close(file_descriptor);
159+
file_descriptor = -1;
156160
reset();
157161
total_read = 0;
158162
found_header = false;

0 commit comments

Comments
 (0)