Read Hex numbers to RS232

For E-blocks user to discuss using E-blocks and programming for them.

Moderators: Benj, Mods

Post Reply
Marcel
Flowcode V4 User
Posts: 23
Joined: Wed Aug 24, 2011 1:22 pm
Has thanked: 2 times

Read Hex numbers to RS232

Post by Marcel »

Hello,

I have a uC with 2 uarts. I have a sensor and this sensor send HEX numbers to the RS232 (Tx,Rx) input.
How can I see these HEX numbers on my PC with hyperterminal ? I see only the ASCII codes on my PC.

Thanks, Marcel

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Read Hex numbers to RS232

Post by Benj »

Hello Marcel,

Decimal, hex, ascii and binary are all different ways of expressing the same value.

for example

48 is equal to 0x30, '0' and 0b00110000

Hyperterminal can only show ascii characters so this is why you cannot see the hexadecimal values.

If you need to be able to see the hexadecimal then use the string manipulation function NumberToHex to generate an ascii string from your number. You can then send this string to hyperterminal and it should print out the ascii representation of the hex number.

Marcel
Flowcode V4 User
Posts: 23
Joined: Wed Aug 24, 2011 1:22 pm
Has thanked: 2 times

Re: Read Hex numbers to RS232

Post by Marcel »

Thanks Ben,

Do I need the incoming RecieveRS232Char change to RecieveRS232String ?

Kind Regards.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Read Hex numbers to RS232

Post by Benj »

Ah sorry I think I got the wrong end of the stick.

No you simply receive the bytes as normal using the RecieveRS232Char function and then you can compare with hex values if you wish.

eg

component macro: in = RecieveRS232Char()

decision: in < 255

yes: decision in = 0x00

where 0x00 is the hex value you want to compare.

the in < 255 simply ensures that a valid byte has been received. 255 means a timeout occurred using the legacy return type in the RS232 comp properties.

Marcel
Flowcode V4 User
Posts: 23
Joined: Wed Aug 24, 2011 1:22 pm
Has thanked: 2 times

Re: Read Hex numbers to RS232

Post by Marcel »

Hello Ben,

I now have a program that shows de Hex numbers of my sensor. I would like to read this data (in Hex) and stores into an array. Is it possible to stores the Hex numbers into an array ? And how could I received(send) Hex number FF ?

Kind Regards.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Read Hex numbers to RS232

Post by Benj »

Hello,

The values are just bytes right, and not ASCII hexadecimal strings?

If so then yes you can put them in an array.

When creating a variable in Flowcode you simply add [x] after the variable name where x is the size of the array.

e.g.

Variable ben1[5]

has 5 bytes which are indexed 0-4

ben1[0] = RS232_Receive_Byte

Marcel
Flowcode V4 User
Posts: 23
Joined: Wed Aug 24, 2011 1:22 pm
Has thanked: 2 times

Re: Read Hex numbers to RS232

Post by Marcel »

Hello Ben,

With this program I have attached I can receive and sent the hex numbers that are comes from my sensor.
But I receive a lots of data that I do not all needed.
The problem is I only need the first 15 bytes when byte[2] has the hex number 81 or 83 or 87.
I am still struggling with this and have tried everything.

Kind Regards.
RS232-Loop.fcf
(8 KiB) Downloaded 578 times

Spanish_dude
Posts: 594
Joined: Thu Sep 17, 2009 7:52 am
Location: Belgium
Has thanked: 63 times
Been thanked: 102 times

Re: Read Hex numbers to RS232

Post by Spanish_dude »

Hi Marcel,

I haven't seen your program but I think I'll be able to help.
Try a programs that works as follows :

Code: Select all

main loop:
    check byte:
        read sensor bytes
        if sensor byte = 83 or sensor byte = 85 or sensor byte = 87
            counter = 0
            send loop while counter < 15:
                read sensor byte
                send sensor byte through RS232
                counter = counter + 1
            end send loop
    end check byte
end main loop
If this is what you have done and you're having some problems with it, I'll check your program later and see if I can find something wrong with it.

Marcel
Flowcode V4 User
Posts: 23
Joined: Wed Aug 24, 2011 1:22 pm
Has thanked: 2 times

Re: Read Hex numbers to RS232

Post by Marcel »

Thank you very much for responce.
I have tried it but I don't know how to implement it in my program. Maybe you could help me.

Thank you, kind regards

Spanish_dude
Posts: 594
Joined: Thu Sep 17, 2009 7:52 am
Location: Belgium
Has thanked: 63 times
Been thanked: 102 times

Re: Read Hex numbers to RS232

Post by Spanish_dude »

Hello,

I just looked at your code.
Everything looks to be ok except the part where it needs to wait for the value 83, 85 or 87.
This can easily be added by putting a "while loop" after your first calculation box.

Add this as condition of the while loop : "(incom != 83) && (incom != 85) && (incom != 87)".
And add a ReceiveRS232Char function in that while loop, just like in the "read into array" loop.

This should make your PIC wait until it receives a byte (incom) with a value of 83, 85 or 87.

Nicolas

