Read UART with different length

⚠️
Hi there.. thanks for coming to the forums. Exciting news! we’re now in the process of moving to our new forum platform that will offer better functionality and is contained within the main Dialog website. All posts and accounts have been migrated. We’re now accepting traffic on the new forum only - please POST any new threads at//www.wsdof.com/support. We’ll be fixing bugs / optimising the searching and tagging over the coming days.
4 posts / 0 new
Last post
dx3gerst
Offline
Last seen:3 years 8 months ago
加入:2017-03-29 13:35
Read UART with different length

Hi Dialog,

there is a Problem with reading different lengths of UART-Messages from my sensor. The standard-length of the received UART is 4 Byte, but sometimes I will receive a 10 Byte message.
Byte 3 and 4 of the input-Array are datacounter, which only are non equal to Zero in case of a Input Array longer than 4 Byte.

So my Approach is to always read the length of 4 Byte by the command:

uart_buf[4];
uart_buf_long[6];

...
ad_uart_read(dev, uart_buf, 4, OS_EVENT_FOREVER);

Then I check the datacounter-bytes:
if(uart_buf[2] != 0 || uart_buf[3] !=0){
ad_uart_read(dev,uart_buf_long, 6, OS_EVENT_FOREVER);
}

So the first part with reading 4 Bytes is working quite well, but in case I get the 10 Byte UART-message I will get stuck in the second ad_uart_read Routine.
So can you please tell me how to manage this Problem?

Thanks

Device:
MT_dialog
Offline
Last seen:1 month 3 weeks ago
Staff
加入:2015-06-08 11:34
Hi dx3gerst,

Hi dx3gerst,

I dont see anything wrong with the code that you 've pasted, the ad_uart_read() will block the task until it gets the amount of data from the UART, so as long as you send 6 bytes from the sensor then the task should unblock. If that is not working then perhaps the device has fell in sleep mode and cannot receive any data from the UART ? Have you tried to test your code without using any sleep mode ? Are you sure that the sensor sends that extra 6 bytes of data that you expect ?

Thanks MT_dialog

dx3gerst
Offline
Last seen:3 years 8 months ago
加入:2017-03-29 13:35
Hi Dialog,

Hi Dialog,

thank you for your answer. My code is based on the ble_multi_link demo. I don't think there is any sleep-mode implemented. respectively the code for sleep mode is put in comments

/* Set the desired sleep mode. */
/ / pm_set_wakeup_mode(真正的);
// pm_set_sleep_mode(pm_mode_extended_sleep);

So the device should be in active mode all the time, right?
Yes, I checked the Bytes sent by the sensor with an terminal-tool and it sends definitively 10 Bytes, which consist of the 4 "standard-Bytes", I always receive and the 6 additional Bytes.

Is there maybe any other approach to realise this functionality?

Thanks!

MT_dialog
Offline
Last seen:1 month 3 weeks ago
Staff
加入:2015-06-08 11:34
Hi dx3gerst,

Hi dx3gerst,

Yes, if you comment out those functions then the device will not go in sleep mode, although, what is suggested would be to set the pm_set_sleep_mode(pm_mode_active); and the pm_set_wakeup_mode(true) doesn't have to do with the sleeping of the device but with the waking up procedure, so it wont affect your code.

I dont see anything wrong with the code and i have also tried it on my side and i could start the BLE activity via waiting 10 bytes from UART using the code below in the system_init() function:

dev = ad_uart_open(SERIAL2);

char uart_buf[4];
char uart_buf_long[6];

ad_uart_read(dev, uart_buf, 4, OS_EVENT_FOREVER);

if(uart_buf[2] != 0 || uart_buf[3] !=0){
ad_uart_read(dev,uart_buf_long, 6, OS_EVENT_FOREVER);
}

Perhaps you could try taking the bytes from the senosr one by one and check if that works, but in any case you will have to debug it in order to check why that happens.

Thanks MT_dialog