mirror of
https://github.com/eddyem/eddys_snippets.git
synced 2025-12-06 02:35:12 +03:00
Compare commits
2 Commits
aa9165b323
...
ce15889295
| Author | SHA1 | Date | |
|---|---|---|---|
| ce15889295 | |||
| 7f2ff12c90 |
59
modbus_relay/Makefile
Normal file
59
modbus_relay/Makefile
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
# run `make DEF=...` to add extra defines
|
||||||
|
PROGRAM := modbus_relay
|
||||||
|
LDFLAGS := -fdata-sections -ffunction-sections -Wl,--gc-sections -Wl,--discard-all
|
||||||
|
LDFLAGS += -lusefull_macros -lmodbus
|
||||||
|
SRCS := $(wildcard *.c)
|
||||||
|
DEFINES := $(DEF) -D_GNU_SOURCE -D_XOPEN_SOURCE=1111
|
||||||
|
OBJDIR := mk
|
||||||
|
CFLAGS += -O2 -Wall -Wextra -Wno-trampolines
|
||||||
|
OBJS := $(addprefix $(OBJDIR)/, $(SRCS:%.c=%.o))
|
||||||
|
DEPS := $(OBJS:.o=.d)
|
||||||
|
TARGFILE := $(OBJDIR)/TARGET
|
||||||
|
CC = gcc
|
||||||
|
#TARGET := RELEASE
|
||||||
|
|
||||||
|
ifeq ($(shell test -e $(TARGFILE) && echo -n yes),yes)
|
||||||
|
TARGET := $(file < $(TARGFILE))
|
||||||
|
else
|
||||||
|
TARGET := RELEASE
|
||||||
|
endif
|
||||||
|
|
||||||
|
ifeq ($(TARGET), DEBUG)
|
||||||
|
.DEFAULT_GOAL := debug
|
||||||
|
endif
|
||||||
|
|
||||||
|
release: CFLAGS += -flto
|
||||||
|
release: LDFLAGS += -flto
|
||||||
|
release: $(PROGRAM)
|
||||||
|
|
||||||
|
debug: CFLAGS += -DEBUG -Werror
|
||||||
|
debug: TARGET := DEBUG
|
||||||
|
debug: $(PROGRAM)
|
||||||
|
|
||||||
|
$(TARGFILE): $(OBJDIR)
|
||||||
|
@echo -e "\t\tTARGET: $(TARGET)"
|
||||||
|
@echo "$(TARGET)" > $(TARGFILE)
|
||||||
|
|
||||||
|
$(PROGRAM) : $(TARGFILE) $(OBJS)
|
||||||
|
@echo -e "\t\tLD $(PROGRAM)"
|
||||||
|
$(CC) $(LDFLAGS) $(OBJS) -o $(PROGRAM)
|
||||||
|
|
||||||
|
$(OBJDIR):
|
||||||
|
@mkdir $(OBJDIR)
|
||||||
|
|
||||||
|
ifneq ($(MAKECMDGOALS),clean)
|
||||||
|
-include $(DEPS)
|
||||||
|
endif
|
||||||
|
|
||||||
|
$(OBJDIR)/%.o: %.c
|
||||||
|
@echo -e "\t\tCC $<"
|
||||||
|
$(CC) -MD -c $(LDFLAGS) $(CFLAGS) $(DEFINES) -o $@ $<
|
||||||
|
|
||||||
|
clean:
|
||||||
|
@echo -e "\t\tCLEAN"
|
||||||
|
@rm -rf $(OBJDIR) 2>/dev/null || true
|
||||||
|
|
||||||
|
xclean: clean
|
||||||
|
@rm -f $(PROGRAM)
|
||||||
|
|
||||||
|
.PHONY: clean xclean
|
||||||
89
modbus_relay/cmdlnopts.c
Normal file
89
modbus_relay/cmdlnopts.c
Normal file
@ -0,0 +1,89 @@
|
|||||||
|
/* geany_encoding=koi8-r
|
||||||
|
* cmdlnopts.c - the only function that parse cmdln args and returns glob parameters
|
||||||
|
*
|
||||||
|
* Copyright 2013 Edward V. Emelianoff <eddy@sao.ru>
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation; either version 2 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||||
|
* MA 02110-1301, USA.
|
||||||
|
*/
|
||||||
|
#include <assert.h>
|
||||||
|
#include <stdarg.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <strings.h>
|
||||||
|
#include "cmdlnopts.h"
|
||||||
|
#include "usefull_macros.h"
|
||||||
|
|
||||||
|
/*
|
||||||
|
* here are global parameters initialisation
|
||||||
|
*/
|
||||||
|
static int help;
|
||||||
|
sl_loglevel_e verblvl = LOGLEVEL_WARN;
|
||||||
|
|
||||||
|
glob_pars GP = {
|
||||||
|
.baudrate = 9600,
|
||||||
|
.device = "/dev/ttyUSB0",
|
||||||
|
.nodenum = 1,
|
||||||
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Define command line options by filling structure:
|
||||||
|
* name has_arg flag val type argptr help
|
||||||
|
*/
|
||||||
|
static sl_option_t cmdlnopts[] = {
|
||||||
|
{"help", NO_ARGS, NULL, 'h', arg_int, APTR(&help), "show this help"},
|
||||||
|
{"verbose", NO_ARGS, NULL, 'v', arg_none, APTR(&GP.verbose), "verbose level for (each `-v` increase it)"},
|
||||||
|
{"logfile", NEED_ARG, NULL, 'l', arg_string, APTR(&GP.logfile), "logging file name"},
|
||||||
|
{"baudrate",NEED_ARG, NULL, 'b', arg_int, APTR(&GP.baudrate), "interface baudrate (default: 9600)"},
|
||||||
|
{"device", NEED_ARG, NULL, 'd', arg_string, APTR(&GP.device), "serial device path (default: /dev/ttyUSB0)"},
|
||||||
|
{"set", MULT_PAR, NULL, 's', arg_int, APTR(&GP.setrelay), "set Nth relay"},
|
||||||
|
{"reset", MULT_PAR, NULL, 'r', arg_int, APTR(&GP.resetrelay),"reset Nth relay"},
|
||||||
|
{"input", MULT_PAR, NULL, 'i', arg_int, APTR(&GP.getinput), "read Nth input"},
|
||||||
|
{"get", MULT_PAR, NULL, 'g', arg_int, APTR(&GP.getrelay), "reat Nth relay"},
|
||||||
|
{"setall", NO_ARGS, NULL, 'S', arg_int, APTR(&GP.setall), "set all relays"},
|
||||||
|
{"resetall",NO_ARGS, NULL, 'R', arg_int, APTR(&GP.resetall), "reset all relays"},
|
||||||
|
{"node", NEED_ARG, NULL, 'n', arg_int, APTR(&GP.nodenum), "node number (default: 1)"},
|
||||||
|
{"readN", NO_ARGS, NULL, 0, arg_int, APTR(&GP.readn), "read node number"},
|
||||||
|
{"setN", NEED_ARG, NULL, 0, arg_int, APTR(&GP.setn), "change node number"},
|
||||||
|
end_option
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse command line options and return dynamically allocated structure
|
||||||
|
* to global parameters
|
||||||
|
* @param argc - copy of argc from main
|
||||||
|
* @param argv - copy of argv from main
|
||||||
|
* @return allocated structure with global parameters
|
||||||
|
*/
|
||||||
|
void parse_args(int argc, char **argv){
|
||||||
|
size_t hlen = 1024;
|
||||||
|
char helpstring[1024], *hptr = helpstring;
|
||||||
|
snprintf(hptr, hlen, "Usage: %%s [args]\n\n\tWhere args are:\n");
|
||||||
|
// format of help: "Usage: progname [args]\n"
|
||||||
|
sl_helpstring(helpstring);
|
||||||
|
// parse arguments
|
||||||
|
sl_parseargs(&argc, &argv, cmdlnopts);
|
||||||
|
if(help) sl_showhelp(-1, cmdlnopts);
|
||||||
|
if(argc > 0) WARNX("Omit %d unexpected arguments", argc);
|
||||||
|
}
|
||||||
|
|
||||||
|
void verbose(sl_loglevel_e lvl, const char *fmt, ...){
|
||||||
|
if(lvl > verblvl) return;
|
||||||
|
va_list ar;
|
||||||
|
va_start(ar, fmt);
|
||||||
|
vprintf(fmt, ar);
|
||||||
|
va_end(ar);
|
||||||
|
fflush(stdout);
|
||||||
|
}
|
||||||
49
modbus_relay/cmdlnopts.h
Normal file
49
modbus_relay/cmdlnopts.h
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of the modbus_relay project.
|
||||||
|
* Copyright 2025 Edward V. Emelianov <edward.emelianoff@gmail.com>.
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <usefull_macros.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
* here are some typedef's for global data
|
||||||
|
*/
|
||||||
|
typedef struct{
|
||||||
|
int verbose; // verbose level
|
||||||
|
int baudrate; // interface baudrate (default: 9600)
|
||||||
|
int setall; // set all relays
|
||||||
|
int resetall; // reset all relays
|
||||||
|
int nodenum; // node number (default: 1)
|
||||||
|
int readn; // read node number
|
||||||
|
int setn; // set node number
|
||||||
|
int **setrelay; // set relay number N
|
||||||
|
int **resetrelay; // reset relay number N
|
||||||
|
int **getinput; // read Nth input
|
||||||
|
int **getrelay; // read Nth relay
|
||||||
|
char *logfile; // logfile name
|
||||||
|
char *device; // serial device path (default: /dev/ttyUSB0)
|
||||||
|
} glob_pars;
|
||||||
|
|
||||||
|
extern glob_pars GP;
|
||||||
|
extern sl_loglevel_e verblvl;
|
||||||
|
void parse_args(int argc, char **argv);
|
||||||
|
void verbose(sl_loglevel_e lvl, const char *fmt, ...);
|
||||||
|
|
||||||
|
#define VMSG(...) verbose(LOGLEVEL_MSG, __VA_ARGS__)
|
||||||
|
#define VDBG(...) verbose(LOGLEVEL_DBG, __VA_ARGS__)
|
||||||
|
#define VANY(...) verbose(LOGLEVEL_ANY, __VA_ARGS__)
|
||||||
120
modbus_relay/main.c
Normal file
120
modbus_relay/main.c
Normal file
@ -0,0 +1,120 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of the modbus_relay project.
|
||||||
|
* Copyright 2025 Edward V. Emelianov <edward.emelianoff@gmail.com>.
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <modbus/modbus.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "cmdlnopts.h"
|
||||||
|
|
||||||
|
static void getN(modbus_t *ctx){
|
||||||
|
uint16_t nodeN;
|
||||||
|
int s = modbus_get_slave(ctx);
|
||||||
|
VDBG("Old node num: %d\n", s);
|
||||||
|
if(modbus_set_slave(ctx, 0)) ERRX("Can't set modbus slave");
|
||||||
|
printf("now slave id=%d\n", modbus_get_slave(ctx));
|
||||||
|
if(modbus_read_registers(ctx, 0, 1, &nodeN) < 0) WARNX("Can't read node number");
|
||||||
|
if(modbus_set_slave(ctx, s)) ERRX("Can't set modbus slave");
|
||||||
|
else printf("NODENUM=%u\n", nodeN);
|
||||||
|
printf("now slave id=%d\n", modbus_get_slave(ctx));
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char **argv){
|
||||||
|
sl_init();
|
||||||
|
parse_args(argc, argv);
|
||||||
|
verblvl = GP.verbose + LOGLEVEL_WARN;
|
||||||
|
if(verblvl >= LOGLEVEL_AMOUNT) verblvl = LOGLEVEL_AMOUNT - 1;
|
||||||
|
if(GP.logfile) OPENLOG(GP.logfile, verblvl, 1);
|
||||||
|
LOGMSG("Started");
|
||||||
|
VMSG("Try to open %s @%d ... ", GP.device, GP.baudrate);
|
||||||
|
modbus_t *ctx = modbus_new_rtu(GP.device, GP.baudrate, 'N', 8, 1);
|
||||||
|
modbus_set_response_timeout(ctx, 0, 100000);
|
||||||
|
if(modbus_set_slave(ctx, GP.nodenum)) ERRX("Can't set modbus slave");
|
||||||
|
if(modbus_connect(ctx) < 0) ERR("Can't open device %s", GP.device);
|
||||||
|
VMSG("OK!\n");
|
||||||
|
uint8_t dest8[8] = {0};
|
||||||
|
if(GP.setall){
|
||||||
|
memset(dest8, 1, 8);
|
||||||
|
if(modbus_write_bits(ctx, 0, 8, dest8) < 0) WARNX("Can't set all relays");
|
||||||
|
}else if(GP.resetall){
|
||||||
|
if(modbus_write_bits(ctx, 0, 8, dest8) < 0) WARNX("Can't clear all relays");
|
||||||
|
}else{
|
||||||
|
if(GP.resetrelay){
|
||||||
|
int **p = GP.resetrelay;
|
||||||
|
while(*p){
|
||||||
|
int n = **p;
|
||||||
|
if(n > 7 || n < 0) WARNX("Relay number should be in [0, 7]");
|
||||||
|
else{
|
||||||
|
if(modbus_write_bit(ctx, n, 0) < 0) WARNX("Can't reset relay #%d", n);
|
||||||
|
else VMSG("RELAY%d=0\n", n);
|
||||||
|
}
|
||||||
|
++p;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(GP.setrelay){
|
||||||
|
int **p = GP.setrelay;
|
||||||
|
while(*p){
|
||||||
|
int n = **p;
|
||||||
|
if(n > 7 || n < 0) WARNX("Relay number should be in [0, 7]");
|
||||||
|
else{
|
||||||
|
if(modbus_write_bit(ctx, n, 1) < 0) WARNX("Can't set relay #%d", n);
|
||||||
|
else VMSG("RELAY%d=1\n", n);
|
||||||
|
}
|
||||||
|
++p;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(GP.getinput){
|
||||||
|
if(modbus_read_input_bits(ctx, 0, 8, dest8) < 0) WARNX("Can't read inputs");
|
||||||
|
else{
|
||||||
|
int **p = GP.getinput;
|
||||||
|
while(*p){
|
||||||
|
int n = **p;
|
||||||
|
if(n > 7 || n < 0) WARNX("Input number should be in [0, 7]");
|
||||||
|
else printf("INPUT%d=%u\n", n, dest8[n]);
|
||||||
|
++p;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(GP.getrelay){
|
||||||
|
if(modbus_read_bits(ctx, 0, 8, dest8) < 0) WARNX("Can't read relays");
|
||||||
|
else{
|
||||||
|
int **p = GP.getrelay;
|
||||||
|
while(*p){
|
||||||
|
int n = **p;
|
||||||
|
if(n > 7 || n < 0) WARNX("Relay number should be in [0, 7]");
|
||||||
|
else printf("RELAY%d=%u\n", n, dest8[n]);
|
||||||
|
++p;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(GP.readn){
|
||||||
|
getN(ctx);
|
||||||
|
}
|
||||||
|
if(GP.setn){
|
||||||
|
uint16_t nodeN = (uint16_t) GP.setn;
|
||||||
|
modbus_write_registers(ctx, 0, 1, &nodeN);
|
||||||
|
modbus_set_slave(ctx, nodeN);
|
||||||
|
getN(ctx);
|
||||||
|
}
|
||||||
|
LOGMSG("End");
|
||||||
|
VMSG("End\n");
|
||||||
|
modbus_close(ctx);
|
||||||
|
modbus_free(ctx);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
BIN
modbus_relay/modbus_relay
Executable file
BIN
modbus_relay/modbus_relay
Executable file
Binary file not shown.
1
modbus_relay/modbus_relay.cflags
Normal file
1
modbus_relay/modbus_relay.cflags
Normal file
@ -0,0 +1 @@
|
|||||||
|
-std=c17
|
||||||
2
modbus_relay/modbus_relay.config
Normal file
2
modbus_relay/modbus_relay.config
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
// Add predefined macros for your project here. For example:
|
||||||
|
// #define THE_ANSWER 42
|
||||||
1
modbus_relay/modbus_relay.creator
Normal file
1
modbus_relay/modbus_relay.creator
Normal file
@ -0,0 +1 @@
|
|||||||
|
[General]
|
||||||
184
modbus_relay/modbus_relay.creator.user
Normal file
184
modbus_relay/modbus_relay.creator.user
Normal file
@ -0,0 +1,184 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE QtCreatorProject>
|
||||||
|
<!-- Written by QtCreator 16.0.1, 2025-05-22T17:51:51. -->
|
||||||
|
<qtcreator>
|
||||||
|
<data>
|
||||||
|
<variable>EnvironmentId</variable>
|
||||||
|
<value type="QByteArray">{cf63021e-ef53-49b0-b03b-2f2570cdf3b6}</value>
|
||||||
|
</data>
|
||||||
|
<data>
|
||||||
|
<variable>ProjectExplorer.Project.ActiveTarget</variable>
|
||||||
|
<value type="qlonglong">0</value>
|
||||||
|
</data>
|
||||||
|
<data>
|
||||||
|
<variable>ProjectExplorer.Project.EditorSettings</variable>
|
||||||
|
<valuemap type="QVariantMap">
|
||||||
|
<value type="bool" key="EditorConfiguration.AutoDetect">true</value>
|
||||||
|
<value type="bool" key="EditorConfiguration.AutoIndent">true</value>
|
||||||
|
<value type="bool" key="EditorConfiguration.CamelCaseNavigation">true</value>
|
||||||
|
<valuemap type="QVariantMap" key="EditorConfiguration.CodeStyle.0">
|
||||||
|
<value type="QString" key="language">Cpp</value>
|
||||||
|
<valuemap type="QVariantMap" key="value">
|
||||||
|
<value type="QByteArray" key="CurrentPreferences">CppGlobal</value>
|
||||||
|
</valuemap>
|
||||||
|
</valuemap>
|
||||||
|
<valuemap type="QVariantMap" key="EditorConfiguration.CodeStyle.1">
|
||||||
|
<value type="QString" key="language">QmlJS</value>
|
||||||
|
<valuemap type="QVariantMap" key="value">
|
||||||
|
<value type="QByteArray" key="CurrentPreferences">QmlJSGlobal</value>
|
||||||
|
</valuemap>
|
||||||
|
</valuemap>
|
||||||
|
<value type="qlonglong" key="EditorConfiguration.CodeStyle.Count">2</value>
|
||||||
|
<value type="QByteArray" key="EditorConfiguration.Codec">KOI8-R</value>
|
||||||
|
<value type="bool" key="EditorConfiguration.ConstrainTooltips">false</value>
|
||||||
|
<value type="int" key="EditorConfiguration.IndentSize">4</value>
|
||||||
|
<value type="bool" key="EditorConfiguration.KeyboardTooltips">false</value>
|
||||||
|
<value type="int" key="EditorConfiguration.LineEndingBehavior">0</value>
|
||||||
|
<value type="int" key="EditorConfiguration.MarginColumn">80</value>
|
||||||
|
<value type="bool" key="EditorConfiguration.MouseHiding">true</value>
|
||||||
|
<value type="bool" key="EditorConfiguration.MouseNavigation">true</value>
|
||||||
|
<value type="int" key="EditorConfiguration.PaddingMode">1</value>
|
||||||
|
<value type="int" key="EditorConfiguration.PreferAfterWhitespaceComments">0</value>
|
||||||
|
<value type="bool" key="EditorConfiguration.PreferSingleLineComments">false</value>
|
||||||
|
<value type="bool" key="EditorConfiguration.ScrollWheelZooming">false</value>
|
||||||
|
<value type="bool" key="EditorConfiguration.ShowMargin">false</value>
|
||||||
|
<value type="int" key="EditorConfiguration.SmartBackspaceBehavior">1</value>
|
||||||
|
<value type="bool" key="EditorConfiguration.SmartSelectionChanging">true</value>
|
||||||
|
<value type="bool" key="EditorConfiguration.SpacesForTabs">true</value>
|
||||||
|
<value type="int" key="EditorConfiguration.TabKeyBehavior">0</value>
|
||||||
|
<value type="int" key="EditorConfiguration.TabSize">8</value>
|
||||||
|
<value type="bool" key="EditorConfiguration.UseGlobal">true</value>
|
||||||
|
<value type="bool" key="EditorConfiguration.UseIndenter">false</value>
|
||||||
|
<value type="int" key="EditorConfiguration.Utf8BomBehavior">1</value>
|
||||||
|
<value type="bool" key="EditorConfiguration.addFinalNewLine">true</value>
|
||||||
|
<value type="bool" key="EditorConfiguration.cleanIndentation">true</value>
|
||||||
|
<value type="bool" key="EditorConfiguration.cleanWhitespace">true</value>
|
||||||
|
<value type="QString" key="EditorConfiguration.ignoreFileTypes">*.md, *.MD, Makefile</value>
|
||||||
|
<value type="bool" key="EditorConfiguration.inEntireDocument">true</value>
|
||||||
|
<value type="bool" key="EditorConfiguration.skipTrailingWhitespace">true</value>
|
||||||
|
<value type="bool" key="EditorConfiguration.tintMarginArea">true</value>
|
||||||
|
</valuemap>
|
||||||
|
</data>
|
||||||
|
<data>
|
||||||
|
<variable>ProjectExplorer.Project.PluginSettings</variable>
|
||||||
|
<valuemap type="QVariantMap">
|
||||||
|
<valuemap type="QVariantMap" key="AutoTest.ActiveFrameworks">
|
||||||
|
<value type="bool" key="AutoTest.Framework.Boost">true</value>
|
||||||
|
<value type="bool" key="AutoTest.Framework.CTest">false</value>
|
||||||
|
<value type="bool" key="AutoTest.Framework.Catch">true</value>
|
||||||
|
<value type="bool" key="AutoTest.Framework.GTest">true</value>
|
||||||
|
<value type="bool" key="AutoTest.Framework.QtQuickTest">true</value>
|
||||||
|
<value type="bool" key="AutoTest.Framework.QtTest">true</value>
|
||||||
|
</valuemap>
|
||||||
|
<value type="bool" key="AutoTest.ApplyFilter">false</value>
|
||||||
|
<valuemap type="QVariantMap" key="AutoTest.CheckStates"/>
|
||||||
|
<valuelist type="QVariantList" key="AutoTest.PathFilters"/>
|
||||||
|
<value type="int" key="AutoTest.RunAfterBuild">0</value>
|
||||||
|
<value type="bool" key="AutoTest.UseGlobal">true</value>
|
||||||
|
<valuemap type="QVariantMap" key="ClangTools">
|
||||||
|
<value type="bool" key="ClangTools.AnalyzeOpenFiles">true</value>
|
||||||
|
<value type="bool" key="ClangTools.BuildBeforeAnalysis">true</value>
|
||||||
|
<value type="QString" key="ClangTools.DiagnosticConfig">Builtin.DefaultTidyAndClazy</value>
|
||||||
|
<value type="int" key="ClangTools.ParallelJobs">4</value>
|
||||||
|
<value type="bool" key="ClangTools.PreferConfigFile">true</value>
|
||||||
|
<valuelist type="QVariantList" key="ClangTools.SelectedDirs"/>
|
||||||
|
<valuelist type="QVariantList" key="ClangTools.SelectedFiles"/>
|
||||||
|
<valuelist type="QVariantList" key="ClangTools.SuppressedDiagnostics"/>
|
||||||
|
<value type="bool" key="ClangTools.UseGlobalSettings">true</value>
|
||||||
|
</valuemap>
|
||||||
|
</valuemap>
|
||||||
|
</data>
|
||||||
|
<data>
|
||||||
|
<variable>ProjectExplorer.Project.Target.0</variable>
|
||||||
|
<valuemap type="QVariantMap">
|
||||||
|
<value type="QString" key="DeviceType">Desktop</value>
|
||||||
|
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Desktop</value>
|
||||||
|
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Desktop</value>
|
||||||
|
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">{91347f2c-5221-46a7-80b1-0a054ca02f79}</value>
|
||||||
|
<value type="qlonglong" key="ProjectExplorer.Target.ActiveBuildConfiguration">0</value>
|
||||||
|
<value type="qlonglong" key="ProjectExplorer.Target.ActiveDeployConfiguration">0</value>
|
||||||
|
<value type="qlonglong" key="ProjectExplorer.Target.ActiveRunConfiguration">0</value>
|
||||||
|
<valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.0">
|
||||||
|
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">/home/eddy/Docs/SAO/ELECTRONICS/Modbus/modbus_relay</value>
|
||||||
|
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
|
||||||
|
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
|
||||||
|
<valuelist type="QVariantList" key="GenericProjectManager.GenericMakeStep.BuildTargets">
|
||||||
|
<value type="QString">all</value>
|
||||||
|
</valuelist>
|
||||||
|
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
|
||||||
|
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">GenericProjectManager.GenericMakeStep</value>
|
||||||
|
</valuemap>
|
||||||
|
<value type="qlonglong" key="ProjectExplorer.BuildStepList.StepsCount">1</value>
|
||||||
|
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Сборка</value>
|
||||||
|
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Сборка</value>
|
||||||
|
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.BuildSteps.Build</value>
|
||||||
|
</valuemap>
|
||||||
|
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.1">
|
||||||
|
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
|
||||||
|
<valuelist type="QVariantList" key="GenericProjectManager.GenericMakeStep.BuildTargets">
|
||||||
|
<value type="QString">clean</value>
|
||||||
|
</valuelist>
|
||||||
|
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
|
||||||
|
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">GenericProjectManager.GenericMakeStep</value>
|
||||||
|
</valuemap>
|
||||||
|
<value type="qlonglong" key="ProjectExplorer.BuildStepList.StepsCount">1</value>
|
||||||
|
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Очистка</value>
|
||||||
|
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Очистка</value>
|
||||||
|
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.BuildSteps.Clean</value>
|
||||||
|
</valuemap>
|
||||||
|
<value type="int" key="ProjectExplorer.BuildConfiguration.BuildStepListCount">2</value>
|
||||||
|
<value type="bool" key="ProjectExplorer.BuildConfiguration.ClearSystemEnvironment">false</value>
|
||||||
|
<valuelist type="QVariantList" key="ProjectExplorer.BuildConfiguration.CustomParsers"/>
|
||||||
|
<value type="bool" key="ProjectExplorer.BuildConfiguration.ParseStandardOutput">false</value>
|
||||||
|
<valuelist type="QVariantList" key="ProjectExplorer.BuildConfiguration.UserEnvironmentChanges"/>
|
||||||
|
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">По умолчанию</value>
|
||||||
|
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">GenericProjectManager.GenericBuildConfiguration</value>
|
||||||
|
</valuemap>
|
||||||
|
<value type="qlonglong" key="ProjectExplorer.Target.BuildConfigurationCount">1</value>
|
||||||
|
<valuemap type="QVariantMap" key="ProjectExplorer.Target.DeployConfiguration.0">
|
||||||
|
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
|
||||||
|
<value type="qlonglong" key="ProjectExplorer.BuildStepList.StepsCount">0</value>
|
||||||
|
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Развёртывание</value>
|
||||||
|
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Развёртывание</value>
|
||||||
|
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.BuildSteps.Deploy</value>
|
||||||
|
</valuemap>
|
||||||
|
<value type="int" key="ProjectExplorer.BuildConfiguration.BuildStepListCount">1</value>
|
||||||
|
<valuemap type="QVariantMap" key="ProjectExplorer.DeployConfiguration.CustomData"/>
|
||||||
|
<value type="bool" key="ProjectExplorer.DeployConfiguration.CustomDataEnabled">false</value>
|
||||||
|
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.DefaultDeployConfiguration</value>
|
||||||
|
</valuemap>
|
||||||
|
<value type="qlonglong" key="ProjectExplorer.Target.DeployConfigurationCount">1</value>
|
||||||
|
<valuemap type="QVariantMap" key="ProjectExplorer.Target.RunConfiguration.0">
|
||||||
|
<value type="bool" key="Analyzer.Perf.Settings.UseGlobalSettings">true</value>
|
||||||
|
<value type="bool" key="Analyzer.QmlProfiler.Settings.UseGlobalSettings">true</value>
|
||||||
|
<value type="int" key="Analyzer.Valgrind.Callgrind.CostFormat">0</value>
|
||||||
|
<value type="bool" key="Analyzer.Valgrind.Settings.UseGlobalSettings">true</value>
|
||||||
|
<value type="QList<int>" key="Analyzer.Valgrind.VisibleErrorKinds"></value>
|
||||||
|
<valuelist type="QVariantList" key="CustomOutputParsers"/>
|
||||||
|
<value type="int" key="PE.EnvironmentAspect.Base">2</value>
|
||||||
|
<valuelist type="QVariantList" key="PE.EnvironmentAspect.Changes"/>
|
||||||
|
<value type="bool" key="PE.EnvironmentAspect.PrintOnRun">false</value>
|
||||||
|
<value type="QString" key="PerfRecordArgsId">-e cpu-cycles --call-graph dwarf,4096 -F 250</value>
|
||||||
|
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName"></value>
|
||||||
|
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.CustomExecutableRunConfiguration</value>
|
||||||
|
<value type="QString" key="ProjectExplorer.RunConfiguration.BuildKey"></value>
|
||||||
|
<value type="bool" key="ProjectExplorer.RunConfiguration.Customized">false</value>
|
||||||
|
<value type="bool" key="RunConfiguration.UseCppDebuggerAuto">true</value>
|
||||||
|
<value type="bool" key="RunConfiguration.UseQmlDebuggerAuto">true</value>
|
||||||
|
</valuemap>
|
||||||
|
<value type="qlonglong" key="ProjectExplorer.Target.RunConfigurationCount">1</value>
|
||||||
|
</valuemap>
|
||||||
|
</data>
|
||||||
|
<data>
|
||||||
|
<variable>ProjectExplorer.Project.TargetCount</variable>
|
||||||
|
<value type="qlonglong">1</value>
|
||||||
|
</data>
|
||||||
|
<data>
|
||||||
|
<variable>ProjectExplorer.Project.Updater.FileVersion</variable>
|
||||||
|
<value type="int">22</value>
|
||||||
|
</data>
|
||||||
|
<data>
|
||||||
|
<variable>Version</variable>
|
||||||
|
<value type="int">22</value>
|
||||||
|
</data>
|
||||||
|
</qtcreator>
|
||||||
1
modbus_relay/modbus_relay.cxxflags
Normal file
1
modbus_relay/modbus_relay.cxxflags
Normal file
@ -0,0 +1 @@
|
|||||||
|
-std=c++17
|
||||||
3
modbus_relay/modbus_relay.files
Normal file
3
modbus_relay/modbus_relay.files
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
cmdlnopts.c
|
||||||
|
cmdlnopts.h
|
||||||
|
main.c
|
||||||
0
modbus_relay/modbus_relay.includes
Normal file
0
modbus_relay/modbus_relay.includes
Normal file
@ -1,6 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<!DOCTYPE QtCreatorProject>
|
<!DOCTYPE QtCreatorProject>
|
||||||
<!-- Written by QtCreator 15.0.1, 2025-02-11T17:06:44. -->
|
<!-- Written by QtCreator 16.0.1, 2025-05-22T14:49:29. -->
|
||||||
<qtcreator>
|
<qtcreator>
|
||||||
<data>
|
<data>
|
||||||
<variable>EnvironmentId</variable>
|
<variable>EnvironmentId</variable>
|
||||||
@ -13,8 +13,8 @@
|
|||||||
<data>
|
<data>
|
||||||
<variable>ProjectExplorer.Project.EditorSettings</variable>
|
<variable>ProjectExplorer.Project.EditorSettings</variable>
|
||||||
<valuemap type="QVariantMap">
|
<valuemap type="QVariantMap">
|
||||||
|
<value type="bool" key="EditorConfiguration.AutoDetect">true</value>
|
||||||
<value type="bool" key="EditorConfiguration.AutoIndent">true</value>
|
<value type="bool" key="EditorConfiguration.AutoIndent">true</value>
|
||||||
<value type="bool" key="EditorConfiguration.AutoSpacesForTabs">false</value>
|
|
||||||
<value type="bool" key="EditorConfiguration.CamelCaseNavigation">true</value>
|
<value type="bool" key="EditorConfiguration.CamelCaseNavigation">true</value>
|
||||||
<valuemap type="QVariantMap" key="EditorConfiguration.CodeStyle.0">
|
<valuemap type="QVariantMap" key="EditorConfiguration.CodeStyle.0">
|
||||||
<value type="QString" key="language">Cpp</value>
|
<value type="QString" key="language">Cpp</value>
|
||||||
|
|||||||
@ -29,21 +29,22 @@
|
|||||||
// work with single client, return FALSE if disconnected
|
// work with single client, return FALSE if disconnected
|
||||||
static int handle_socket(int sock, sl_tty_t *d){
|
static int handle_socket(int sock, sl_tty_t *d){
|
||||||
char buff[BUFLEN];
|
char buff[BUFLEN];
|
||||||
ssize_t rd = read(sock, buff, BUFLEN-1);;
|
ssize_t rd = read(sock, buff, BUFLEN-1);
|
||||||
DBG("Got %zd bytes", rd);
|
DBG("Got %zd bytes", rd);
|
||||||
if(rd <= 0){ // error or disconnect
|
if(rd <= 0){ // error or disconnect
|
||||||
DBG("Nothing to read from fd %d (ret: %zd)", sock, rd);
|
DBG("Nothing to read from fd %d (ret: %zd)", sock, rd);
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
// add trailing zero to be on the safe side
|
// add trailing zero to be on the safe side
|
||||||
|
#ifdef EBUG
|
||||||
buff[rd] = 0;
|
buff[rd] = 0;
|
||||||
|
#endif
|
||||||
DBG("GOT: _%s_", buff);
|
DBG("GOT: _%s_", buff);
|
||||||
ssize_t blen = strlen(buff);
|
if(sl_tty_write(d->comfd, buff, rd)){
|
||||||
if(blen != write(d->comfd, buff, blen)){
|
|
||||||
LOGWARN("write()");
|
LOGWARN("write()");
|
||||||
WARN("write()");
|
WARN("write()");
|
||||||
}
|
}
|
||||||
if(buff[blen-1] == '\n') buff[blen-1] = 0;
|
if(buff[rd-1] == '\n') buff[rd-1] = 0;
|
||||||
LOGMSG("CLIENT_%d: %s", sock, buff);
|
LOGMSG("CLIENT_%d: %s", sock, buff);
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
@ -79,76 +80,6 @@ static int canberead(int fd){
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief getserdata - read data (ending by '\n') from serial device
|
|
||||||
* @param D (i) - device
|
|
||||||
* @param len (o) - amount of butes read (-1 if disconnected)
|
|
||||||
* @return pointer to data buffer or NULL if none
|
|
||||||
*/
|
|
||||||
static char *getserdata(sl_tty_t *D, int *len){
|
|
||||||
static char serbuf[BUFLEN], *ptr = serbuf;
|
|
||||||
static size_t blen = BUFLEN - 1;
|
|
||||||
if(!D || D->comfd < 0) return NULL;
|
|
||||||
char *nl = NULL;
|
|
||||||
do{
|
|
||||||
int s = canberead(D->comfd);
|
|
||||||
if(s == 0) break;
|
|
||||||
if(s < 0){ // interrupted?
|
|
||||||
if(len) *len = 0;
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
ssize_t l = read(D->comfd, ptr, blen);
|
|
||||||
if(l < 1){ // disconnected
|
|
||||||
DBG("device disconnected");
|
|
||||||
if(len) *len = -1;
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
ptr[l] = 0;
|
|
||||||
DBG("GOT %zd bytes: '%s'", l, ptr);
|
|
||||||
nl = strchr(ptr, '\n');
|
|
||||||
ptr += l;
|
|
||||||
blen -= l;
|
|
||||||
if(nl){
|
|
||||||
DBG("Got newline");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}while(blen);
|
|
||||||
// recalculate newline from the beginning (what if old data stays there?)
|
|
||||||
nl = strchr(serbuf, '\n');
|
|
||||||
if(blen && !nl){
|
|
||||||
if(len) *len = 0;
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
// in case of overflow send buffer without trailing '\n'
|
|
||||||
int L;
|
|
||||||
if(nl) L = nl - serbuf + 1; // get line to '\n'
|
|
||||||
else L = strlen(serbuf); // get all buffer
|
|
||||||
if(len) *len = L;
|
|
||||||
memcpy(D->buf, serbuf, L); // copy all + trailing zero
|
|
||||||
D->buflen = L;
|
|
||||||
D->buf[L] = 0;
|
|
||||||
DBG("Put to buf %d bytes: '%s'", L, D->buf);
|
|
||||||
if(nl){
|
|
||||||
L = ptr - nl - 1; // symbols after newline
|
|
||||||
if(L > 0){ // there's some data after '\n' -> put it into the beginning
|
|
||||||
memmove(serbuf, nl+1, L);
|
|
||||||
blen = BUFLEN - 1 - L;
|
|
||||||
ptr = serbuf + L;
|
|
||||||
*ptr = 0;
|
|
||||||
DBG("now serbuf is '%s'", serbuf);
|
|
||||||
}else{
|
|
||||||
blen = BUFLEN - 1;
|
|
||||||
ptr = serbuf;
|
|
||||||
*ptr = 0;
|
|
||||||
}
|
|
||||||
}else{
|
|
||||||
blen = BUFLEN - 1;
|
|
||||||
ptr = serbuf;
|
|
||||||
*ptr = 0;
|
|
||||||
}
|
|
||||||
return D->buf;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void server_(int sock, sl_tty_t *d){
|
static void server_(int sock, sl_tty_t *d){
|
||||||
if(listen(sock, MAXCLIENTS) == -1){
|
if(listen(sock, MAXCLIENTS) == -1){
|
||||||
WARN("listen");
|
WARN("listen");
|
||||||
@ -185,20 +116,21 @@ static void server_(int sock, sl_tty_t *d){
|
|||||||
++nfd;
|
++nfd;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
int l = -1;
|
int l = sl_tty_read(d);
|
||||||
char *serdata = getserdata(d, &l);
|
|
||||||
if(l < 0){
|
if(l < 0){
|
||||||
LOGERR("Serial device disconnected");
|
LOGERR("Serial device disconnected");
|
||||||
ERRX("Serial device disconnected");
|
ERRX("Serial device disconnected");
|
||||||
}
|
}
|
||||||
if(serdata){
|
if(l > 0){
|
||||||
|
DBG("Got %d bytes from serial", l);
|
||||||
for(int i = 1; i < nfd; ++i)
|
for(int i = 1; i < nfd; ++i)
|
||||||
if(l != send(poll_set[i].fd, serdata, l, MSG_NOSIGNAL)){
|
if(l != send(poll_set[i].fd, d->buf, l, MSG_NOSIGNAL)){
|
||||||
LOGWARN("send()");
|
LOGWARN("send()");
|
||||||
WARN("send()");
|
WARN("send()");
|
||||||
}
|
}
|
||||||
if(serdata[l-1] == '\n') serdata[l-1] = 0;
|
if(l < (int)d->bufsz) d->buf[l] = 0;
|
||||||
LOGMSG("SERIAL: %s", serdata);
|
if(d->buf[l-1] == '\n') d->buf[l-1] = 0;
|
||||||
|
LOGMSG("SERIAL: %s", d->buf);
|
||||||
}
|
}
|
||||||
// scan connections
|
// scan connections
|
||||||
for(int fdidx = 1; fdidx < nfd; ++fdidx){
|
for(int fdidx = 1; fdidx < nfd; ++fdidx){
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user