USB Serial Receive

For questions and comments on programming in general. And for any items that don't fit into the forums below.

Moderators: Benj, Mods

Post Reply
Rich
Posts: 4
Joined: Tue Jun 12, 2012 10:27 pm

USB Serial Receive

Post by Rich »

I am new to Flowcode, so I appoligize this may be a dumb question.

I am attempting to create a program that uses Hyperterminal to program timing functions in a PIC18f2455. For the most part the functions are working well. I have menu options that are sent from the pic to the terminal and routine allows me to select single menu option using the USBGETSTRING function. It all works fine until I get to the enter time delay routine. Getstring only allows for one character to be entered even when I set the string length to 10. If I use the GETByte function I get the byte then can "see" when enter is pressed, however since the byte are all ascii I cannot assemble them as a string, to create a multidigit number. EX 500 which gives me 53484813. How can I either convert this back to 500, or get the GET STRING function to allow me to enter more then one character within the timeout period.

My latest attempt was to assemble the bytes into a string then figured I can convert the string to an integer.

instportconv = ToString$ (instportstrex[charcntassemble])
instportstr = instportstr + instportconv

Any suggestions.

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: USB Serial Receive

Post by Benj »

Hello,

Can you do something like this.

First copy the numeric bytes from the incoming string to a new temp string.

idx = start position of number in incoming string
idx2 = 0

while (string[idx] >= '0' && string[idx] <= '9')
{
temp[idx2] = string[idx]
idx = idx + 1
idx2 = idx2 + 1
}

Then convert the Ascii string to a number using the StringToInt function.

variable = StringToInt$(temp)

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

Re: USB Serial Receive

Post by Spanish_dude »

Hi,

ASCII characters are just values. Read your value from USB, either in byte array or char/string format, it doens't really matter.
Once you have your string do as follows :

Code: Select all

int i = lenght_of_received_data;
int value;

while (received_data[i] >= '0' && received_data[i] <= '9')
{
    value = value * 10 + (received_data[i] - '0');
    i--;
}
If received_data is a string containing "518" it will do the following calculation:
1) value = 0 * 10 + (53 - 48) -> value = 5
2) value = 5 * 10 + (49 - 48) -> value = 51
3) value = 51 * 10 + (56 - 48) -> value = 518

Cheers,
Nicolas

PS: Don't forget that if you receive "518\n" you need to substract 1 from the length_of_received_data or it won't execute the loop.

Post Reply