How to merge two bytes in matlab?
22 次查看(过去 30 天)
显示 更早的评论
Hello all, consider that I receive digital data in matlab from ESP8266 through serial port. The digital data that I get from ESP is 16 bit (2 bytes) data which I initially write it to serial port and then to a text file using matlab. But, in matlab when I acquire that data and write into a text file, at a time only 8 bits of data I can write. (ie.,a single 16 bits data will be written into text file as two separate 8 bits data in little endian format). Now the questions are, (1) when I want to plot the data written into that text file that i need to merge the two successive data and then plot right? (2) how I can achieve that?
5 个评论
Guillaume
2016-10-14
The sample of the text file that you show does not match the description you gave. 1DC, is not 8 bits, it's at least 24 bits. Each character of an hexadecimal string is 8-bit, so 3 characters is 24bits.
In the end, if you do want to merge 1DC with 31A, what number do you want: 1DC31A, 1DC031A, something else?
What about the 3 with the 0? Are they to be understood as 24-bit each as well to make 3000, 32-bits to make 30000, or just 8-bit to make 30?
When you received data from the serial port do you actually receive hex characters, that is you first receive the character '1' (whose 8-bit ASCII value is 31 in hexadecimal), then the character 'D' (whose 8-bit ASCII value is 44 in hexadecimal), etc. Or do you actually receive one byte whose ascii value is 1D?
Walter Roberson
2016-10-14
Each hex character represents 4 bits, so 3 hex characters represents 12 bits. Merging adjacent pairs would require at least 24 bits.
回答(2 个)
Guillaume
2016-10-6
编辑:Guillaume
2016-10-6
It's really not clear how you wrote the files to start with. If the digital data is a 16-bit integer, why (and how) do you write it as text?
Assuming, your data has been written in a binary file that is just a stream of bits, you can read it directly as numbers:
fid = fopen('C:\somepath\somefile', 'r', 'le'); %open the file as binary, little endian format
data = fread(fid, Inf, 'uint16'); %read all the file as 16-bit unsigned integer
fclose(fid);
2 个评论
Guillaume
2016-10-14
I'm really lost as to what you're trying to do (see also my latest comment to your question). The answer I gave was about reading from a file, not a serial port.
Yes, you cannot set the DataBits property of a serial port to 16. The maximum is 8. Note that this has nothing to do with how you read the data.
If you want to read the data as 16-bit integer from the port, one integer at a time, then:
fread(s, 1, '*uint16')
will work. You can read more than one integer at a time, but your cnt must be less than or equal to inputBufferSize / 2 (as the error message told you), so 2048 is the maximum for cnt. You cannot use Inf for a serial port, that only works for files.
Note that for the above, I am assuming that your serial port is sending bytes, i.e. for example it is sending 01110101 in binary (73 in hex, 115 in decimal).
If you serial port is sending characters which are the encoding of bytes in hexadecimal (i.e. when transmitting the hex value 73, it first sends the character '7' (so the actual byte being transmitted is the ASCII value of '7' as hex = 37), then character '3' (Hex ASCII: 33), then the above is not the way to read your values.
Please clarify.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Entering Commands 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!