how can I find a short int array from a long int array

Now I have a long int array including some short array such beginning with 55 and ending 00, whose length was 10, but the contents between 55 and 00 could not be recognized for they were send from serial port. How could I extract those short arrays from the long array? Thanks!

4 Comments

How did you do the reading of the serial port?
I just read all the datas into a txt file, then I read it and got the long array! Thanks
How are you doing the reading from the text file? The details might matter.
I had a stm32f0, it just send datas from serial port to pc, I did not use any protocol

Sign in to comment.

 Accepted Answer

as_short = typecast(YourLongArray, 'uint16');
sub_pos = find(as_short(1:end-9) == 55 & as_short(10:end) == 0);
short_array = as_short(sub_pos:sub_pos+9);

8 Comments

Thanks! Is there any other to resolve this problem using regexp? For example, could I find an array like 55 xx xx xx xx xx xx xx 00 int the long array?
"Is there any other to resolve this problem using regexp"
No. int32 arrays and uint32 arrays would mostly overflow if converted to character strings, as any one character is only 16 bits. With your having an unknown 16 bit int as the second short, right after the 55 in the original array, then it is highly likely that you are going to overflow 16 bits, making it impossible to convert to char to regexp.
It is not completely guaranteed that you will overflow. When you typecast() a long int array into short int, then the first short of the pair is the least signficant if you are using Little Endian, which you almost certainly are (unless you are using SunOS or IRIX and a really old MATLAB.) So if the short int that was right after the 55 happened to be 0, then when it got swapped into the most significant byte when you char()'d the long int array, it would not change the value, leaving the numeric value as 55. However, any other value than 0 would certainly lead to char overflow.
I used tmtool to test the serial, set baudrate = 115200, did not set others, the timeout period is 10. I hope to read from serial port but it always said the unsuccessful read: the specified amount of data was not returned within the timeout period, what's the problem? Thanks
When you use a serial port and do not set termination mode to byte, then fscanf() without a count or fread() without a count will read the input until terminator is read or until the buffer is full or until a timeout is reached.
You should be checking that your terminator is the same as the transmitter is sending, and you should check whether the transmitter is sending binary bytes or is encoding the values as text.
A program such as TeraTerm is very useful to ensure that you have proper communications with the serial port, proper baud, proper terminator.
I set termination node to byte, sometimes I could receive datas but most time I could not, even could not open the port, why? I also used a tool like TeraTerm, it could receive datas every time exactly!
I thought I found the reason, first I shoud connect the port, then run the m file, at last power on the mcu so it sent datas and the m file read datas. I did not know why
You would normally need to write a protocol that triggers the mcu to send data, instead of just immediately sending it.
Perhaps you were thinking that when the MCU opens its serial port that the MCU would then wait untl MATLAB opens the serial port on its side before the MCU would proceed to send the data. It is possible to program serial ports that way, but it is not typically done on instruments.
Thanks for your suggestion

Sign in to comment.

More Answers (0)

Categories

Tags

Asked:

on 30 Apr 2017

Commented:

on 4 May 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!