Discussion:
I only recieve Ascii on my RS232. how do I get hex?
(too old to reply)
Jörn Ahrens
2007-05-10 09:10:46 UTC
Permalink
I send hex values from a microcontroller to matlab.
but matlab is always making ASCII from the Hex bytes!
how do i get the Hex?

i tryied %c,%s....

data = fscanf(Port1,'%s' , [1,bytes])
flushinput(Port1);
disp([int2str(bytes) ' bytes received from COM1: ' data]);
dave
2007-05-10 10:00:04 UTC
Permalink
Post by Jörn Ahrens
I send hex values from a microcontroller to matlab.
but matlab is always making ASCII from the Hex bytes!
how do i get the Hex?
i tryied %c,%s....
data = fscanf(Port1,'%s' , [1,bytes])
flushinput(Port1);
disp([int2str(bytes) ' bytes received from COM1: ' data]);
well, actually you only send and receive streams of bits. the bits
get presented to the transmitter in groups of 8 called byted, get
streamed one at a time over the wire, then grouped into bytes again
by the receiver. These bytes get read by matlab... it is then up to
you how you interpret them. i would guess from the above that you
have mixed up your data types... fscanf with either %s or %c is going
to be looking for an ascii character and will give you the single
letter or number character that cooresponds to the bit pattern you
read. to get a proper answer here you will have to post the code
that formats the data for sending, AND the code that does the read
from the port and interprets it in matlab.
Jörn Ahrens
2007-05-10 11:26:10 UTC
Permalink
yes the microcontroller is sending packages of 8 Bit + Start/Stop
bits....

The incoming "hex" for example is, as I can see it in Terminal oder
Hterm ....

i want to tranfer the number: 1400 (DEZ)

so the MC sends: 0578 (HEX)

05 78 05 78 05 78 .....

matlab ist tranfering the value of "8 Bits" (05) into the value=5 and
taking the Ascii letter of this value from 5.

how do i read direct the Hex letter of 8 Bits?then i just have to
tranfer HEX (of 16 Bit) into a number...

actually i convert it this way but this is not the best one :-)

bytes = Port1.BytesAvailable;
while bytes ~= 2
bytes = Port1.BytesAvailable;
end

data = fscanf(Port1,'%s' , [1,bytes]);
trans = double(data);
hexwert = dec2hex(trans);
zahlh = hex2dec(hexwert(1,1:2));
zahll = hex2dec(hexwert(2,1:2));
nums(i,1) = (zahlh*256+zahll);
flushinput(Port1);
Post by dave
Post by Jörn Ahrens
I send hex values from a microcontroller to matlab.
but matlab is always making ASCII from the Hex bytes!
how do i get the Hex?
i tryied %c,%s....
data = fscanf(Port1,'%s' , [1,bytes])
flushinput(Port1);
disp([int2str(bytes) ' bytes received from COM1: ' data]);
well, actually you only send and receive streams of bits. the bits
get presented to the transmitter in groups of 8 called byted, get
streamed one at a time over the wire, then grouped into bytes again
by the receiver. These bytes get read by matlab... it is then up to
you how you interpret them. i would guess from the above that you
have mixed up your data types... fscanf with either %s or %c is going
to be looking for an ascii character and will give you the single
letter or number character that cooresponds to the bit pattern you
read. to get a proper answer here you will have to post the code
that formats the data for sending, AND the code that does the read
from the port and interprets it in matlab.
Jörn Ahrens
2007-05-10 11:50:10 UTC
Permalink
but i found out that for example 1421 & 1422 & 1423 are transformed
wrong! why!

1421 -> he recieves 1A instead of 5D!

can noone tell me a direct way?
Post by Jörn Ahrens
yes the microcontroller is sending packages of 8 Bit + Start/Stop
bits....
The incoming "hex" for example is, as I can see it in Terminal oder
Hterm ....
i want to tranfer the number: 1400 (DEZ)
so the MC sends: 0578 (HEX)
05 78 05 78 05 78 .....
matlab ist tranfering the value of "8 Bits" (05) into the value=5 and
taking the Ascii letter of this value from 5.
how do i read direct the Hex letter of 8 Bits?then i just have to
tranfer HEX (of 16 Bit) into a number...
actually i convert it this way but this is not the best one :-)
bytes = Port1.BytesAvailable;
while bytes ~= 2
bytes = Port1.BytesAvailable;
end
data = fscanf(Port1,'%s' , [1,bytes]);
trans = double(data);
hexwert = dec2hex(trans);
zahlh = hex2dec(hexwert(1,1:2));
zahll = hex2dec(hexwert(2,1:2));
nums(i,1) = (zahlh*256+zahll);
flushinput(Port1);
Post by dave
Post by Jörn Ahrens
I send hex values from a microcontroller to matlab.
but matlab is always making ASCII from the Hex bytes!
how do i get the Hex?
i tryied %c,%s....
data = fscanf(Port1,'%s' , [1,bytes])
flushinput(Port1);
disp([int2str(bytes) ' bytes received from COM1: ' data]);
well, actually you only send and receive streams of bits. the
bits
Post by dave
get presented to the transmitter in groups of 8 called byted,
get
Post by Jörn Ahrens
Post by dave
streamed one at a time over the wire, then grouped into bytes
again
Post by dave
by the receiver. These bytes get read by matlab... it is then
up
Post by Jörn Ahrens
Post by dave
to
you how you interpret them. i would guess from the above that
you
Post by dave
have mixed up your data types... fscanf with either %s or %c is going
to be looking for an ascii character and will give you the
single
Post by Jörn Ahrens
Post by dave
letter or number character that cooresponds to the bit pattern
you
Post by dave
read. to get a proper answer here you will have to post the
code
Post by Jörn Ahrens
Post by dave
that formats the data for sending, AND the code that does the
read
Post by dave
from the port and interprets it in matlab.
Walter Roberson
2007-05-10 14:27:15 UTC
Permalink
Post by Jörn Ahrens
yes the microcontroller is sending packages of 8 Bit + Start/Stop
bits....
so the MC sends: 0578 (HEX)
05 78 05 78 05 78 .....
data = fscanf(Port1,'%s' , [1,bytes]);
You need to use %c format rather than %s . That will give you
binary bytes. If you need to access the hex representation for
some reason (and to me it doesn't sound like you do, only that
you think you do ;-) ) then use bin2hex().
--
Prototypes are supertypes of their clones. -- maplesoft
Jörn Ahrens
2007-05-10 13:20:16 UTC
Permalink
is this so difficult?

matlab is always recieving:

05 78 05 78 05 78 ....

0h0578 = 1400 = one "Block" with 2 bytes

so i use fscanf(Port, ????, [1,2])

%x does not work. the always comes the message :

Warning: Matching failure in format.

what am I doing wrong?
Walter
2007-06-28 19:27:55 UTC
Permalink
Post by Jörn Ahrens
is this so difficult?
05 78 05 78 05 78 ....
0h0578 = 1400 = one "Block" with 2 bytes
so i use fscanf(Port, ????, [1,2])
Warning: Matching failure in format.
what am I doing wrong?
Hi
you can use fread(Port,...)
fscanf gives you always text output (see matlab reference)

Loading...