fz-lyu / modbuspp Goto Github PK
View Code? Open in Web Editor NEWA C++ Library for Modbus TCP Client Protocol
License: GNU General Public License v3.0
A C++ Library for Modbus TCP Client Protocol
License: GNU General Public License v3.0
currently the variables below are defined and the end of the function, overwriting the error on the variable "msg" identified previously by the switch.
err = false;
error_msg = "NO ERR";
Moving the definition at before the switch will show the error properly
/**
* Error Code Handler
* @param msg Message Received from the Server
* @param func Modbus Functional Code
*/
void modbus::modbuserror_handle(const uint8_t *msg, int func) {
err = false;
error_msg = "NO ERR";
if(msg[7] == func + 0x80) {
err = true;
switch(msg[8]){
case EX_ILLEGAL_FUNCTION:
error_msg = "1 Illegal Function";
break;
case EX_ILLEGAL_ADDRESS:
error_msg = "2 Illegal Address";
break;
case EX_ILLEGAL_VALUE:
error_msg = "3 Illegal Value";
break;
case EX_SERVER_FAILURE:
error_msg = "4 Server Failure";
break;
case EX_ACKNOWLEDGE:
error_msg = "5 Acknowledge";
break;
case EX_SERVER_BUSY:
error_msg = "6 Server Busy";
break;
case EX_NEGATIVE_ACK:
error_msg = "7 Negative Acknowledge";
break;
case EX_MEM_PARITY_PROB:
error_msg = "8 Memory Parity Problem";
break;
case EX_GATEWAY_PROBLEMP:
error_msg = "10 Gateway Path Unavailable";
break;
case EX_GATEWYA_PROBLEMF:
error_msg = "11 Gateway Target Device Failed to Respond";
break;
default:
error_msg = "UNK";
break;
}
}
// err = false;
// error_msg = "NO ERR";
}
void modbus::modbus_write(int address, int amount, int func, uint16_t value) {
if (func == WRITE_COIL || func == WRITE_REG) {
uint8_t to_send[12];
modbus_build_request(to_send, address, func);
to_send[5] = 6;
to_send[10] = (uint8_t)(value[0] >> 8);
to_send[11] = (uint8_t)(value[0] & 0x00FF);
modbus_send(to_send, 12);
}
else if (func == WRITE_REGS) {
uint8_t to_send = new uint8_t[13 + 2 * amount];
modbus_build_request(to_send, address, func);
to_send[5] = (uint8_t)(5 + 2 * amount);
to_send[10] = (uint8_t)(amount >> 8);
to_send[11] = (uint8_t)(amount & 0x00FF);
to_send[12] = (uint8_t)(2 * amount);
for (int i = 0; i < amount; i++) {
to_send[13 + 2 * i] = (uint8_t)(value[i] >> 8);
to_send[14 + 2 * i] = (uint8_t)(value[i] & 0x00FF);
}
cout << to_send << endl;
modbus_send(to_send, 13 + 2 * amount);
}
else if (func == WRITE_COILS) {
uint8_t* to_send = new uint8_t[14 + (amount - 1) / 8];
modbus_build_request(to_send, address, func);
to_send[5] = (uint8_t)(7 + (amount - 1) / 8);
to_send[10] = (uint8_t)(amount >> 8);
to_send[11] = (uint8_t)(amount>>8);
to_send[12] = (uint8_t)((amount + 7) / 8);
for (int i = 0; i < amount; i++) {
to_send[13 + (i - 1) / 8] += (uint8_t)(value[i] << (i % 8));
}
cout << to_send << endl;
modbus_send(to_send, 14 + (amount - 1) / 8);
}
}
when "modbus_write_coil"s function calls it gives me "illegal data values" error. i do to_send[11] = (uint8_t)(amount); in func == WRITE_COILS of "modbus_write" function butt it did not write multiple coils coils
The length coding field in the MBAP header part of the mb/tcp ADU is wrong calculated, so enforcing mb receivers to read too few holding registers from the request.
I'm in C and C++ and Labview and Git, but i'm totally newb to the github thing. I hope to provide a new version or pull request the right way...
Hello,
i just started using your library but I found an issue.
When I instantiate the class, the code works but I get an exception when the program exits. See below and attached screenshot.
Output :
address : 192.168.1.21 port : 502
reg #1 : 1 reg #2 : 2
Found Proper Host 192.168.1.21 and Port 502
Socket Opened Successfully
Connected
Register #0 holds : 0x7fff360f17ae
Socket Closed
*** Error in `./modbus_retriever': munmap_chunk(): invalid pointer: 0x00007fff360f1800 ***
Abandon (core dumped)
@fanzhe98 Thanks you for helping
I saw you said this code could also be used in Windows, but when I used it in Windows, firstly it need to compile with -lwsock32. But when I get the executable file, it could only connect with the Modbus Slaver, could not read or write, only can connect this IP with the correct port, but it can not read or write.
When multiple coils are written the modbus_write writes wrongfully the low byte of the requested coil
Requested Number of Coil Low Byte should be:
to_send[11] = (uint8_t) (amount);
instead of :
to_send[11] = (uint8_t) (amount>>8);
Is there any way to emulate a MODBUS Server too using modbuspp?
So I can use my own PC (or 2 PCs + Ethernet comm) to run the modbus datagram transfer?
visual studio says that " Using uninitialized memory 'to_send' " and " potentially uninitialized local pointer variable 'to_send' used ". Because of that I can't send anything to my registers, but only read.
From microsoft documentation SO_RCVTIMEO and SO_SNDTIMEO are expressed in ms whereas in the code the timeout seems to be targetting 20s as for linux:
#ifdef WIN32
const DWORD timeout = 20;
#else
struct timeval timeout
{
};
timeout.tv_sec = 20; // after 20 seconds connect() will timeout
timeout.tv_usec = 0;
#endif
When you successfully connect and send data, receive data, this is normal.
Unplugging the network cable and sending data again will return an error, which is normal.
However, when you plug the network cable back in, no error will be returned because "err_no" is 0,
This bug occurs in -> If (err) {return err_no; }
Another problem is that when the wire is broken and reconnected, there is no check to see if the content of the protocol frame returned is correct
This got me confused for some time initially.
In Line 85, It's written
Writing Operations include writing sigle coil
sigle here is actually meant to be single right?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.