// // init slave to get really the beginning of the records // static int init_slaves(mbus_handle *handle) { if (debug) printf("%s: debug: sending init frame #1\n", __PRETTY_FUNCTION__); if (mbus_send_ping_frame(handle, MBUS_ADDRESS_NETWORK_LAYER, 1) == -1) { return 0; } // // resend SND_NKE, maybe the first get lost // if (debug) printf("%s: debug: sending init frame #2\n", __PRETTY_FUNCTION__); if (mbus_send_ping_frame(handle, MBUS_ADDRESS_NETWORK_LAYER, 1) == -1) { return 0; } return 1; }
int ping_address(mbus_handle *handle, mbus_frame *reply, int address) { int i, ret = MBUS_RECV_RESULT_ERROR; memset((void *)reply, 0, sizeof(mbus_frame)); for (i = 0; i <= handle->max_retry; i++) { if (debug) { printf("%d ", address); fflush(stdout); } if (mbus_send_ping_frame(handle, address, 0) == -1) { fprintf(stderr,"Scan failed. Could not send ping frame: %s\n", mbus_error_str()); return MBUS_RECV_RESULT_ERROR; } ret = mbus_recv_frame(handle, reply); if (ret != MBUS_RECV_RESULT_TIMEOUT) { return ret; } } return ret; }
//------------------------------------------------------------------------------ // Primary addressing scanning of mbus devices. //------------------------------------------------------------------------------ int main(int argc, char **argv) { mbus_handle *handle; char *device; int address, baudrate = 2400, timeout = 200; fprintf(stderr, "Hello, welcome to mbus\n"); if (argc == 2) { device = argv[1]; } else if (argc == 4 && strcmp(argv[1], "-b") == 0) { baudrate = atoi(argv[2]); device = argv[3]; } else if (argc == 4 && strcmp(argv[1], "-t") == 0) { timeout = atoi(argv[2]); device = argv[3]; } else if (argc == 6 && strcmp(argv[1], "-b") == 0 && strcmp(argv[3], "-t") == 0) { baudrate = atoi(argv[2]); timeout = atoi(argv[4]); device = argv[5]; } else { fprintf(stderr, "usage: %s [-b BAUDRATE] [-t TIMEOUT] device\n", argv[0]); return 0; } fprintf(stderr, "Going to use device %s at baud %d and timeout %d\n", device, baudrate, timeout); if ((handle = mbus_connect_serial(device)) == NULL) { printf("Failed to setup connection to M-bus gateway\n"); return -1; } if (mbus_serial_set_baudrate(handle->m_serial_handle, baudrate) == -1) { printf("Failed to set baud rate.\n"); return -1; } if (mbus_serial_set_timeout(handle->m_serial_handle, timeout) == -1) { printf("Failed to set timeout.\n"); return -1; } for (address = 0; address < 254; address++) { mbus_frame reply; memset((void *)&reply, 0, sizeof(mbus_frame)); if (mbus_send_ping_frame(handle, address) == -1) { printf("Scan failed. Could not send ping frame to handle %d and address %d: %s\n", handle, address, mbus_error_str()); continue; // return -1; } if (mbus_recv_frame(handle, &reply) == -1) { printf("No reply address %d\n", address); continue; } else { printf("Got a reply\n"); mbus_hexdump((const char * )&reply, reply.length1); } int frame_type= mbus_frame_type(&reply); if (frame_type == MBUS_FRAME_TYPE_ACK) { printf("Found a M-Bus device at address %d \n", address); } else { printf("not found addr:%d got frame type %d\n", address, frame_type); } } mbus_disconnect(handle); return 0; }
//------------------------------------------------------------------------------ // Primary addressing scanning of mbus devices. //------------------------------------------------------------------------------ int main(int argc, char **argv) { mbus_handle *handle; char *device; int address, baudrate = 9600; int ret; if (argc == 2) { device = argv[1]; } else if (argc == 3 && strcmp(argv[1], "-d") == 0) { debug = 1; device = argv[2]; } else if (argc == 4 && strcmp(argv[1], "-b") == 0) { baudrate = atoi(argv[2]); device = argv[3]; } else if (argc == 5 && strcmp(argv[1], "-d") == 0 && strcmp(argv[2], "-b") == 0) { debug = 1; baudrate = atoi(argv[3]); device = argv[4]; } else { fprintf(stderr, "usage: %s [-d] [-b BAUDRATE] device\n", argv[0]); return 0; } if (debug) { mbus_register_send_event(&mbus_dump_send_event); mbus_register_recv_event(&mbus_dump_recv_event); } 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) == -1) { 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; } if (debug) printf("Scanning primary addresses:\n"); for (address = 0; address <= 250; address++) { mbus_frame reply; memset((void *)&reply, 0, sizeof(mbus_frame)); if (debug) { printf("%d ", address); fflush(stdout); } if (mbus_send_ping_frame(handle, address, 0) == -1) { printf("Scan failed. Could not send ping frame: %s\n", mbus_error_str()); return 1; } ret = mbus_recv_frame(handle, &reply); if (ret == MBUS_RECV_RESULT_TIMEOUT) { continue; } if (debug) printf("\n"); if (ret == MBUS_RECV_RESULT_INVALID) { /* check for more data (collision) */ mbus_purge_frames(handle); printf("Collision at address %d\n", address); continue; } if (mbus_frame_type(&reply) == MBUS_FRAME_TYPE_ACK) { /* check for more data (collision) */ if (mbus_purge_frames(handle)) { printf("Collision at address %d\n", address); continue; } printf("Found a M-Bus device at address %d\n", address); } } mbus_disconnect(handle); mbus_context_free(handle); return 0; }