nrf51822 nrfjprog.exe ERROR: Invalid serial number!

本文介绍了解决J-Link出现“Invalid serial number”错误的方法。通过更改J-Link的序列号从10位为9位,并重新插拔设备完成设置。文中还提供了使用J-Link Commander进行序列号修改的具体步骤。

遇到问题:
Hi,I use nRFgo Studio to erase nRF51822, like this
这里写图片描述
but it display: ERROR: Invalid serial number!
这里写图片描述

Is this a J-link? I’ve never seen a J-Link with a 10 digit serial number, but maybe I’m wrong. Try to open J-Link configurator and see if every thing looks OK: www.segger.com/configurator.html. Maybe it’s not a genuine one.
Yes, it’s a J-link. Everything is ok after I changed the J-link S/N with a 9 digit number. Thanks for your answer.
I’m glad it worked out for you. Please accept my answer if it is correct.

来自:https://devzone.nordicsemi.com/f/nordic-q-a/11326/nrfjprog-exe-error-invalid-serial-number

具体解决方法:
安装 Setup_V512e.exe 或者类似软件:

在开始菜单:
这里写图片描述

将10位的数字 修改为9位的数字 SN 序列号

修改完之后,需要重新插拔,重新上电 一下 j-link

https://blog.youkuaiyun.com/tcm455090672/article/details/79856973

打开J-Link commander,如下图:
这里写图片描述

J-Link commander界面如下:
这里写图片描述

输入:exec setsn=xxxxxxxx

