Page 1 of 1

Read Hex numbers to RS232

Posted: Fri Nov 25, 2011 11:06 am
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

Re: Read Hex numbers to RS232

Posted: Fri Nov 25, 2011 11:11 am
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.

Re: Read Hex numbers to RS232

Posted: Fri Nov 25, 2011 12:22 pm
by Marcel
Thanks Ben,

Do I need the incoming RecieveRS232Char change to RecieveRS232String ?

Kind Regards.

Re: Read Hex numbers to RS232

Posted: Fri Nov 25, 2011 5:26 pm
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.

Re: Read Hex numbers to RS232

Posted: Tue Nov 29, 2011 5:02 pm
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.

Re: Read Hex numbers to RS232

Posted: Tue Nov 29, 2011 5:46 pm
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

Re: Read Hex numbers to RS232

Posted: Fri Dec 02, 2011 11:15 am
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

Re: Read Hex numbers to RS232

Posted: Fri Dec 02, 2011 3:03 pm
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.

Re: Read Hex numbers to RS232

Posted: Mon Dec 05, 2011 12:47 pm
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

Re: Read Hex numbers to RS232

Posted: Mon Dec 05, 2011 3:45 pm
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)

Re: Read Hex numbers to RS232

Posted: Tue Dec 06, 2011 1:05 pm
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

Re: Read Hex numbers to RS232

Posted: Tue Dec 06, 2011 8:02 pm
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.

Re: Read Hex numbers to RS232

Posted: Wed Dec 07, 2011 3:27 pm
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?

Re: Read Hex numbers to RS232

Posted: Wed Dec 07, 2011 5:31 pm
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

Re: Read Hex numbers to RS232

Posted: Thu Dec 08, 2011 9:11 am
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.

Re: Read Hex numbers to RS232

Posted: Thu Dec 08, 2011 12:03 pm
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.

Re: Read Hex numbers to RS232

Posted: Thu Dec 08, 2011 12:05 pm
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.

Re: Read Hex numbers to RS232

Posted: Thu Dec 08, 2011 12:31 pm
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.

Re: Read Hex numbers to RS232

Posted: Thu Dec 08, 2011 2:51 pm
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.

Re: Read Hex numbers to RS232

Posted: Mon Dec 12, 2011 10:08 am
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.

Re: Read Hex numbers to RS232

Posted: Thu Feb 07, 2013 12:01 am
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.