diff --git a/Makefile b/Makefile index 6b3dfef..bd7d5a3 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ all: gclog gclog: $(OBJECTS) $(CC) $(LDFLAGS) -o $@ $(OBJECTS) - which upx >/dev/null 2>&1 && upx -9 -qq $@ || true +# which upx >/dev/null 2>&1 && upx -9 -qq $@ || true %.o: %.c $(CC) $(CFLAGS) -c $< diff --git a/README b/README index 698a6d4..7824811 100644 --- a/README +++ b/README @@ -1,7 +1,7 @@ ___ Geiger Counter LOGger - \_/ Version 0.2.4 + \_/ Version 0.2.5 .--,O.--, - \/ \/ Copyright (C) 2014-18 Steffen Lange, gclog@stelas.de + \/ \/ Copyright (C) 2014-19 Steffen Lange, gclog@stelas.de This program comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it diff --git a/README.Packaging b/README.Packaging index 9334681..1ea8ea4 100644 --- a/README.Packaging +++ b/README.Packaging @@ -3,7 +3,7 @@ Packaging Geiger Counter Logger OpenWrt ------- -Recipe from: https://openwrt.org/docs/guide-developer/obtain.firmware.sdk +Recipe from: https://openwrt.org/docs/guide-developer/using_the_sdk 1. Download precompiled SDK (.tar.bz2 archive) 2. Copy openwrt/Makefile to /package/gclog/ diff --git a/diygeiger.c b/diygeiger.c index c5f71c0..a9baa08 100644 --- a/diygeiger.c +++ b/diygeiger.c @@ -1,6 +1,6 @@ #include "diygeiger.h" -int diy_open(const char *device) { +int diy_open(const char *device, speed_t baud) { int fd = -1; struct termios tio; @@ -10,7 +10,7 @@ int diy_open(const char *device) { tio.c_cc[VTIME] = 5; if ((fd = open(device, O_RDWR)) != -1) { - if (cfsetspeed(&tio, B9600) == 0) // 9600 baud + if (cfsetspeed(&tio, baud) == 0) // usually 9600 baud if (tcsetattr(fd, TCSANOW, &tio) == 0) // Return file descriptor, return fd; diff --git a/diygeiger.h b/diygeiger.h index 8aaf91e..d5d3d88 100644 --- a/diygeiger.h +++ b/diygeiger.h @@ -12,7 +12,7 @@ Specification of GC10 Data I/O Interface: http://netiodev.com/GC10/GC10_UART_SPEC.pdf */ -int diy_open(const char *device); +int diy_open(const char *device, speed_t baud); void diy_close(int device); int diy_get_cpm(int device); int diy_read(int device, char *buf, unsigned int len); diff --git a/gclog.1 b/gclog.1 index 4b53485..28c8c55 100644 --- a/gclog.1 +++ b/gclog.1 @@ -1,4 +1,4 @@ -.TH GCLOG 1 "April 2018" "GCLOG 0.2" "Geiger Counter Logger" +.TH GCLOG 1 "April 2019" "GCLOG 0.2" "Geiger Counter Logger" .SH NAME .B gclog @@ -39,6 +39,12 @@ Serial device filename. Example: \fI/dev/ttyUSB0\fP +.TP +.B device.baudrate +Serial port baud rate. + +Example: \fI57600\fP + .TP .B location Name of your sensor location (required for Safecast). diff --git a/gclog.c b/gclog.c index b796d07..214df73 100644 --- a/gclog.c +++ b/gclog.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include "logger.h" @@ -60,19 +61,33 @@ void signal_handler(int sig) { } } -int geiger_open(enum EGeiger type, const char *device) { +int baud_rate(int bps) { + switch (bps) { + case 1200: return B1200; + case 2400: return B2400; + case 4800: return B4800; + case 9600: return B9600; + case 19200: return B19200; + case 38400: return B38400; + case 57600: return B57600; + case 115200: return B115200; + default: return -1; // Undefined baud rate + } +} + +int geiger_open(enum EGeiger type, const char *device, speed_t baud) { int fd = -1; time_t t = time(NULL); struct tm *tm = gmtime(&t); if (type == GQ) { - if ((fd = gq_open(device)) != -1) { + if ((fd = gq_open(device, baud)) != -1) { gq_set_date(fd, tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday); gq_set_time(fd, tm->tm_hour, tm->tm_min, tm->tm_sec); } } else if (type == DIY) { - fd = diy_open(device); + fd = diy_open(device, baud); } else if (type == SIM) { fd = STDIN_FILENO; @@ -193,6 +208,7 @@ int main(int argc, char *argv[]) { unsigned int interval = 60; enum EGeiger device_type = SIM; char *device_port = NULL; + speed_t device_baudrate = B9600; float latitude = 0.0, longitude = 0.0; char *location = NULL; char *netc_id = NULL; @@ -250,6 +266,8 @@ int main(int argc, char *argv[]) { } if ((val = map_get(ini, "device.port")) != NULL) device_port = string_copy(val); + if ((val = map_get(ini, "device.baudrate")) != NULL) + device_baudrate = baud_rate(atoi(val)); if ((val = map_get(ini, "latitude")) != NULL) latitude = MIN(MAX(atof(val), -90.0), 90.0); if ((val = map_get(ini, "longitude")) != NULL) @@ -283,7 +301,7 @@ int main(int argc, char *argv[]) { int fd = -1; - if (string_isset(device_port) && ((fd = geiger_open(device_type, device_port)) != -1)) { + if (string_isset(device_port) && ((fd = geiger_open(device_type, device_port, device_baudrate)) != -1)) { fclose(stdin); fclose(stdout); fclose(stderr); diff --git a/gclog.conf b/gclog.conf index e30362b..6fb9e56 100644 --- a/gclog.conf +++ b/gclog.conf @@ -3,17 +3,26 @@ ############################################ # Geiger counter type: +###################### # 'gq' -> GQ GMC Geiger Counter (GQ-RFC1201 protocol) # 'diy' -> DIY/MyGeiger/NET-IO Geiger Kit or compatible ######################################################## #device.type=diy -#device.type=gq -device.type=sim +device.type=gq # Serial device filename ######################## device.port=/dev/ttyUSB0 +# Serial port baud rate: +######################## +# DIY Geiger counter -> 9600 bps +# GMC-300 V3 and older -> 57600 bps +# GMC-300 Plus V4, GMC-320 -> 115200 bps +######################################### +#device.baudrate=9600 +device.baudrate=57600 + # GPS location of your sensor; required for Safecast #################################################### location=Berlin, DE diff --git a/gqgeiger.c b/gqgeiger.c index b19ac35..b875094 100644 --- a/gqgeiger.c +++ b/gqgeiger.c @@ -1,6 +1,6 @@ #include "gqgeiger.h" -int gq_open(const char *device) { +int gq_open(const char *device, speed_t baud) { int fd = -1; struct termios tio; @@ -10,7 +10,7 @@ int gq_open(const char *device) { tio.c_cc[VTIME] = 5; if ((fd = open(device, O_RDWR)) != -1) { - if (cfsetspeed(&tio, B57600) == 0) // 57600 baud + if (cfsetspeed(&tio, baud) == 0) // usually 57600 or 115200 baud if (tcsetattr(fd, TCSANOW, &tio) == 0) // Turn off heartbeat, if (gq_set_heartbeat_off(fd)) @@ -35,7 +35,8 @@ int gq_get_cpm(int device) { if (gq_write(device, cmd) == (ssize_t) strlen(cmd)) gq_read(device, buf, 2); - return buf[0] * 256 + buf[1]; + // Mask out upper 2 bits as mentioned in GQ-RFC1201. + return (buf[0] & 0x3f) * 256 + buf[1]; } bool gq_set_heartbeat_off(int device) { diff --git a/gqgeiger.h b/gqgeiger.h index 7e5b35a..03562ab 100644 --- a/gqgeiger.h +++ b/gqgeiger.h @@ -12,7 +12,7 @@ GQ Geiger Counter Communication Protocol: http://www.gqelectronicsllc.com/download/GQ-RFC1201.txt */ -int gq_open(const char *device); +int gq_open(const char *device, speed_t baud); void gq_close(int device); int gq_get_cpm(int device); bool gq_set_heartbeat_off(int device);