From: Walter Roberson on 23 Jul 2010 12:37 Jose wrote: > %data(5:8)..my boss says...jose..is usperchan--number of microseonds per > sample, held as a single precision floating point number --need to write > a routine byte2single ---check the web for this format...Can you helpme > with it too? byte2single = @(b) typecast(b, 'single'); However, this depends upon the order of the bytes being the same between the master and slave. If you cannot be certain of that, then you will need to order the bytes into some known order for transmission, with the master or slave (or both) re-ordering the bytes to or from the common order. The standard common transmission format is most significant byte first, but Intel-type processors use a different internal ordering; swapbytes() might help. Re-ordering for transmission is not needed if the master and slave use the same processor family.
From: Jose on 23 Jul 2010 12:38 sorry, I forgot: function [longint]=byte2long (d); data=double(d); longint=data(1)+data(2)*(2^8)+data(3)*(2^16)+data(4)*(2^24); function [strout]=byte2string (data); data i=find(data==0); if length(i)==0 strout=[]; else 'que fuerte' strout=char(data(1:i)') end "Jose " <jose.l.vega(a)gmail.com> wrote in message <i2cfv7$d6u$1(a)fred.mathworks.com>... > Walter, this is the data send from the master to the slave: > > case sm_adc > > %sending 4 bytes; > -------- > %sending 40 bytes: > > data=data(1:40) > ptsperchan=byte2long(data(1:4)); > %data(5:8)..my boss says...jose..is usperchan--number of microseonds per sample, held as a single precision floating point number --need to write a routine byte2single ---check the web for this format...Can you helpme with it too? > > stimname=byte2string(data(9:29))) > stimtype=byte2long(data(33:36)); > datasize=byte2long(data(37:40)); > > And this is the information receive in my slave (the original code of my first message): > > case sm_adc > > % receiving 44 bytes > sdata.ptsperchan=typecast(data(5:8),'uint32'); > sdata.usperadc=typecast(data(9:12),'single'); > sdata.stimname=byte2string(data(13:33)); > sdata.stimtype=typecast(data(37:40),'uint32'); > sdata.datasize=typecast(data(41:44),'uint32'); > adc=msrecvraw (sock,sdata.datasize); > > it has sense now?...please can you explain to me it better....I think He wrote single in usperadc, just to supply the subrutine byte2single that he was looking for. > > Walter Roberson <roberson(a)hushmail.com> wrote in message <y_i2o.22765$lS1.4988(a)newsfe12.iad>... > > Jose wrote: > > > Fred, but I can´t understand why my bosss in his code use 'single' > > > instead of 'uint32'..in this line > > > sdata.usperadc=typecast(data(9:12),'single');...and I want to understand > > > it. > > > > It's a byte array derived from some binary interface (or a simulation of > > a binary interface). data(9:12) extracts four bytes from the data and > > re-interprets that as a single precision floating point number, because > > the binary interface happens to send a single precision floating point > > number at that point in the array.
From: Jose on 23 Jul 2010 12:52 Ok, guys and walter thank you for your time and your information...i'll try to sort out it...cheers. "Jose " <jose.l.vega(a)gmail.com> wrote in message <i2cghc$jir$1(a)fred.mathworks.com>... > sorry, I forgot: > > function [longint]=byte2long (d); > data=double(d); > > longint=data(1)+data(2)*(2^8)+data(3)*(2^16)+data(4)*(2^24); > > function [strout]=byte2string (data); > data > i=find(data==0); > > if length(i)==0 > strout=[]; > else > 'que fuerte' > strout=char(data(1:i)') > end > > > "Jose " <jose.l.vega(a)gmail.com> wrote in message <i2cfv7$d6u$1(a)fred.mathworks.com>... > > Walter, this is the data send from the master to the slave: > > > > case sm_adc > > > > %sending 4 bytes; > > -------- > > %sending 40 bytes: > > > > data=data(1:40) > > ptsperchan=byte2long(data(1:4)); > > %data(5:8)..my boss says...jose..is usperchan--number of microseonds per sample, held as a single precision floating point number --need to write a routine byte2single ---check the web for this format...Can you helpme with it too? > > > > stimname=byte2string(data(9:29))) > > stimtype=byte2long(data(33:36)); > > datasize=byte2long(data(37:40)); > > > > And this is the information receive in my slave (the original code of my first message): > > > > case sm_adc > > > > % receiving 44 bytes > > sdata.ptsperchan=typecast(data(5:8),'uint32'); > > sdata.usperadc=typecast(data(9:12),'single'); > > sdata.stimname=byte2string(data(13:33)); > > sdata.stimtype=typecast(data(37:40),'uint32'); > > sdata.datasize=typecast(data(41:44),'uint32'); > > adc=msrecvraw (sock,sdata.datasize); > > > > it has sense now?...please can you explain to me it better....I think He wrote single in usperadc, just to supply the subrutine byte2single that he was looking for. > > > > Walter Roberson <roberson(a)hushmail.com> wrote in message <y_i2o.22765$lS1.4988(a)newsfe12.iad>... > > > Jose wrote: > > > > Fred, but I can´t understand why my bosss in his code use 'single' > > > > instead of 'uint32'..in this line > > > > sdata.usperadc=typecast(data(9:12),'single');...and I want to understand > > > > it. > > > > > > It's a byte array derived from some binary interface (or a simulation of > > > a binary interface). data(9:12) extracts four bytes from the data and > > > re-interprets that as a single precision floating point number, because > > > the binary interface happens to send a single precision floating point > > > number at that point in the array.
From: Walter Roberson on 23 Jul 2010 14:47 Jose wrote: > sorry, I forgot: > > function [longint]=byte2long (d); > data=double(d); > > longint=data(1)+data(2)*(2^8)+data(3)*(2^16)+data(4)*(2^24); That code is not able to create signed values as is suggested by its name (since the name does not indicate "unsigned"). The code also produces a double rather than an integer data type such as is suggested by its name. The code appears to be assuming the data is in "little-endian" byte order, such as is used on Intel processors, but does not assume that the processor it is operating on is little endian. That is unusual, in that binary data transfer interfaces are most often written to use big-endian order (google "network byte order"), but there are some instances in which it would be reasonable. > function [strout]=byte2string (data); > data > i=find(data==0); > > if length(i)==0 > strout=[]; > else > 'que fuerte' > strout=char(data(1:i)') > end That code is peculiar. I gather that it must be assuming that the string should be NUL terminated and is producing the empty array if it is not. If, though, the data happens to contain multiple NUL (decimal 0's) values then the routine will bomb.
First
|
Prev
|
Pages: 1 2 3 Prev: not a valid symbolic expression Next: remove horizontal shifts in image |