Merge pull request #13 from lategoodbye/master

Few bugfixes
This commit is contained in:
Robert Johansson 2012-07-05 06:30:52 -07:00
commit d7f63586c6
7 changed files with 40 additions and 13 deletions

View File

@ -98,10 +98,10 @@ main(int argc, char **argv)
}
frame->control = MBUS_CONTROL_MASK_SND_NKE | MBUS_CONTROL_MASK_DIR_M2S;
frame->address = MBUS_ADDRESS_BROADCAST_NOREPLY;
frame->address = MBUS_ADDRESS_NETWORK_LAYER;
if (debug)
printf("%s: debug: sending init frame\n", __PRETTY_FUNCTION__);
printf("%s: debug: sending init frame #1\n", __PRETTY_FUNCTION__);
if (mbus_send_frame(handle, frame) == -1)
{
@ -110,8 +110,27 @@ main(int argc, char **argv)
return 1;
}
mbus_recv_frame(handle, &reply);
while (mbus_recv_frame(handle, &reply) != -1);
//
// resend SND_NKE, maybe the first get lost
//
frame->control = MBUS_CONTROL_MASK_SND_NKE | MBUS_CONTROL_MASK_DIR_M2S;
frame->address = MBUS_ADDRESS_NETWORK_LAYER;
if (debug)
printf("%s: debug: sending init frame #2\n", __PRETTY_FUNCTION__);
if (mbus_send_frame(handle, frame) == -1)
{
fprintf(stderr, "Failed to send mbus frame.\n");
mbus_frame_free(frame);
return 1;
}
while (mbus_recv_frame(handle, &reply) != -1);
if (strlen(addr_str) == 16)
{
// secondary addressing
@ -147,7 +166,7 @@ main(int argc, char **argv)
// instead of the send and recv, use this sendrecv function that
// takes care of the possibility of multi-telegram replies (limit = 16 frames)
if (mbus_sendrecv_request(handle, address, &reply, 16) == -1)
if (mbus_sendrecv_request(handle, address, &reply, 16) != 0)
{
fprintf(stderr, "Failed to send/receive M-Bus request.\n");
return 1;

View File

@ -126,7 +126,7 @@ main(int argc, char **argv)
}
}
if (mbus_recv_frame(handle, &reply) == -1)
if (mbus_recv_frame(handle, &reply) != 0)
{
fprintf(stderr, "Failed to receive M-Bus response frame.\n");
return 1;

View File

@ -127,7 +127,7 @@ main(int argc, char **argv)
// init slaves
//
frame->control = MBUS_CONTROL_MASK_SND_NKE | MBUS_CONTROL_MASK_DIR_M2S;
frame->address = 0xFD;
frame->address = MBUS_ADDRESS_NETWORK_LAYER;
if (mbus_send_frame(handle, frame) == -1)
{
@ -139,7 +139,7 @@ main(int argc, char **argv)
(void) mbus_recv_frame(handle, &reply);
frame->control = MBUS_CONTROL_MASK_SND_NKE | MBUS_CONTROL_MASK_DIR_M2S;
frame->address = 0xFF;
frame->address = MBUS_ADDRESS_BROADCAST_NOREPLY;
if (mbus_send_frame(handle, frame) == -1)
{

View File

@ -130,7 +130,7 @@ main(int argc, char **argv)
// instead of the send and recv, use this sendrecv function that
// takes care of the possibility of multi-telegram replies (limit = 16 frames)
if (mbus_sendrecv_request(handle, address, &reply, 16) == -1)
if (mbus_sendrecv_request(handle, address, &reply, 16) != 0)
{
fprintf(stderr, "Failed to send/receive M-Bus request.\n");
return 1;

View File

@ -111,7 +111,7 @@ main(int argc, char **argv)
}
}
if (mbus_recv_frame(handle, &reply) == -1)
if (mbus_recv_frame(handle, &reply) != 0)
{
fprintf(stderr, "Failed to receive M-Bus response frame.\n");
return 1;

View File

@ -75,7 +75,7 @@ main(int argc, char **argv)
// init slaves
//
frame->control = MBUS_CONTROL_MASK_SND_NKE | MBUS_CONTROL_MASK_DIR_M2S;
frame->address = 0xFD;
frame->address = MBUS_ADDRESS_NETWORK_LAYER;
if (mbus_send_frame(handle, frame) == -1)
{
@ -88,7 +88,7 @@ main(int argc, char **argv)
sleep(1);
frame->control = MBUS_CONTROL_MASK_SND_NKE | MBUS_CONTROL_MASK_DIR_M2S;
frame->address = 0xFF;
frame->address = MBUS_ADDRESS_BROADCAST_NOREPLY;
if (mbus_send_frame(handle, frame) == -1)
{

View File

@ -1389,6 +1389,12 @@ mbus_recv_frame(mbus_handle * handle, mbus_frame *frame)
return 0;
}
if (frame == NULL)
{
MBUS_ERROR("%s: Invalid frame.\n", __PRETTY_FUNCTION__);
return 0;
}
if (handle->is_serial)
{
result = mbus_serial_recv_frame(handle->m_serial_handle, frame);
@ -1594,6 +1600,8 @@ mbus_sendrecv_request(mbus_handle *handle, int address, mbus_frame *reply, int m
//
next_frame = reply;
memset((void *)&reply_data, 0, sizeof(mbus_frame_data));
while (more_frames)
{
frame_count++;
@ -1601,7 +1609,7 @@ mbus_sendrecv_request(mbus_handle *handle, int address, mbus_frame *reply, int m
if (debug)
printf("%s: debug: receiving response frame #%d\n", __PRETTY_FUNCTION__, frame_count);
if (mbus_recv_frame(handle, next_frame) == -1)
if (mbus_recv_frame(handle, next_frame) != 0)
{
MBUS_ERROR("%s: Failed to receive M-Bus response frame.\n", __PRETTY_FUNCTION__);
retval = 1;
@ -1905,7 +1913,7 @@ int mbus_read_slave(mbus_handle * handle, mbus_address *address, mbus_frame * re
}
}
if (mbus_recv_frame(handle, reply) == -1)
if (mbus_recv_frame(handle, reply) != 0)
{
MBUS_ERROR("%s: Failed to receive M-Bus response frame.\n",
__PRETTY_FUNCTION__);