adding first modbus code

main
Steffen Pohle 2 years ago
parent 27a2489212
commit ed87982a8e

3
.gitignore vendored

@ -1,3 +1,6 @@
Makefile.rules
config.h
testmodbus-client
.project
testmodbus-client.ui~
*.oo

@ -3,7 +3,7 @@ VERSION = 1.0.1
-include Makefile.rules
OBJECTSCLI = client.oo tcp.oo nwthread.oo
OBJECTSCLI = client.oo tcp.oo nwthread.oo modbus.oo
DISTNAME=testmodbus-client-$(VERSION)

@ -0,0 +1,88 @@
/////////////////////////////////////////////////////////////////////////////////
//
// modbus.cc is part of TestModbus-Client.
//
/////////////////////////////////////////////////////////////////////////////////
#include <string.h>
#include <errno.h>
#include "modbus.h"
#define PACK_INT8(__dstptr, __srci8) \
memcpy(__dstptr, &__srci8, 1); \
__dstptr += 1
#define UNPACK_INT8(__dsti8, __srcptr) \
memcpy(&__dsti8, __srcptr, 1); \
__srcptr += 1
#define PACK_INT16(__tmpi16, __dstptr, __srci16) \
__tmpi16 = htons((uint16_t)__srci16); \
memcpy(__dstptr, &__tmpi16, 2); \
__dstptr += 2
#define UNPACK_INT16(__tmpi16, __dsti16, __srcptr) \
memcpy(&__tmpi16, __srcptr, 2); \
__srcptr += 2; \
__dsti16 = ntohs((uint16_t)__tmpi16)
/*
* packs the header into the buffer data and returns the lenght of the header
* returns NULL on error or points to end of data
*/
char * modbustcp_pkt_header (char *destbuffer, int bufsize, modbustcp_header *srcmbh) {
uint16_t tmp;
// destination memory valid
if (destbuffer == NULL) {
errno = EFAULT;
return NULL;
}
if (bufsize < 12) {
errno = EOVERFLOW;
return NULL;
}
char *pos = destbuffer;
PACK_INT16(tmp, pos, srcmbh->transid);
PACK_INT16(tmp, pos, srcmbh->protid);
PACK_INT16(tmp, pos, srcmbh->length);
PACK_INT8(pos, srcmbh->uid);
PACK_INT8(pos, srcmbh->fc);
PACK_INT16(tmp, pos, srcmbh->offset);
PACK_INT16(tmp, pos, srcmbh->number);
return pos;
}
/*
* unpacks the buffer into the header
* returns NULL on error or points to end of data
*/
char * modbustcp_unpkt_header (modbustcp_header *destmbh, char *srcbuffer, int bufsize) {
uint16_t tmp;
// source memory valid
if (srcbuffer == NULL) {
errno = EFAULT;
return NULL;
}
if (bufsize < 12) {
errno = EOVERFLOW;
return NULL;
}
char *src = srcbuffer;
UNPACK_INT16(tmp, destmbh->transid, src);
UNPACK_INT16(tmp, destmbh->protid, src);
UNPACK_INT16(tmp, destmbh->length, src);
UNPACK_INT8(destmbh->uid, src);
UNPACK_INT8(destmbh->fc, src);
UNPACK_INT16(tmp, destmbh->offset, src);
UNPACK_INT16(tmp, destmbh->number, src);
return src;
}

@ -0,0 +1,33 @@
/////////////////////////////////////////////////////////////////////////////////
//
// modbus.h is part of TestModbus-Client.
//
/////////////////////////////////////////////////////////////////////////////////
#ifndef _MODBUS_H_
#define _MODBUS_H_
#include <stdint.h>
#include <stdlib.h>
#include <arpa/inet.h>
/*
* this file will only contain the encoding and decoding functions
*/
struct {
uint16_t transid; // transaction id
uint16_t protid; // protocol id
uint16_t length; // number of bytes followed
uint8_t uid; // unit/slave id
uint8_t fc; // function code
uint16_t offset; // first register to read
uint16_t number; // number of registers to read
} typedef modbustcp_header;
char * modbustcp_pkt_header (char *destbuffer, int bufsize, modbustcp_header *srcmbh);
char * modbustcp_unpkt_header (modbustcp_header *destmbh, char *srcbuffer, int bufsize);
#endif

@ -36,6 +36,11 @@ NetworkThread::NetworkThread() {
state = 0;
host = "";
port = "";
req_fc = -1;
req_offset = 0;
req_size = 0;
memset (&req_time, 0x0, sizeof (req_time));
req_timeout = 0;
};
NetworkThread::~NetworkThread() {

@ -2,6 +2,12 @@
<!-- Generated with glade 3.40.0 -->
<interface>
<requires lib="gtk+" version="3.24"/>
<object class="GtkEntryBuffer" id="entrybuffer_numbers">
<property name="text" translatable="yes">16</property>
</object>
<object class="GtkEntryBuffer" id="entrybuffer_offset">
<property name="text" translatable="yes">0</property>
</object>
<object class="GtkWindow" id="testmodbus-client">
<property name="can-focus">False</property>
<signal name="destroy" handler="gtk_main_quit" swapped="no"/>
@ -355,6 +361,7 @@
<property name="receives-default">False</property>
<property name="active">True</property>
<property name="draw-indicator">True</property>
<property name="group">cli_FC1</property>
</object>
<packing>
<property name="expand">False</property>
@ -371,6 +378,7 @@
<property name="receives-default">False</property>
<property name="active">True</property>
<property name="draw-indicator">True</property>
<property name="group">cli_FC1</property>
</object>
<packing>
<property name="expand">False</property>
@ -387,6 +395,7 @@
<property name="receives-default">False</property>
<property name="active">True</property>
<property name="draw-indicator">True</property>
<property name="group">cli_FC1</property>
</object>
<packing>
<property name="expand">False</property>
@ -455,8 +464,10 @@
<object class="GtkEntry">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="buffer">entrybuffer_offset</property>
<property name="max-length">5</property>
<property name="width-chars">5</property>
<property name="placeholder-text" translatable="yes">0</property>
</object>
<packing>
<property name="left-attach">1</property>
@ -467,8 +478,10 @@
<object class="GtkEntry">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="buffer">entrybuffer_numbers</property>
<property name="max-length">5</property>
<property name="width-chars">5</property>
<property name="placeholder-text" translatable="yes">16</property>
</object>
<packing>
<property name="left-attach">1</property>

Loading…
Cancel
Save