Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
taka-wang committed Oct 12, 2016
1 parent 9355b8e commit f5db98b
Show file tree
Hide file tree
Showing 17 changed files with 512 additions and 636 deletions.
2 changes: 0 additions & 2 deletions .drone.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# drone.io v0.4
# armhf


build:
image: takawang/dind:$$arch
volumes:
Expand All @@ -18,7 +17,6 @@ build:
- docker-compose -f dockerfile/docker-compose.$$arch build --no-cache
- docker-compose -f dockerfile/docker-compose.$$arch up --abort-on-container-exit


matrix:
arch:
- armhf
Expand Down
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ project(modbusd C)

set(CMAKE_VERBOSE_MAKEFILE ON)
set(bin_name modbusd)
#set(modbus_include /usr/local/include/modbus)
set(modbus_include /usr/include/modbus)
set(czmq_include /usr/include/)
set(bin_source json/cJSON.c json.c mb.c mbtcp.c main.c)
Expand All @@ -18,7 +17,8 @@ include(GetGitRevisionDescription)
# set latest git tag as modbusd version string
git_describe(MODBUSD_VERSION --abbrev=0 --tags)
# define version string to main.h
add_definitions(-DVERSION="${MODBUSD_VERSION}" )
#add_definitions(-DVERSION="${MODBUSD_VERSION}" )
add_definitions(-DVERSION="0.7.1" )

# cflag
set(EXT_C_FLAGS "-W -Wall -Wno-format-security -O3 -std=c99")
Expand Down
36 changes: 20 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,31 +49,35 @@ I do continuous integration and build docker images after git push by self-hoste
### Coil/register number and address table

>|Coil/Register numbers|data address |type |table name |offset| function code|
>|:--------------------|:------------------|:-------------|:------------------------------|:-----|:-------------|
>|1-9999 |0000 to 270E (9998)|Read-Write |Discrete Output Coils |1 | 1, 5, 15 |
>|10001-19999 |0000 to 270E (9998)|Read-Only |Discrete Input Contacts |10001 | 2 |
>|30001-39999 |0000 to 270E (9998)|Read-Only |Analog Input Registers |30001 | 4 |
>|40001-49999 |0000 to 270E (9998)|Read-Write |Analog Output Holding Registers|40001 | 3, 6, 16 |
>| Coil/Register numbers| data address | type | table name | offset | function code |
>|:---------------------|:-------------------|:--------------|:-------------------------------|:-------|:--------------|
>| 1-9999 | 0000 to 270E (9998)| Read-Write | Discrete Output Coils | 1 | 1, 5, 15 |
>| 10001-19999 | 0000 to 270E (9998)| Read-Only | Discrete Input Contacts | 10001 | 2 |
>| 30001-39999 | 0000 to 270E (9998)| Read-Only | Analog Input Registers | 30001 | 4 |
>| 40001-49999 | 0000 to 270E (9998)| Read-Write | Analog Output Holding Registers| 40001 | 3, 6, 16 |
### Command mapping table

>| Command | Number | Description |
>|:---------------:|-------:|:-----------------|
>| fc1 | 1 | modbus fc 1 |
>| fc2 | 2 | modbus fc 2 |
>| fc3 | 3 | modbus fc 3 |
>| fc4 | 4 | modbus fc 4 |
>| fc5 | 5 | modbus fc 5 |
>| fc6 | 6 | modbus fc 6 |
>| fc15 | 15 | modbus fc 15 |
>| fc16 | 16 | modbus fc 16 |
>| set_tcp_timeout | 50 | set tcp timeout |
>| get_tcp_timeout | 51 | get tcp timeout |
>| FC1 | 1 | modbus fc 1 |
>| FC2 | 2 | modbus fc 2 |
>| FC3 | 3 | modbus fc 3 |
>| FC4 | 4 | modbus fc 4 |
>| FC5 | 5 | modbus fc 5 |
>| FC6 | 6 | modbus fc 6 |
>| FC15 | 15 | modbus fc 15 |
>| FC16 | 16 | modbus fc 16 |
>| SET_TCP_TIMEOUT | 50 | set tcp timeout |
>| GET_TCP_TIMEOUT | 51 | get tcp timeout |

---

### Environment variable

- **CONF_MODBUSD**: config file path

### Configuration File

