Add baud rate setting and some minor changes

This commit is contained in:
Steffen Lange 2019-04-19 14:39:18 +02:00
parent 7e8ba61b04
commit 13ffe0c2c7
10 changed files with 52 additions and 18 deletions

View file

@ -7,7 +7,7 @@ all: gclog
gclog: $(OBJECTS) gclog: $(OBJECTS)
$(CC) $(LDFLAGS) -o $@ $(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 %.o: %.c
$(CC) $(CFLAGS) -c $< $(CC) $(CFLAGS) -c $<

4
README
View file

@ -1,7 +1,7 @@
___ Geiger Counter LOGger ___ Geiger Counter LOGger
\_/ Version 0.2.4 \_/ Version 0.2.5
.--,O.--, .--,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 program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it This is free software, and you are welcome to redistribute it

View file

@ -3,7 +3,7 @@ Packaging Geiger Counter Logger
OpenWrt 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) 1. Download precompiled SDK (.tar.bz2 archive)
2. Copy openwrt/Makefile to <SDK>/package/gclog/ 2. Copy openwrt/Makefile to <SDK>/package/gclog/

View file

@ -1,6 +1,6 @@
#include "diygeiger.h" #include "diygeiger.h"
int diy_open(const char *device) { int diy_open(const char *device, speed_t baud) {
int fd = -1; int fd = -1;
struct termios tio; struct termios tio;
@ -10,7 +10,7 @@ int diy_open(const char *device) {
tio.c_cc[VTIME] = 5; tio.c_cc[VTIME] = 5;
if ((fd = open(device, O_RDWR)) != -1) { 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) if (tcsetattr(fd, TCSANOW, &tio) == 0)
// Return file descriptor, // Return file descriptor,
return fd; return fd;

View file

@ -12,7 +12,7 @@
Specification of GC10 Data I/O Interface: http://netiodev.com/GC10/GC10_UART_SPEC.pdf 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); void diy_close(int device);
int diy_get_cpm(int device); int diy_get_cpm(int device);
int diy_read(int device, char *buf, unsigned int len); int diy_read(int device, char *buf, unsigned int len);

View file

@ -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 .SH NAME
.B gclog .B gclog
@ -39,6 +39,12 @@ Serial device filename.
Example: \fI/dev/ttyUSB0\fP Example: \fI/dev/ttyUSB0\fP
.TP
.B device.baudrate
Serial port baud rate.
Example: \fI57600\fP
.TP .TP
.B location .B location
Name of your sensor location (required for Safecast). Name of your sensor location (required for Safecast).

26
gclog.c
View file

@ -6,6 +6,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <signal.h> #include <signal.h>
#include <string.h> #include <string.h>
#include <termios.h>
#include <time.h> #include <time.h>
#include <unistd.h> #include <unistd.h>
#include "logger.h" #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; int fd = -1;
time_t t = time(NULL); time_t t = time(NULL);
struct tm *tm = gmtime(&t); struct tm *tm = gmtime(&t);
if (type == GQ) { 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_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); gq_set_time(fd, tm->tm_hour, tm->tm_min, tm->tm_sec);
} }
} }
else if (type == DIY) { else if (type == DIY) {
fd = diy_open(device); fd = diy_open(device, baud);
} }
else if (type == SIM) { else if (type == SIM) {
fd = STDIN_FILENO; fd = STDIN_FILENO;
@ -193,6 +208,7 @@ int main(int argc, char *argv[]) {
unsigned int interval = 60; unsigned int interval = 60;
enum EGeiger device_type = SIM; enum EGeiger device_type = SIM;
char *device_port = NULL; char *device_port = NULL;
speed_t device_baudrate = B9600;
float latitude = 0.0, longitude = 0.0; float latitude = 0.0, longitude = 0.0;
char *location = NULL; char *location = NULL;
char *netc_id = NULL; char *netc_id = NULL;
@ -250,6 +266,8 @@ int main(int argc, char *argv[]) {
} }
if ((val = map_get(ini, "device.port")) != NULL) if ((val = map_get(ini, "device.port")) != NULL)
device_port = string_copy(val); 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) if ((val = map_get(ini, "latitude")) != NULL)
latitude = MIN(MAX(atof(val), -90.0), 90.0); latitude = MIN(MAX(atof(val), -90.0), 90.0);
if ((val = map_get(ini, "longitude")) != NULL) if ((val = map_get(ini, "longitude")) != NULL)
@ -283,7 +301,7 @@ int main(int argc, char *argv[]) {
int fd = -1; 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(stdin);
fclose(stdout); fclose(stdout);
fclose(stderr); fclose(stderr);

View file

@ -3,17 +3,26 @@
############################################ ############################################
# Geiger counter type: # Geiger counter type:
######################
# 'gq' -> GQ GMC Geiger Counter (GQ-RFC1201 protocol) # 'gq' -> GQ GMC Geiger Counter (GQ-RFC1201 protocol)
# 'diy' -> DIY/MyGeiger/NET-IO Geiger Kit or compatible # 'diy' -> DIY/MyGeiger/NET-IO Geiger Kit or compatible
######################################################## ########################################################
#device.type=diy #device.type=diy
#device.type=gq device.type=gq
device.type=sim
# Serial device filename # Serial device filename
######################## ########################
device.port=/dev/ttyUSB0 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 # GPS location of your sensor; required for Safecast
#################################################### ####################################################
location=Berlin, DE location=Berlin, DE

View file

@ -1,6 +1,6 @@
#include "gqgeiger.h" #include "gqgeiger.h"
int gq_open(const char *device) { int gq_open(const char *device, speed_t baud) {
int fd = -1; int fd = -1;
struct termios tio; struct termios tio;
@ -10,7 +10,7 @@ int gq_open(const char *device) {
tio.c_cc[VTIME] = 5; tio.c_cc[VTIME] = 5;
if ((fd = open(device, O_RDWR)) != -1) { 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) if (tcsetattr(fd, TCSANOW, &tio) == 0)
// Turn off heartbeat, // Turn off heartbeat,
if (gq_set_heartbeat_off(fd)) 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)) if (gq_write(device, cmd) == (ssize_t) strlen(cmd))
gq_read(device, buf, 2); 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) { bool gq_set_heartbeat_off(int device) {

View file

@ -12,7 +12,7 @@
GQ Geiger Counter Communication Protocol: http://www.gqelectronicsllc.com/download/GQ-RFC1201.txt 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); void gq_close(int device);
int gq_get_cpm(int device); int gq_get_cpm(int device);
bool gq_set_heartbeat_off(int device); bool gq_set_heartbeat_off(int device);