Sync behaviour from serial to TCP connections
This commit is contained in:
@ -20,12 +20,12 @@
|
|||||||
static int debug = 0;
|
static int debug = 0;
|
||||||
|
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
// Execution starts here:
|
// Scan for devices using secondary addressing.
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
int
|
int
|
||||||
main(int argc, char **argv)
|
main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
mbus_frame reply, *reply_iter;
|
mbus_frame *frame, reply;
|
||||||
mbus_frame_data reply_data;
|
mbus_frame_data reply_data;
|
||||||
mbus_handle *handle = NULL;
|
mbus_handle *handle = NULL;
|
||||||
|
|
||||||
@ -67,6 +67,33 @@ main(int argc, char **argv)
|
|||||||
fprintf(stderr, "Failed to setup connection to M-bus gateway\n");
|
fprintf(stderr, "Failed to setup connection to M-bus gateway\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// init slave to get really the beginning of the records
|
||||||
|
//
|
||||||
|
|
||||||
|
frame = mbus_frame_new(MBUS_FRAME_TYPE_SHORT);
|
||||||
|
|
||||||
|
if (frame == NULL)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Failed to allocate mbus frame.\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
frame->control = MBUS_CONTROL_MASK_SND_NKE | MBUS_CONTROL_MASK_DIR_M2S;
|
||||||
|
frame->address = MBUS_ADDRESS_BROADCAST_NOREPLY;
|
||||||
|
|
||||||
|
if (debug)
|
||||||
|
printf("%s: debug: sending init frame\n", __PRETTY_FUNCTION__);
|
||||||
|
|
||||||
|
if (mbus_send_frame(handle, frame) == -1)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Failed to send mbus frame.\n");
|
||||||
|
mbus_frame_free(frame);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
mbus_recv_frame(handle, &reply);
|
||||||
|
|
||||||
if (strlen(addr_str) == 16)
|
if (strlen(addr_str) == 16)
|
||||||
{
|
{
|
||||||
@ -125,7 +152,9 @@ main(int argc, char **argv)
|
|||||||
fprintf(stderr, "Failed to generate XML representation of MBUS frames: %s\n", mbus_error_str());
|
fprintf(stderr, "Failed to generate XML representation of MBUS frames: %s\n", mbus_error_str());
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("%s", xml_result);
|
printf("%s", xml_result);
|
||||||
|
free(xml_result);
|
||||||
|
|
||||||
mbus_disconnect(handle);
|
mbus_disconnect(handle);
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -29,7 +29,7 @@ main(int argc, char **argv)
|
|||||||
mbus_frame_data reply_data;
|
mbus_frame_data reply_data;
|
||||||
mbus_handle *handle = NULL;
|
mbus_handle *handle = NULL;
|
||||||
|
|
||||||
char *host, *addr_str, matching_addr[16];
|
char *host, *addr_str, matching_addr[16], *xml_result;
|
||||||
int port, address;
|
int port, address;
|
||||||
|
|
||||||
memset((void *)&reply, 0, sizeof(mbus_frame));
|
memset((void *)&reply, 0, sizeof(mbus_frame));
|
||||||
@ -130,7 +130,15 @@ main(int argc, char **argv)
|
|||||||
fprintf(stderr, "M-bus data parse error.\n");
|
fprintf(stderr, "M-bus data parse error.\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
printf("%s", mbus_frame_data_xml(&reply_data));
|
|
||||||
|
if ((xml_result = mbus_frame_data_xml(&reply_data)) == NULL)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Failed to generate XML representation of MBUS frame: %s\n", mbus_error_str());
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("%s", xml_result);
|
||||||
|
free(xml_result);
|
||||||
|
|
||||||
// manual free
|
// manual free
|
||||||
if (reply_data.data_var.record)
|
if (reply_data.data_var.record)
|
||||||
|
@ -17,8 +17,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <mbus/mbus.h>
|
#include <mbus/mbus.h>
|
||||||
|
|
||||||
static mbus_handle *handle = NULL;
|
static int debug = 0;
|
||||||
|
|
||||||
|
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
// Execution starts here:
|
// Execution starts here:
|
||||||
@ -28,6 +27,10 @@ main(int argc, char **argv)
|
|||||||
{
|
{
|
||||||
char *host, *addr_mask;
|
char *host, *addr_mask;
|
||||||
int port;
|
int port;
|
||||||
|
mbus_handle *handle = NULL;
|
||||||
|
mbus_frame *frame = NULL, reply;
|
||||||
|
|
||||||
|
memset((void *)&reply, 0, sizeof(mbus_frame));
|
||||||
|
|
||||||
if (argc != 4 && argc != 3)
|
if (argc != 4 && argc != 3)
|
||||||
{
|
{
|
||||||
@ -59,6 +62,43 @@ main(int argc, char **argv)
|
|||||||
fprintf(stderr, "Failed to setup connection to M-bus gateway: %s\n", mbus_error_str());
|
fprintf(stderr, "Failed to setup connection to M-bus gateway: %s\n", mbus_error_str());
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
frame = mbus_frame_new(MBUS_FRAME_TYPE_SHORT);
|
||||||
|
|
||||||
|
if (frame == NULL)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Failed to allocate mbus frame.\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// init slaves
|
||||||
|
//
|
||||||
|
frame->control = MBUS_CONTROL_MASK_SND_NKE | MBUS_CONTROL_MASK_DIR_M2S;
|
||||||
|
frame->address = 0xFD;
|
||||||
|
|
||||||
|
if (mbus_send_frame(handle, frame) == -1)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Failed to send SND_NKE #1.\n");
|
||||||
|
mbus_frame_free(frame);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
(void) mbus_recv_frame(handle, &reply);
|
||||||
|
sleep(1);
|
||||||
|
|
||||||
|
frame->control = MBUS_CONTROL_MASK_SND_NKE | MBUS_CONTROL_MASK_DIR_M2S;
|
||||||
|
frame->address = 0xFF;
|
||||||
|
|
||||||
|
if (mbus_send_frame(handle, frame) == -1)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Failed to send SND_NKE #2.\n");
|
||||||
|
mbus_frame_free(frame);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
(void) mbus_recv_frame(handle, &reply);
|
||||||
|
sleep(1);
|
||||||
|
|
||||||
mbus_scan_2nd_address_range(handle, 0, addr_mask);
|
mbus_scan_2nd_address_range(handle, 0, addr_mask);
|
||||||
|
|
||||||
|
@ -27,6 +27,7 @@ main(int argc, char **argv)
|
|||||||
mbus_handle *handle;
|
mbus_handle *handle;
|
||||||
char *host;
|
char *host;
|
||||||
int port, address, debug = 0;
|
int port, address, debug = 0;
|
||||||
|
int ret;
|
||||||
|
|
||||||
if (argc == 3)
|
if (argc == 3)
|
||||||
{
|
{
|
||||||
@ -59,11 +60,13 @@ main(int argc, char **argv)
|
|||||||
|
|
||||||
if (debug)
|
if (debug)
|
||||||
printf("Scanning primary addresses:\n");
|
printf("Scanning primary addresses:\n");
|
||||||
|
|
||||||
for (address = 0; address < 254; address++)
|
for (address = 0; address <= 250; address++)
|
||||||
{
|
{
|
||||||
mbus_frame reply;
|
mbus_frame reply;
|
||||||
|
|
||||||
|
memset((void *)&reply, 0, sizeof(mbus_frame));
|
||||||
|
|
||||||
if (debug)
|
if (debug)
|
||||||
{
|
{
|
||||||
printf("%d ", address);
|
printf("%d ", address);
|
||||||
@ -75,16 +78,41 @@ main(int argc, char **argv)
|
|||||||
printf("Scan failed. Could not send ping frame: %s\n", mbus_error_str());
|
printf("Scan failed. Could not send ping frame: %s\n", mbus_error_str());
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ret = mbus_recv_frame(handle, &reply);
|
||||||
|
|
||||||
if (mbus_recv_frame(handle, &reply) == -1)
|
if (ret == -1)
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (debug)
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
if (ret == -2)
|
||||||
|
{
|
||||||
|
/* check for more data (collision) */
|
||||||
|
while (mbus_recv_frame(handle, &reply) != -1);
|
||||||
|
|
||||||
|
printf("Collision at address %d\n", address);
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if (mbus_frame_type(&reply) == MBUS_FRAME_TYPE_ACK)
|
if (mbus_frame_type(&reply) == MBUS_FRAME_TYPE_ACK)
|
||||||
{
|
{
|
||||||
if (debug)
|
/* check for more data (collision) */
|
||||||
printf("\n");
|
while (mbus_recv_frame(handle, &reply) != -1)
|
||||||
|
{
|
||||||
|
ret = -2;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ret == -2)
|
||||||
|
{
|
||||||
|
printf("Collision at address %d\n", address);
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
printf("Found a M-Bus device at address %d\n", address);
|
printf("Found a M-Bus device at address %d\n", address);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user