11 years, 2 months ago.

Sending more than 8 bits using printf

Hello,

i'm using a mbed ship in order to get a analogic signal from a weight sensor and then send it to a computer. I can get the value with the function read (between 0 and 1), and then multiply it per 3.3 and convert it into 4 digital numbers (ex: 2.214 V).

I put my 4 values (2 2 1 4) on a table and I send this table with a pc.printf("%s",tab); Tab is 8 char sized, and sent through the USB port.

On the computer side, I get this data thanks to a library found on the interet, and it works pretty well (I print the datas "on live", as a graphic, point to point).

My problem is that I don't send the values from the mbed to the PC. I send about 150 points/sec and I would need about 1000, at least.

I read that I can't send more than 8 bits of data per printf, so i can't more than 2 points (4*2).

That's why I'm looking for a way to: -Make the number of printf/sec higher -Make possible to send about 15 points, so a 60 char sized table through a printf using %s

Thanks a lot for your answers, and please excuse me if there is a few English mistakes again.

Antoine

2 Answers

11 years, 2 months ago.

You can send more than 8-byte with printf, you cant send more than 8-bit with putc, since that sends a single byte, which is 8 bit.

If you want to send it in that format I would just use %f: pc.printf("%1.3f\n", yourvalue);

However it depends on your exact code, but I expect your current problem to be due to simply the speed of your serial connection. Set a higher baud rate using pc.baud, and put the same value in the serial program you use on your computer. Then you can send your data alot faster.

11 years, 2 months ago.

Thanks for your answer. However, I can't change the baudrate, it create mistakes and I don't know why. Of course pc.baud(19200) works for exemple, but even if i put the same value on the computer side, i can't receive the datas properly.

I stille can't send more than about 3 points per printf, because the computer, or the connection "miss" more than half of the points, wich is annoying... That's wired.

Here's my code, if it can help:

include the mbed library with this snippet

while(1)
{
                for(i=0;i<16;i+=4)      //For 4 points with 4 numbers/points
                {
                    V=capteur.read();    //read my sensor
                    wait(0.001);
                    
                    W=3300*V;                        //get the first number
                    tab[i]=(W/1000)+48;          //second one, etc...
                    tab[i+1]=((W/100)%10)+48;
                    tab[i+2]=(((W/10)%100)%10)+48;
                    tab[i+3]=((((W/1)%1000)%100)%10)+48;   
                }
                tab[16]=0;       //put a 0 at the end

                pc.printf("%s",tab);
                wait(0.005);

   }


                

Using %f is still the easiest solution, but you seem to have a different problem. It really should work with higher baudrates, I have used baudrates up to 1mbps without any issues. I guess your printf problem is related to that. Have you tried using a different terminal program? Or maybe got another mbed you can test it on?

Anyway you need a higher baudrate for sure, it simply isnt possible with 9600 baudrate to meet your requirements. Putting more in a single printf won't result in a higher speed, that stays pretty much the same.

posted by Erik - 01 Mar 2013