ref: 2b08ecf97d835fc04aa539ee984a2fe03067f4f4
parent: 2783a8c62a74b253d5974d2aa7eba8c515adc88b
author: Jonne Kokkonen <jonne.kokkonen@gmail.com>
date: Wed Jul 6 10:45:39 EDT 2022
check device disconnect with a test write
--- a/main.c
+++ b/main.c
@@ -8,8 +8,6 @@
#include <SDL.h>
#include <libserialport.h>
#include <signal.h>
-#include <string.h>
-#include <unistd.h>
#include "command.h"
#include "config.h"
@@ -99,7 +97,8 @@
if (result == 1) {run = RUN;
} else {- SDL_LogCritical(SDL_LOG_CATEGORY_ERROR,"Device not detected on begin loop.");
+ SDL_LogCritical(SDL_LOG_CATEGORY_ERROR,
+ "Device not detected on begin loop.");
run = QUIT;
}
}
@@ -116,7 +115,7 @@
// get current inputs
input_msg_s input = get_input_msg(&conf);
if (input.type == special && input.value == msg_quit) {- SDL_LogCritical(SDL_LOG_CATEGORY_ERROR,"Input message QUIT.");
+ SDL_LogCritical(SDL_LOG_CATEGORY_ERROR, "Input message QUIT.");
run = QUIT;
}
@@ -138,7 +137,7 @@
run = RUN;
screensaver_destroy();
} else {- SDL_LogCritical(SDL_LOG_CATEGORY_ERROR,"Device not detected.");
+ SDL_LogCritical(SDL_LOG_CATEGORY_ERROR, "Device not detected.");
run = QUIT;
screensaver_destroy();
}
@@ -231,13 +230,20 @@
// zero byte packet, increment counter
zerobyte_packets++;
if (zerobyte_packets > conf.wait_packets) {- // i guess it can be assumed that the device has been disconnected
- zerobyte_packets = 0; // reset so we dont constantly reset the device if waiting
- run = WAIT_FOR_DEVICE;
- close_serial_port(port);
- port = NULL;
- // we'll make one more loop to see if the device is still there but just sending zero bytes
- // if it doesn't get detected when resetting the port, it will disconnect
+ zerobyte_packets = 0;
+
+ // try opening the serial port to check if it's alive
+ if (check_serial_port(port)) {+ // the device is still there, carry on
+ break;
+ } else {+ run = WAIT_FOR_DEVICE;
+ close_serial_port(port);
+ port = NULL;
+ /* we'll make one more loop to see if the device is still there
+ * but just sending zero bytes. if it doesn't get detected when
+ * resetting the port, it will disconnect */
+ }
}
break;
}
--- a/serial.c
+++ b/serial.c
@@ -30,6 +30,19 @@
return 0;
}
+int check_serial_port(struct sp_port *m8_port) {+ int buf[1] = {0x01};+ int result;
+ SDL_LogDebug(SDL_LOG_CATEGORY_SYSTEM, "Checking serial port");
+ result = sp_blocking_write(m8_port, buf, 1, 5);
+ if (result != 1) {+ SDL_LogDebug(SDL_LOG_CATEGORY_SYSTEM,
+ "Cannot send test packet to device, code %d", result);
+ return 0;
+ }
+ return 1;
+}
+
struct sp_port *init_serial(int verbose) {/* A pointer to a null-terminated array of pointers to
* struct sp_port, which will contain the ports found.*/
--- a/serial.h
+++ b/serial.h
@@ -7,5 +7,6 @@
#include <libserialport.h>
struct sp_port *init_serial(int verbose);
+int check_serial_port(struct sp_port *m8_port);
#endif
\ No newline at end of file
--
⑨