7 years, 7 months ago.

programming problem

Hello pros. I am writing a program on mbed to show hexdecimal commands on pc and I use realterm to send commands. The commands can be 7 bytes or 8 bytes. I set up an array[9] cause the last one is 0 for termination. The problem is when I send 7 bytes commands (\xFF\x01\x00\x00\x00\x00\x01) in mbed for first time, there is nothing on screen. When I send the same command twice, it shows FF 01 00 00 00 00 01 FF 00. I know last one is string termination. It appears the first 'FF'of second command come to the array. Then I send the same command again, it shows 01 00 00 00 00 01 FF 01 00 where first six bytes are from second command and 'FF' and '01 'is from third command. How can I have my array store numbers and stopped when there is no numbers. Like if there are only 7 bytes(FF 01 00 00 00 00 01), it shows FF 01 00 00 00 00 01 00 00. If there are 8 bytes(A0 01 00 00 00 00 AF 01), it shows A0 01 00 00 00 00 AF 01.



Hi Neil, you can copy and paste code into questions and surround them with <<code>> tags to format them.

Is there any way to tell the difference between the end of one command and the start of another?

posted by Stephen Paulger 04 Dec 2012

1 Answer

7 years, 7 months ago.

What is pc.gets() I don't see that function in the library, but if it is a function that returns after getting 9 chars, it looks like when you first entered the data you only put 7 chars, then when you enter more data the second time, it took 1 char and then added a NULL for to make it 9, that is why you got the output.

I think you should just use getc() to get byte by byte in a for loop. If you want to get 7 bytes (+ the NULL) then 8 then 9, then change your gets(commmand_input,8) then gets(commmand_input,9), gets(commmand_input,10).

Try this code in the handbook for serial i/o http://mbed.org/handbook/SerialPC

Thank you for your answer. But how should I use getc() in a for loop? And what is the difference in getc() and gets()?

posted by Neil Powell 05 Dec 2012