10 years, 9 months ago.

How to use gets function?

Hello all,

I have question about gets function: Here is my part of code:

int main() { pc.baud(115200); pc.printf("Test Print \n\r "); char instr[128]; char c; while(1) {

if (pc.readable() >0) { pc.gets(instr, 5);

if(( instr[0] == 'L') && ( instr[1] == 'A')) {

long num = atoi(&instr[2]); pc.printf("I got '%i'\n\r", num);

while (pc.readable()) { c = pc.getc(); } } } }

When I wrote pc.gets(instr, 5); and try to send command via Hyper Terminal:

Sent receive LA123 = > 12 my micro controller respond quickly and cut last number. LA12 => 12 it’s add 0 in the end of my string and micro controller respond quickly LA1 => I need to send twice the commands and get 1.

How I can send string with different lengths and read ones all my numbers without missing anything? I need to send to the motor different length for moving.

Maybe somebody has alternative solution? Where I can read about that function ?

Thank you for your help.

Alex

I don't know where the setting is in hyperterminal, but Teraterm by default only transmits \r for an enter. You need to set it to transmit a \n (also), then gets should terminate once it receives the \n.

posted by Erik - 13 Mar 2014

1 Answer

Alex Alex
poster
10 years, 9 months ago.

Thank you Erik You solve it

Alex

Good :)

posted by Erik - 13 Mar 2014