/* * 简化的Apple设备扫描和Model获取工具 - 修复连接超时问题 */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include "btstack.h" // 配置参数 #define SCAN_INTERVAL 0x0030 #define SCAN_WINDOW 0x0030 #define CONNECTION_TIMEOUT_MS 10000 #define SERVICE_QUERY_TIMEOUT_MS 8000 #define CONNECTION_STABLE_DELAY_MS 1000 // 全局状态 static enum { APP_STATE_IDLE, APP_STATE_SCANNING, APP_STATE_W4_SCAN_STOP, APP_STATE_CONNECTING, APP_STATE_CONNECTED, APP_STATE_W4_CONNECTION_READY, APP_STATE_QUERYING_SERVICES } app_state = APP_STATE_IDLE; static hci_con_handle_t connection_handle = HCI_CON_HANDLE_INVALID; static btstack_timer_source_t connection_timer; static btstack_timer_source_t query_timer; static btstack_timer_source_t scan_timer; static btstack_timer_source_t ready_timer; static bd_addr_t target_device; static uint8_t target_addr_type; static int query_attempted = 0; static int connection_retry_count = 0; // 前向声明 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); static void start_device_info_query(void); // 简单的Apple设备检测 static int is_apple_device(const uint8_t *adv_data, uint8_t adv_len) { ad_context_t context; for (ad_iterator_init(&context, adv_len, (uint8_t *)adv_data); ad_iterator_has_more(&context); ad_iterator_next(&context)) { if (ad_iterator_get_data_type(&context) == BLUETOOTH_DATA_TYPE_MANUFACTURER_SPECIFIC_DATA) { uint8_t data_len = ad_iterator_get_data_len(&context); const uint8_t *data = ad_iterator_get_data(&context); if (data_len >= 2 && little_endian_read_16(data, 0) == 0x004C) { return 1; } } } return 0; } // 检查是否为可连接设备 static int is_connectable_device(uint8_t event_type) { return (event_type == 0x00 || event_type == 0x01); // ADV_IND or ADV_DIRECT_IND } // 连接超时处理 static void connection_timeout_handler(btstack_timer_source_t *ts) { UNUSED(ts); printf("=== CONNECTION TIMEOUT ===\n"); if (app_state == APP_STATE_CONNECTING) { printf("Connection attempt timed out, resuming scan\n"); // 连接超时后直接恢复扫描状态 app_state = APP_STATE_SCANNING; gap_start_scan(); } else if (app_state == APP_STATE_QUERYING_SERVICES) { printf("Service query timed out, disconnecting\n"); gap_disconnect(connection_handle); } } // 查询超时处理 static void query_timeout_handler(btstack_timer_source_t *ts) { UNUSED(ts); printf("=== QUERY TIMEOUT ===\n"); if (app_state == APP_STATE_QUERYING_SERVICES) { printf("Service query timed out, disconnecting\n"); gap_disconnect(connection_handle); } } // 连接就绪处理 static void connection_ready_handler(btstack_timer_source_t *ts) { UNUSED(ts); printf("Connection is now ready for GATT operations\n"); start_device_info_query(); } // 扫描停止后连接处理 static void scan_stopped_handler(btstack_timer_source_t *ts) { UNUSED(ts); printf("Scan stopped, attempting connection...\n"); app_state = APP_STATE_CONNECTING; query_attempted = 0; connection_retry_count = 0; // 连接设备 printf("Attempting connection to %s...\n", bd_addr_to_str(target_device)); uint8_t status = gap_connect(target_device, target_addr_type); if (status != ERROR_CODE_SUCCESS) { printf("Connection request failed: 0x%02x, resuming scan\n", status); app_state = APP_STATE_SCANNING; gap_start_scan(); return; } printf("Connection request sent successfully\n"); // 设置连接超时 btstack_run_loop_set_timer_handler(&connection_timer, connection_timeout_handler); btstack_run_loop_set_timer(&connection_timer, CONNECTION_TIMEOUT_MS); btstack_run_loop_add_timer(&connection_timer); } // 安全的定时器操作 static void safe_remove_timer(btstack_timer_source_t *timer) { btstack_run_loop_remove_timer(timer); } static void safe_add_timer(btstack_timer_source_t *timer, uint32_t timeout_ms) { safe_remove_timer(timer); btstack_run_loop_set_timer(timer, timeout_ms); btstack_run_loop_add_timer(timer); } // 开始设备信息服务查询 // 修改start_device_info_query函数,添加更多调试信息 static void start_device_info_query(void) { printf("Starting Device Information Service query...\n"); app_state = APP_STATE_QUERYING_SERVICES; query_attempted = 1; // 设置查询超时 btstack_run_loop_set_timer_handler(&query_timer, query_timeout_handler); safe_add_timer(&query_timer, 15000); // 增加到15秒 printf("Calling device_information_service_client_query...\n"); uint8_t status = device_information_service_client_query(connection_handle, packet_handler); printf("device_information_service_client_query returned: 0x%02x\n", status); if (status != ERROR_CODE_SUCCESS) { printf("Device Information Service query failed: 0x%02x\n", status); if (status == 0x0c) { // COMMAND_DISALLOWED printf("Command disallowed - connection not ready yet\n"); connection_retry_count++; if (connection_retry_count < 3) { printf("Retrying query in 1000ms (attempt %d)...\n", connection_retry_count); btstack_run_loop_set_timer_handler(&ready_timer, connection_ready_handler); safe_add_timer(&ready_timer, 1000); return; } else { printf("Max retries reached, trying service discovery...\n"); } } // 备用方案:手动发现服务 printf("Trying manual service discovery...\n"); status = gatt_client_discover_primary_services_by_uuid16(packet_handler, connection_handle, 0x180A); if (status != ERROR_CODE_SUCCESS) { printf("Manual service discovery also failed: 0x%02x\n", status); gap_disconnect(connection_handle); } } else { printf("Device Information Service query started successfully\n"); connection_retry_count = 0; } } // 延迟后开始查询 static void delayed_query_handler(btstack_timer_source_t *ts) { UNUSED(ts); printf("Connection stable, waiting for GATT readiness...\n"); app_state = APP_STATE_W4_CONNECTION_READY; // 等待连接完全就绪 btstack_run_loop_set_timer_handler(&ready_timer, connection_ready_handler); safe_add_timer(&ready_timer, CONNECTION_STABLE_DELAY_MS); } // 延迟后重新扫描 static void delayed_scan_handler(btstack_timer_source_t *ts) { UNUSED(ts); app_state = APP_STATE_SCANNING; gap_start_scan(); printf("Resumed scanning for Apple devices...\n"); } // 处理GATT服务事件 static void handle_gatt_service_event(const uint8_t *packet) { uint8_t subevent = hci_event_gattservice_meta_get_subevent_code(packet); uint8_t att_status; const char *value; switch (subevent) { case GATTSERVICE_SUBEVENT_DEVICE_INFORMATION_MANUFACTURER_NAME: att_status = gattservice_subevent_device_information_manufacturer_name_get_att_status(packet); if (att_status == ATT_ERROR_SUCCESS) { value = gattservice_subevent_device_information_manufacturer_name_get_value(packet); printf(">>> Manufacturer: %s\n", value); } else { printf("Manufacturer read failed: 0x%02x\n", att_status); } break; case GATTSERVICE_SUBEVENT_DEVICE_INFORMATION_MODEL_NUMBER: att_status = gattservice_subevent_device_information_model_number_get_att_status(packet); if (att_status == ATT_ERROR_SUCCESS) { value = gattservice_subevent_device_information_model_number_get_value(packet); printf(">>> === SUCCESS: MODEL NUMBER: %s ===\n", value); printf(">>> Device: %s - Model: %s\n", bd_addr_to_str(target_device), value); connection_retry_count = 0; } else { printf("Model number read failed: 0x%02x\n", att_status); } break; case GATTSERVICE_SUBEVENT_DEVICE_INFORMATION_SERIAL_NUMBER: att_status = gattservice_subevent_device_information_serial_number_get_att_status(packet); if (att_status == ATT_ERROR_SUCCESS) { value = gattservice_subevent_device_information_serial_number_get_value(packet); printf(">>> Serial: %s\n", value); } break; case GATTSERVICE_SUBEVENT_DEVICE_INFORMATION_DONE: att_status = gattservice_subevent_device_information_done_get_att_status(packet); printf("Device info query completed with status: 0x%02x\n", att_status); safe_remove_timer(&query_timer); connection_retry_count = 0; // 查询完成,断开连接 printf("Disconnecting after query completion...\n"); gap_disconnect(connection_handle); break; default: break; } } // 处理GATT服务发现事件 static void handle_gatt_service_discovery(const uint8_t *packet) { uint8_t event_type = hci_event_packet_get_type(packet); switch (event_type) { case GATT_EVENT_SERVICE_QUERY_RESULT: { // 使用兼容的API uint16_t start_handle = gatt_event_service_query_result_get_handle(packet); printf("Found service, start handle: 0x%04x\n", start_handle); break; } case GATT_EVENT_QUERY_COMPLETE: { uint8_t att_status = gatt_event_query_complete_get_att_status(packet); printf("Service discovery complete, status: 0x%02x\n", att_status); safe_remove_timer(&query_timer); connection_retry_count = 0; if (att_status == ATT_ERROR_SUCCESS) { printf("Service discovery successful, but no Device Information Service found\n"); } // 断开连接 printf("Disconnecting after service discovery...\n"); gap_disconnect(connection_handle); break; } default: break; } } // 主事件处理函数 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) { UNUSED(channel); UNUSED(size); if (packet_type != HCI_EVENT_PACKET) return; uint8_t event_type = hci_event_packet_get_type(packet); switch (event_type) { case BTSTACK_EVENT_STATE: if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING) { printf("Bluetooth controller ready, starting scan...\n"); app_state = APP_STATE_SCANNING; gap_start_scan(); } break; case GAP_EVENT_ADVERTISING_REPORT: if (app_state == APP_STATE_SCANNING) { // 检查是否为Apple设备且可连接 uint8_t data_len = gap_event_advertising_report_get_data_length(packet); const uint8_t *data = gap_event_advertising_report_get_data(packet); uint8_t adv_event_type = gap_event_advertising_report_get_advertising_event_type(packet); printf_hexdump(data, data_len); int8_t rssi = gap_event_advertising_report_get_rssi(packet); if (is_apple_device(data, data_len) && is_connectable_device(adv_event_type) && rssi >-60) { // 获取设备地址 bd_addr_t addr; gap_event_advertising_report_get_address(packet, addr); uint8_t addr_type = gap_event_advertising_report_get_address_type(packet); printf("\n*** Found connectable Apple device: %s (RSSI: %d, Type: %d) ***\n", bd_addr_to_str(addr), rssi, adv_event_type); // 停止扫描 gap_stop_scan(); app_state = APP_STATE_W4_SCAN_STOP; // 保存目标设备 memcpy(target_device, addr, 6); target_addr_type = addr_type; // 等待扫描完全停止后再连接 printf("Waiting for scan to stop...\n"); btstack_run_loop_set_timer_handler(&scan_timer, scan_stopped_handler); safe_add_timer(&scan_timer, 100); } } break; case HCI_EVENT_META_GAP: switch (hci_event_gap_meta_get_subevent_code(packet)) { case GAP_SUBEVENT_LE_CONNECTION_COMPLETE: safe_remove_timer(&connection_timer); uint8_t data_len = gap_event_advertising_report_get_data_length(packet); const uint8_t *data = gap_event_advertising_report_get_data(packet); uint8_t adv_event_type = gap_event_advertising_report_get_advertising_event_type(packet); printf_hexdump(data, data_len); uint8_t status = gap_subevent_le_connection_complete_get_status(packet); if (status == ERROR_CODE_SUCCESS) { connection_handle = gap_subevent_le_connection_complete_get_connection_handle(packet); printf("=== Connected successfully, handle: 0x%04x ===\n", connection_handle); app_state = APP_STATE_CONNECTED; // 延迟一下确保连接稳定,然后开始查询 printf("Waiting for connection to stabilize...\n"); btstack_run_loop_set_timer_handler(&connection_timer, delayed_query_handler); safe_add_timer(&connection_timer, 10); } else { printf("Connection failed: 0x%02x, resuming scan\n", status); app_state = APP_STATE_SCANNING; gap_start_scan(); } break; default: break; } break; case HCI_EVENT_DISCONNECTION_COMPLETE: printf("=== Disconnected, reason: 0x%02x", hci_event_disconnection_complete_get_reason(packet)); if (!query_attempted) { printf(" (No query attempted)"); } printf(" ===\n"); connection_handle = HCI_CON_HANDLE_INVALID; safe_remove_timer(&connection_timer); safe_remove_timer(&query_timer); safe_remove_timer(&ready_timer); // 短暂延迟后重新开始扫描 printf("Waiting before resuming scan...\n"); btstack_run_loop_set_timer_handler(&connection_timer, delayed_scan_handler); safe_add_timer(&connection_timer, 1000); break; case HCI_EVENT_GATTSERVICE_META: uint8_t data_len = gap_event_advertising_report_get_data_length(packet); const uint8_t *data = gap_event_advertising_report_get_data(packet); printf_hexdump(data, data_len); printf("HCI_EVENT_GATTSERVICE_META\n"); handle_gatt_service_event(packet); break; case GATT_EVENT_SERVICE_QUERY_RESULT: case GATT_EVENT_QUERY_COMPLETE: printf_hexdump(data, data_len); printf("GATT_EVENT_QUERY_COMPLETE\n"); handle_gatt_service_discovery(packet); break; default: break; } } // 专门为手表和耳机优化的连接参数 static void setup_optimal_connection_params(void) { printf("Setting optimal connection parameters for Apple devices...\n"); // 更保守的参数,适合连接不稳定的情况 gap_set_connection_parameters( 0x00C0, // conn_scan_interval: 192 * 0.625ms = 120ms 0x00C0, // conn_scan_window: 192 * 0.625ms = 120ms 0x0006, // conn_interval_min: 6 * 1.25ms = 7.5ms 0x000C, // conn_interval_max: 12 * 1.25ms = 15ms 0x0004, // conn_latency: 4 0x0190, // supervision_timeout: 400 * 10ms = 4s 0x0002, // min_ce_length: 2 * 0.625ms = 1.25ms 0x0004 // max_ce_length: 4 * 0.625ms = 2.5ms ); printf("Connection parameters optimized for Apple Watch/AirPods\n"); } // 初始化 static void setup(void) { printf("Initializing Bluetooth stack...\n"); // 初始化协议栈 l2cap_init(); sm_init(); // 设置安全管理器(无输入无输出) sm_set_io_capabilities(IO_CAPABILITY_NO_INPUT_NO_OUTPUT); // 设置GATT客户端 gatt_client_init(); device_information_service_client_init(); setup_optimal_connection_params(); // 配置扫描参数 gap_set_scan_parameters(1, SCAN_INTERVAL, SCAN_WINDOW); // 注册事件处理器 static btstack_packet_callback_registration_t hci_callback; hci_callback.callback = &packet_handler; hci_add_event_handler(&hci_callback); // 初始化定时器 memset(&connection_timer, 0, sizeof(connection_timer)); memset(&query_timer, 0, sizeof(query_timer)); memset(&scan_timer, 0, sizeof(scan_timer)); memset(&ready_timer, 0, sizeof(ready_timer)); printf("Bluetooth stack initialized\n"); } int btstack_main(void) { printf("\n========================================\n"); printf(" Apple Device Model Scanner\n"); printf(" Scanning for Apple devices...\n"); printf("========================================\n\n"); setup(); hci_power_control(HCI_POWER_ON); return 0; } 以上是我的代码,可以识别apple手机和MAC的内部型号,但是无法识别手表的内部型号。 我刚刚使用竞品和手机的BLE调试助手和nRF Connect for Desktop Bluetooth Low Energy都可以获取内部型号,使用nRF Connect for Desktop Bluetooth Low Energy获取的手机和手表的流程移植,以下是我的日志,帮忙分析一下 *** Found connectable Apple device: 66:15:EE:B7:7B:99 (RSSI: -40, Type: 0) *** out:0C 20 02 00 00 Waiting for scan to stop... Scan stopped, attempting connection... Attempting connection to 66:15:EE:B7:7B:99... out:0D 20 19 C0 00 C0 00 00 01 99 7B B7 EE 15 66 01 06 00 0C 00 04 00 90 01 02 00 04 00 Connection request sent successfully 15 66 00 00 00 00 00 00 00 00 00 00 00 00 0C 00 04 00 90 01 00 FF FF FF 2F 56 00 00 00 53 8C EC 56 36 76 F5 F0 B6 0E FE FD 7F 00 00 D3 6B 42 31 2F 56 00 00 70 F4 44 31 15 00 0D 00 AF 26 45 31 2F 56 00 00 10 B7 0E FE FD 7F 00 00 15 00 00 00 13 00 00 00 84 9C 15 69 00 00 00 00 61 EB 0B 00 00 00 00 00 5E 01 00 00 00 00 00 00 00 53 8C EC 56 36 76 F5 F0 B6 0E FE FD 7F 00 00 92 D2 42 31 2F 56 00 00 20 B7 0E FE FD 7F 00 00 00 53 8C EC 56 36 76 F5 AF 26 45 31 2F 56 00 00 00 00 00 00 00 00 00 00 20 B7 0E FE FD 7F 00 00 AA 6E 42 31 2F 56 00 00 AF 26 45 31 2F 56 00 00 15 00 43 31 04 56 00 00 5E 01 00 00 00 00 00 00 88 5C A4 39 F7 5A 00 01 40 B7 0E FE FD 7F 00 00 93 95 43 31 2F 56 00 00 50 B7 0E FE FD 7F 00 00 A2 05 === Connected successfully, handle: 0x0e4c === Waiting for connection to stabilize... Connection stable, waiting for GATT readiness... Connection is now ready for GATT operations Starting Device Information Service query... Calling device_information_service_client_query... device_information_service_client_query returned: 0x00 Device Information Service query started successfully === QUERY TIMEOUT === Service query timed out, disconnecting out:06 04 03 4C 0E 13 Device info query completed with status: 0x1f Disconnecting after query completion... === Disconnected, reason: 0x16 === *** Found connectable Apple device: 55:EB:71:D2:7B:AF (RSSI: -36, Type: 0) *** out:0C 20 02 00 00 Waiting for scan to stop... Scan stopped, attempting connection... Attempting connection to 55:EB:71:D2:7B:AF... out:0D 20 19 C0 00 C0 00 00 01 AF 7B D2 71 EB 55 01 06 00 0C 00 04 00 90 01 02 00 04 00 Connection request sent successfully EB 55 00 00 00 00 00 00 00 00 00 00 00 00 0C 00 04 00 90 01 00 FF FF FF 2F 56 00 00 00 53 8C EC 56 36 76 F5 F0 B6 0E FE FD 7F 00 00 D3 6B 42 31 2F 56 00 00 70 F4 44 31 15 00 0D 00 AF 26 45 31 2F 56 00 00 10 B7 0E FE FD 7F 00 00 15 00 00 00 13 00 00 00 96 9C 15 69 00 00 00 00 BD 82 04 00 00 00 00 00 70 01 00 00 00 00 00 00 00 53 8C EC 56 === Connected successfully, handle: 0x0e5b === Waiting for connection to stabilize... Connection stable, waiting for GATT readiness... Connection is now ready for GATT operations Starting Device Information Service query... Calling device_information_service_client_query... device_information_service_client_query returned: 0x00 Device Information Service query started successfully >>> Manufacturer: Apple Inc. >>> === SUCCESS: MODEL NUMBER: iPhone10,1 === >>> Device: 55:EB:71:D2:7B:AF - Model: iPhone10,1 Device info query completed with status: 0x00 Disconnecting after query completion... out:06 04 03 5B 0E 13 === Disconnected, reason: 0x16 === 第一个是手表、第二个是手机,首先应该排除超时问题,我使用nRF Connect for Desktop Bluetooth Low Energy,连接后立马获取到各个UUID的数据包括,内部型号,我还有一个疑问out:后面的数据是请求数据的打印,device_information_service_client_query不会触发请求打印吗,如果不会触发,手机是怎么得到的内部型号
最新发布
11-14
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值