Examples from bin directory updated. API usage updated in mbus-protocol-aux.h.
This commit is contained in:
parent
34255c7237
commit
ec50ab9286
13
bin/mbus-serial-request-data-multi-reply.c
Normal file → Executable file
13
bin/mbus-serial-request-data-multi-reply.c
Normal file → Executable file
@ -73,13 +73,19 @@ main(int argc, char **argv)
|
||||
mbus_register_recv_event(&mbus_dump_recv_event);
|
||||
}
|
||||
|
||||
if ((handle = mbus_connect_serial(device)) == NULL)
|
||||
if ((handle = mbus_context_serial(device)) == NULL)
|
||||
{
|
||||
fprintf(stderr, "Failed to setup connection to M-bus gateway\n");
|
||||
fprintf(stderr, "Could not initialize M-Bus context: %s\n", mbus_error_str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (mbus_serial_set_baudrate(handle->m_serial_handle, baudrate) == -1)
|
||||
if (!mbus_connect(handle))
|
||||
{
|
||||
printf("Failed to setup connection to M-bus gateway\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (mbus_serial_set_baudrate(handle, baudrate) == -1)
|
||||
{
|
||||
printf("Failed to set baud rate.\n");
|
||||
return 1;
|
||||
@ -174,6 +180,7 @@ main(int argc, char **argv)
|
||||
free(xml_result);
|
||||
|
||||
mbus_disconnect(handle);
|
||||
mbus_context_free(handle);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -70,13 +70,19 @@ main(int argc, char **argv)
|
||||
mbus_register_recv_event(&mbus_dump_recv_event);
|
||||
}
|
||||
|
||||
if ((handle = mbus_connect_serial(device)) == NULL)
|
||||
if ((handle = mbus_context_serial(device)) == NULL)
|
||||
{
|
||||
fprintf(stderr, "Failed to setup connection to M-bus gateway\n");
|
||||
fprintf(stderr, "Could not initialize M-Bus context: %s\n", mbus_error_str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (mbus_serial_set_baudrate(handle->m_serial_handle, baudrate) == -1)
|
||||
if (!mbus_connect(handle))
|
||||
{
|
||||
printf("Failed to setup connection to M-bus gateway\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (mbus_serial_set_baudrate(handle, baudrate) == -1)
|
||||
{
|
||||
printf("Failed to set baud rate.\n");
|
||||
return 1;
|
||||
@ -162,6 +168,7 @@ main(int argc, char **argv)
|
||||
}
|
||||
|
||||
mbus_disconnect(handle);
|
||||
mbus_context_free(handle);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
14
bin/mbus-serial-scan-secondary.c
Normal file → Executable file
14
bin/mbus-serial-scan-secondary.c
Normal file → Executable file
@ -102,13 +102,19 @@ main(int argc, char **argv)
|
||||
return 1;
|
||||
}
|
||||
|
||||
if ((handle = mbus_connect_serial(device)) == NULL)
|
||||
if ((handle = mbus_context_serial(device)) == NULL)
|
||||
{
|
||||
fprintf(stderr, "Failed to setup connection to M-bus gateway: %s\n", mbus_error_str());
|
||||
fprintf(stderr, "Could not initialize M-Bus context: %s\n", mbus_error_str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (mbus_serial_set_baudrate(handle->m_serial_handle, baudrate) == -1)
|
||||
if (!mbus_connect(handle))
|
||||
{
|
||||
printf("Failed to setup connection to M-bus gateway\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (mbus_serial_set_baudrate(handle, baudrate) == -1)
|
||||
{
|
||||
fprintf(stderr, "Failed to set baud rate.\n");
|
||||
return 1;
|
||||
@ -153,7 +159,7 @@ main(int argc, char **argv)
|
||||
mbus_scan_2nd_address_range(handle, 0, addr_mask);
|
||||
|
||||
mbus_disconnect(handle);
|
||||
|
||||
mbus_context_free(handle);
|
||||
//printf("Summary: Tried %ld address masks and found %d devices.\n", probe_count, match_count);
|
||||
|
||||
free(addr_mask);
|
||||
|
@ -62,13 +62,19 @@ main(int argc, char **argv)
|
||||
mbus_register_recv_event(&mbus_dump_recv_event);
|
||||
}
|
||||
|
||||
if ((handle = mbus_connect_serial(device)) == NULL)
|
||||
if ((handle = mbus_context_serial(device)) == NULL)
|
||||
{
|
||||
fprintf(stderr, "Could not initialize M-Bus context: %s\n", mbus_error_str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!mbus_connect(handle))
|
||||
{
|
||||
printf("Failed to setup connection to M-bus gateway\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (mbus_serial_set_baudrate(handle->m_serial_handle, baudrate) == -1)
|
||||
if (mbus_serial_set_baudrate(handle, baudrate) == -1)
|
||||
{
|
||||
printf("Failed to set baud rate.\n");
|
||||
return 1;
|
||||
@ -128,6 +134,7 @@ main(int argc, char **argv)
|
||||
}
|
||||
|
||||
mbus_disconnect(handle);
|
||||
mbus_context_free(handle);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -53,13 +53,19 @@ main(int argc, char **argv)
|
||||
return 1;
|
||||
}
|
||||
|
||||
if ((handle = mbus_connect_serial(device)) == NULL)
|
||||
if ((handle = mbus_context_serial(device)) == NULL)
|
||||
{
|
||||
printf("Failed to setup connection to M-bus device: %s\n", mbus_error_str());
|
||||
fprintf(stderr, "Could not initialize M-Bus context: %s\n", mbus_error_str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (mbus_serial_set_baudrate(handle->m_serial_handle, baudrate) == -1)
|
||||
if (!mbus_connect(handle))
|
||||
{
|
||||
printf("Failed to setup connection to M-bus gateway\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (mbus_serial_set_baudrate(handle, baudrate) == -1)
|
||||
{
|
||||
printf("Failed to set baud rate.\n");
|
||||
return 1;
|
||||
@ -113,6 +119,7 @@ main(int argc, char **argv)
|
||||
|
||||
free(addr);
|
||||
mbus_disconnect(handle);
|
||||
mbus_context_free(handle);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -52,13 +52,19 @@ main(int argc, char **argv)
|
||||
return 0;
|
||||
}
|
||||
|
||||
if ((handle = mbus_connect_serial(device)) == NULL)
|
||||
if ((handle = mbus_context_serial(device)) == NULL)
|
||||
{
|
||||
printf("Failed to setup connection to M-bus device: %s\n", mbus_error_str());
|
||||
fprintf(stderr, "Could not initialize M-Bus context: %s\n", mbus_error_str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!mbus_connect(handle))
|
||||
{
|
||||
printf("Failed to setup connection to M-bus gateway\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (mbus_serial_set_baudrate(handle->m_serial_handle, source_baudrate) == -1)
|
||||
if (mbus_serial_set_baudrate(handle, source_baudrate) == -1)
|
||||
{
|
||||
printf("Failed to set baud rate.\n");
|
||||
return 1;
|
||||
@ -88,6 +94,7 @@ main(int argc, char **argv)
|
||||
}
|
||||
|
||||
mbus_disconnect(handle);
|
||||
mbus_context_free(handle);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
9
bin/mbus-tcp-request-data-multi-reply.c
Normal file → Executable file
9
bin/mbus-tcp-request-data-multi-reply.c
Normal file → Executable file
@ -62,7 +62,13 @@ main(int argc, char **argv)
|
||||
mbus_register_recv_event(&mbus_dump_recv_event);
|
||||
}
|
||||
|
||||
if ((handle = mbus_connect_tcp(host, port)) == NULL)
|
||||
if ((handle = mbus_context_tcp(host, port)) == NULL)
|
||||
{
|
||||
fprintf(stderr, "Could not initialize M-Bus context: %s\n", mbus_error_str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!mbus_connect(handle))
|
||||
{
|
||||
fprintf(stderr, "Failed to setup connection to M-bus gateway\n");
|
||||
return 1;
|
||||
@ -157,6 +163,7 @@ main(int argc, char **argv)
|
||||
free(xml_result);
|
||||
|
||||
mbus_disconnect(handle);
|
||||
mbus_context_free(handle);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -62,7 +62,13 @@ main(int argc, char **argv)
|
||||
mbus_register_recv_event(&mbus_dump_recv_event);
|
||||
}
|
||||
|
||||
if ((handle = mbus_connect_tcp(host, port)) == NULL)
|
||||
if ((handle = mbus_context_tcp(host, port)) == NULL)
|
||||
{
|
||||
fprintf(stderr, "Could not initialize M-Bus context: %s\n", mbus_error_str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!mbus_connect(handle))
|
||||
{
|
||||
fprintf(stderr, "Failed to setup connection to M-bus gateway\n");
|
||||
return 1;
|
||||
@ -147,6 +153,7 @@ main(int argc, char **argv)
|
||||
}
|
||||
|
||||
mbus_disconnect(handle);
|
||||
mbus_context_free(handle);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
11
bin/mbus-tcp-scan-secondary.c
Normal file → Executable file
11
bin/mbus-tcp-scan-secondary.c
Normal file → Executable file
@ -57,9 +57,15 @@ main(int argc, char **argv)
|
||||
return 1;
|
||||
}
|
||||
|
||||
if ((handle = mbus_connect_tcp(host, port)) == NULL)
|
||||
if ((handle = mbus_context_tcp(host, port)) == NULL)
|
||||
{
|
||||
fprintf(stderr, "Failed to setup connection to M-bus gateway: %s\n", mbus_error_str());
|
||||
fprintf(stderr, "Could not initialize M-Bus context: %s\n", mbus_error_str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!mbus_connect(handle))
|
||||
{
|
||||
fprintf(stderr, "Failed to setup connection to M-bus gateway\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -103,6 +109,7 @@ main(int argc, char **argv)
|
||||
mbus_scan_2nd_address_range(handle, 0, addr_mask);
|
||||
|
||||
mbus_disconnect(handle);
|
||||
mbus_context_free(handle);
|
||||
|
||||
//printf("Summary: Tried %ld address masks and found %d devices.\n", probe_count, match_count);
|
||||
|
||||
|
@ -51,7 +51,13 @@ main(int argc, char **argv)
|
||||
mbus_register_recv_event(&mbus_dump_recv_event);
|
||||
}
|
||||
|
||||
if ((handle = mbus_connect_tcp(host, port)) == NULL)
|
||||
if ((handle = mbus_context_tcp(host, port)) == NULL)
|
||||
{
|
||||
printf("Scan failed: Could not initialize M-Bus context: %s\n", mbus_error_str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!mbus_connect(handle))
|
||||
{
|
||||
printf("Scan failed: Could not setup connection to M-bus gateway: %s\n", mbus_error_str());
|
||||
return 1;
|
||||
@ -110,6 +116,7 @@ main(int argc, char **argv)
|
||||
}
|
||||
|
||||
mbus_disconnect(handle);
|
||||
mbus_context_free(handle);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -45,9 +45,15 @@ main(int argc, char **argv)
|
||||
return 1;
|
||||
}
|
||||
|
||||
if ((handle = mbus_connect_tcp(host, port)) == NULL)
|
||||
if ((handle = mbus_context_tcp(host, port)) == NULL)
|
||||
{
|
||||
printf("Failed to setup connection to M-bus gateway: %s\n", mbus_error_str());
|
||||
fprintf(stderr, "Could not initialize M-Bus context: %s\n", mbus_error_str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!mbus_connect(handle))
|
||||
{
|
||||
fprintf(stderr, "Failed to setup connection to M-bus gateway\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -99,6 +105,7 @@ main(int argc, char **argv)
|
||||
|
||||
free(addr);
|
||||
mbus_disconnect(handle);
|
||||
mbus_context_free(handle);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -1336,7 +1336,7 @@ mbus_context_serial(const char *device)
|
||||
handle->close = mbus_serial_disconnect;
|
||||
handle->recv = mbus_serial_recv_frame;
|
||||
handle->send = mbus_serial_send_frame;
|
||||
handle->free_auxdata = mbus_tcp_data_free;
|
||||
handle->free_auxdata = mbus_serial_data_free;
|
||||
|
||||
if ((serial_data->device = strdup(device)) == NULL)
|
||||
{
|
||||
@ -1403,7 +1403,7 @@ mbus_context_free(mbus_handle * handle)
|
||||
}
|
||||
|
||||
int
|
||||
modbus_connect(mbus_handle * handle)
|
||||
mbus_connect(mbus_handle * handle)
|
||||
{
|
||||
if (handle == NULL)
|
||||
{
|
||||
|
@ -20,9 +20,11 @@
|
||||
* the data processing.
|
||||
* Typical use might be (in oversimplified "pseudocode"):
|
||||
* \verbatim
|
||||
* mbus_handle = mbus_connect_serial(device);
|
||||
* mbus_handle = mbus_context_serial(device);
|
||||
* or
|
||||
* mbus_handle = mbus_connect_tcp(host, port);
|
||||
* mbus_handle = mbus_context_tcp(host, port);
|
||||
*
|
||||
* mbus_connect(mbus_handle);
|
||||
*
|
||||
* ...
|
||||
*
|
||||
@ -45,6 +47,7 @@
|
||||
* ...
|
||||
*
|
||||
* mbus_disconnect(mbus_handle);
|
||||
* mbus_context_free(mbus_handle);
|
||||
* \endverbatim
|
||||
*
|
||||
* Note that the quantity values are partially "normalized". For example energy
|
||||
|
@ -81,8 +81,8 @@ mbus_tcp_connect(mbus_handle *handle)
|
||||
}
|
||||
|
||||
// Set a timeout
|
||||
time_out.tv_sec = 0; //seconds
|
||||
time_out.tv_usec = 100000;
|
||||
time_out.tv_sec = 4; //seconds
|
||||
time_out.tv_usec = 0;
|
||||
setsockopt(handle->fd, SOL_SOCKET, SO_SNDTIMEO, &time_out, sizeof(time_out));
|
||||
setsockopt(handle->fd, SOL_SOCKET, SO_RCVTIMEO, &time_out, sizeof(time_out));
|
||||
|
||||
@ -189,7 +189,7 @@ retry:
|
||||
goto retry;
|
||||
|
||||
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
||||
mbus_error_str_set("M-Bus tcp transport layer timeout has been reached.");
|
||||
mbus_error_str_set("M-Bus tcp transport layer response timeout has been reached.");
|
||||
return -3;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user