```javascript
Expand Down
2 changes: 1 addition & 1 deletion circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ machine:

dependencies:
pre:
- sudo pip install docker-compose
- sudo apt-get install python-dev && sudo pip install --upgrade docker-compose==1.8.0

test:
pre:
Expand Down
2 changes: 0 additions & 2 deletions dockerfile/Dockerfile.armhf
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,5 @@ RUN apk update \

EXPOSE 502

#CMD /usr/bin/modbusd /etc/modbusd/modbusd.json

ENTRYPOINT ["/usr/bin/modbusd"]
CMD ["/etc/modbusd/modbusd.json"]
2 changes: 0 additions & 2 deletions dockerfile/Dockerfile.ubuntu
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,5 @@ RUN cp "config/service.sh" "/etc/init.d/modbusd" && \
## Default exported port
EXPOSE 502

#CMD /usr/bin/modbusd /etc/modbusd/modbusd.json

ENTRYPOINT ["/usr/bin/modbusd"]
CMD ["/etc/modbusd/modbusd.json"]
2 changes: 0 additions & 2 deletions dockerfile/Dockerfile.x86
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,5 @@ RUN apk update \

EXPOSE 502

#CMD /usr/bin/modbusd /etc/modbusd/modbusd.json

ENTRYPOINT ["/usr/bin/modbusd"]
CMD ["/etc/modbusd/modbusd.json"]
18 changes: 18 additions & 0 deletions docs/command.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,14 @@ Please refer to [command code](https://github.com/taka-wang/modbusd#command-mapp
>| len | bit/register length | integer | - | 20 |
>| status | response status | string | - | "ok" |
---

### 1.1 Read request

#### 1.1.1 psmb to modbusd

**mbtcp read request**

```javascript
{
"tid": "123456",
Expand All @@ -75,7 +79,9 @@ Please refer to [command code](https://github.com/taka-wang/modbusd#command-mapp
```

#### 1.1.2 modbusd to psmb

**mbtcp single read response**

```javascript
{
"tid": "123456",
Expand All @@ -85,6 +91,7 @@ Please refer to [command code](https://github.com/taka-wang/modbusd#command-mapp
```

**mbtcp multiple read response**

```javascript
{
"tid": "123456",
Expand All @@ -96,7 +103,9 @@ Please refer to [command code](https://github.com/taka-wang/modbusd#command-mapp
### 1.2 Write request

#### 1.2.1 psmb to modbusd

**mbtcp single write request**

```javascript
{
"tid": "123456",
Expand All @@ -110,6 +119,7 @@ Please refer to [command code](https://github.com/taka-wang/modbusd#command-mapp
```

**mbtcp multiple write request**

```javascript
{
"ip": "192.168.3.2",
Expand All @@ -122,9 +132,11 @@ Please refer to [command code](https://github.com/taka-wang/modbusd#command-mapp
"data": [1, 2, 3, 4]
}
```

#### 1.2.2 modbusd to psmb

**mbtcp write response**

```javascript
{
"tid": "123456",
Expand All @@ -137,6 +149,7 @@ Please refer to [command code](https://github.com/taka-wang/modbusd#command-mapp
#### 1.3.1 psmb to modbusd

**mbtcp set timeout request**

```javascript
{
"tid": "123456",
Expand All @@ -146,6 +159,7 @@ Please refer to [command code](https://github.com/taka-wang/modbusd#command-mapp
```

**mbtcp get timeout request**

```javascript
{
"tid": "123456",
Expand All @@ -156,6 +170,7 @@ Please refer to [command code](https://github.com/taka-wang/modbusd#command-mapp
#### 1.3.2 modbusd to psmb

**mbtcp set timeout response**

```javascript
{
"tid": "123456",
Expand All @@ -164,16 +179,19 @@ Please refer to [command code](https://github.com/taka-wang/modbusd#command-mapp
```

**mbtcp get timeout response**

```javascript
{
"tid": "123456",
"status": "ok",
"timeout": 210000
}
```

### 1.4 Generic fail response

**mbtcp fail response**

```javascript
{
"tid": "123456",
Expand Down
42 changes: 21 additions & 21 deletions json.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,50 +8,50 @@
#include <stdlib.h>
#include "json.h"

char *json_get_char(cJSON *inJson, const char *key)
char *json_get_char (cJSON *inJson, const char *key)
{
return cJSON_GetObjectItem(inJson, key)->valuestring;
return cJSON_GetObjectItem (inJson, key)->valuestring;
}

int json_get_int(cJSON *inJson, const char *key)
int json_get_int (cJSON *inJson, const char *key)
{
return cJSON_GetObjectItem(inJson, key)->valueint;
return cJSON_GetObjectItem (inJson, key)->valueint;
}

void json_set_int(cJSON *inJson, const char *key, int value)
void json_set_int (cJSON *inJson, const char *key, int value)
{
cJSON_GetObjectItem(inJson, key)->valueint = value;
cJSON_GetObjectItem (inJson, key)->valueint = value;
}

double json_get_double(cJSON *inJson, const char *key)
double json_get_double (cJSON *inJson, const char *key)
{
return cJSON_GetObjectItem(inJson, key)->valuedouble;
return cJSON_GetObjectItem (inJson, key)->valuedouble;
}

void json_set_double(cJSON *inJson, const char *key, double value)
void json_set_double (cJSON *inJson, const char *key, double value)
{
cJSON_GetObjectItem(inJson, key)->valuedouble = value;
cJSON_GetObjectItem (inJson, key)->valuedouble = value;
}

long json_get_long(cJSON *inJson, const char *key)
long json_get_long (cJSON *inJson, const char *key)
{
// convert double float to long integer
return (long)cJSON_GetObjectItem(inJson, key)->valuedouble;
}

int file_to_json(const char *fname, cJSON **outJson)
int file_to_json (const char *fname, cJSON **outJson)
{
FILE *fPtr = fopen(fname,"rb");
FILE *fPtr = fopen (fname,"rb");
if (fPtr)
{
fseek(fPtr, 0, SEEK_END);
long len = ftell(fPtr);
long len = ftell (fPtr);
fseek(fPtr, 0, SEEK_SET);
char *data = (char*) malloc (len+1);
int _ = fread(data, 1, len, fPtr); _=_;
fclose(fPtr);
*outJson = cJSON_Parse(data);
free(data);
int _ = fread (data, 1, len, fPtr); _=_;
fclose (fPtr);
*outJson = cJSON_Parse (data);
free (data);
return outJson ? 0 : 1;
}
else
Expand All @@ -60,13 +60,13 @@ int file_to_json(const char *fname, cJSON **outJson)
}
}

int json_to_file(const char *fname, cJSON *inJson)
int json_to_file (const char *fname, cJSON *inJson)
{
FILE *fPtr = fopen(fname, "w");
if (fPtr)
{
fprintf(fPtr, "%s", cJSON_Print(inJson));
fclose(fPtr);
fprintf (fPtr, "%s", cJSON_Print (inJson));
fclose (fPtr);
return 0;
}
else
Expand Down
16 changes: 8 additions & 8 deletions json.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* @param key Json key.
* @return C char string.
*/
char * json_get_char(cJSON *inJson, const char *key);
char * json_get_char (cJSON *inJson, const char *key);

/**
* @brief Get integer value via key from cJSON object
Expand All @@ -25,7 +25,7 @@ char * json_get_char(cJSON *inJson, const char *key);
* @param key Json key.
* @return Integer.
*/
int json_get_int(cJSON *inJson, const char *key);
int json_get_int (cJSON *inJson, const char *key);


/**
Expand All @@ -36,7 +36,7 @@ int json_get_int(cJSON *inJson, const char *key);
* @param value Integer value.
* @return Void.
*/
void json_set_int(cJSON *inJson, const char *key, int value);
void json_set_int (cJSON *inJson, const char *key, int value);

/**
* @brief Get double integer value via key from cJSON object
Expand All @@ -45,7 +45,7 @@ void json_set_int(cJSON *inJson, const char *key, int value);
* @param key Json key.
* @return Double integer value.
*/
double json_get_double(cJSON *inJson, const char *key);
double json_get_double (cJSON *inJson, const char *key);

/**
* @brief Set double integer value via key to existed cJSON object
Expand All @@ -55,7 +55,7 @@ double json_get_double(cJSON *inJson, const char *key);
* @param double Double integer value.
* @return Void.
*/
void json_set_double(cJSON *inJson, const char *key, double value);
void json_set_double (cJSON *inJson, const char *key, double value);

/**
* @brief Get long integer value via key from cJSON object
Expand All @@ -64,7 +64,7 @@ void json_set_double(cJSON *inJson, const char *key, double value);
* @param key Json key.
* @return Long.
*/
long json_get_long(cJSON *inJson, const char *key);
long json_get_long (cJSON *inJson, const char *key);

/**
* @brief Load JSON file to cJSON object
Expand All @@ -73,7 +73,7 @@ long json_get_long(cJSON *inJson, const char *key);
* @param outJson Pointer to cJSON output object.
* @return Success or not.
*/
int file_to_json(const char *fname, cJSON **outJson);
int file_to_json (const char *fname, cJSON **outJson);

/**
* @brief Save cJSON object to JSON file
Expand All @@ -82,6 +82,6 @@ int file_to_json(const char *fname, cJSON **outJson);
* @param inJson cJSON input object.
* @return Success or not.
*/
int json_to_file(const char *fname, cJSON *inJson);
int json_to_file (const char *fname, cJSON *inJson);

#endif // JSON_H
Loading

0 comments on commit f5db98b

Please sign in to comment.