Doing simple math with SerRxd inputs

peterzomer

New Member
Hi !

Does someone know if it is possible to do simple math with the SerRxd input variables with the 18M2 ?? :eek:
The adding of b1 + b2 in this example is not b3 of course because these variables are ASCII codes and not decimal numbers.

Code:
Main:
SerTxd ("Enter 2 decimal numbers: ", 13, 10)
SerRxd b1, b2
Let b3 = b1 + b2
SerTxd (#b3, 13, 10)
goto main
There are some functions to convert BCD to ASCII, but not from ASCII to DEC ??

Hope to hear you soon.

PeterZ
 

nick12ab

Senior Member
As there's only one digit per byte in ASCII, you can simply subtract 48 from the ASCII byte in order to get the decimal.

So for example to convert from ascii to dec for two digits, you could do:
Code:
let b3 = b2 - 48
let b3 = b1 - 48 * 10 + b3
 

westaust55

Moderator
Another option is to place a hash symbol in front of the variable number:

SerRxd #b0, #b1

But you also need to look at the format where you send the values from a PC or whatever.
Are you entering the value in quotes ( "3") or direct 3, 13 ?
 

peterzomer

New Member
Hi guys,

Thanks for the fast responce !! :cool:

No, the inputs are direct numbers between 0 and 99, so without the quotes.
In fact, I want to use the input for a counter or for other calculation.
So, the first digit must be mulitplied by 10 and than be added to the second.

Example:
the input is 34
b1 = 3
b2 = 4
b3 = b1 * 10
b4 = 4
b5 = b3 + b4

Its funny that there is no ready-to-use function for this. :(
 

westaust55

Moderator
try:
1. Value entry as "99"

2. SerRxd #b1

The # flags to the command to receive a string of digits until a no digit (ie space, letter, etc) is received, so "123" will put the decimal value 123 into the receiving variable.


From PICAXE Manual (v7.7) page 207 SERRXD:
- Variable(s) receive the result(s) (0-255). Optional #’s are for inputting ASCII decimal numbers into variables, rather than raw characters.
 
Last edited:

peterzomer

New Member
This is great news !!

The only thing is that you have to send a no-digit char at the end.
So, using the Terminal in the editor is only possible if the option 'add <CR> on send' is checked.
But okay, this can also be set in Hyperterminal or so.

Anyway, thanks for your help.
 
Top