PS:
"(incom != 83) && (incom != 85) && (incom != 87)".
while (incom NOT EQUAL 83) AND (incom NOT EQUAL 85) AND (incom NOT EQUAL 87)
If incom is equal to 40.
- incom NOT EQUAL 83 => True (1)
- incom NOT EQUAL 85 => True (1)
- incom NOT EQUAL 87 => True (1)
=> Stay in the loop (1 AND 1 AND 1 = 1)

If incom is equal to 85.
- incom NOT EQUAL 83 => True (1)
- incom NOT EQUAL 85 => False (0)
- incom NOT EQUAL 87 => True (1)
=> Jump out of the loop (1 AND 0 AND 1 = 0)

Marcel
Flowcode V4 User
Posts: 23
Joined: Wed Aug 24, 2011 1:22 pm
Has thanked: 2 times

Re: Read Hex numbers to RS232

Post by Marcel »

Hi Nicolas,

Thanks for looking at my code. I have add the while loop, but it would not work.
When I simulate it, its going wrong at the decision box. Also when I program it I see nothing.
I have attached the new program. Could you please look whats going wrong, Thanks.
RS232-Loop.fcf
(9 KiB) Downloaded 554 times

Spanish_dude
Posts: 594
Joined: Thu Sep 17, 2009 7:52 am
Location: Belgium
Has thanked: 63 times
Been thanked: 102 times

Re: Read Hex numbers to RS232

Post by Spanish_dude »

That's strange...
I just tried your program and it simulates fine.

This what I wrote in RS232(1):
81,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,83,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,87,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20

The bytes sent by RS232(0) are :
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14

If the read value is either 81, 83 or 87 then save & send the next 15 received bytes.

PS: I thought it was 85 instead of 81, hence the 85 in my previous posts... My bad.

Marcel
Flowcode V4 User
Posts: 23
Joined: Wed Aug 24, 2011 1:22 pm
Has thanked: 2 times

Re: Read Hex numbers to RS232

Post by Marcel »

Hi Nicolas,

The program is working now, Also when I program it. Thank you very much for your help.

I have another question, I would also like to receive hex number FF (255)
In the decision box I fill in: income <= 255. It works but is it allowed?

Spanish_dude
Posts: 594
Joined: Thu Sep 17, 2009 7:52 am
Location: Belgium
Has thanked: 63 times
Been thanked: 102 times

Re: Read Hex numbers to RS232

Post by Spanish_dude »

The receiveRS232 function will return 255 when nothing has been received.

But you could try to detect when it's a useless byte or if it's a received byte by checking if the previous and/or next received byte has a value of 255.
I don't think you will receive two or more bytes with value 255 from your sensor.

Nicolas

User avatar
Steve
Matrix Staff
Posts: 3431
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times

Re: Read Hex numbers to RS232

Post by Steve »

In the RS232 component, you can set the return value to be a "short" (rather than a "byte"). This will allow you to receive valid input between 0 and 255. A return of -1 (I think) will indicate nothing has been received.

Marcel
Flowcode V4 User
Posts: 23
Joined: Wed Aug 24, 2011 1:22 pm
Has thanked: 2 times

Re: Read Hex numbers to RS232

Post by Marcel »

Hello Steve,

Do you mean char receive type "Int" and the data bits remains 8 ?
In the decision box should I fill : income <= 255 ?

Thanks.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Read Hex numbers to RS232

Post by Benj »

Do you mean char receive type "Int" and the data bits remains 8 ?
Yep and yep.
In the decision box should I fill : income <= 255 ?
Also yep, just make sure that 'income' is a INT type variable.

Marcel
Flowcode V4 User
Posts: 23
Joined: Wed Aug 24, 2011 1:22 pm
Has thanked: 2 times

Re: Read Hex numbers to RS232

Post by Marcel »

Hi Ben,

In my program I make a calculation: income = Idx. Do I also change Idx in a INT type variable ? It's now a Byte type variable.

Thanks.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Read Hex numbers to RS232

Post by Benj »

Hello,

The variable you use as the RS232 Receive Return variable must be of type INT to use the new return type.

Once you have received the data and checked it is valid you can then copy the variable into a byte variable which will simply hold the 8-bit data.

You cannot do the data validity check on the byte variable using the new return type as the extra bits of information will be lost.

eg

INT = RS232 Receive
if (INT <= 255)
yes: BYTE = INT

The BYTE variable now contains your valid incoming serial data.

Marcel
Flowcode V4 User
Posts: 23
Joined: Wed Aug 24, 2011 1:22 pm
Has thanked: 2 times

Re: Read Hex numbers to RS232

Post by Marcel »

Hello,

Thanks Ben now it's clear. But.....If I have a variable of type Byte, why do I see in the RS232 Macro: Return value: (INT).

Thank you, kind regards.

Brian Walsh
Posts: 29
Joined: Thu Jan 14, 2010 10:43 pm
Has thanked: 3 times

Re: Read Hex numbers to RS232

Post by Brian Walsh »

Hi,

Does anyone have a neat way of sending a string showing the binary representation of a byte? This would be handy for looking at register contents for comparison with PIC datasheets.

I don't see a NumberToBin$ function in flowcode 5.

TIA,

Brian Walsh.

Post Reply