Send Byte

Hi everyone,

I guess this should be a easy question but I couldn’t find the answer anywhere. How can I use the prinft() function to send a single byte (or a couple of them) over the serialport but not as a string like printf(“MyByte: %d”, (int)myByte); would do. So when I send 32 for example I would like to have just one byte send not 2 … Sry if I explained that bad but I hope its understandable …

Thanks in advantage,

bye Felix

Felix,
If you use %c in your printf format string (instead of %d) it will send it as a single byte. It will just send the 0-255 value of the argument you give it rather than converting it into an ASCII number first. As you probably know, you’ll need something more fancy than the IFI loader terminal program or Hyperterminal to view those bytes that come out if you do that, though.

Yeah thx, thats exactly what I was looking for. What I’m working on right now is a little Program in VB.Net that will allow you to watch whatever numbers you got in a little bit more sophisticated / graphical way. I found a good class for the serial connection and got the base coding done. I hope I’ll finish the thing in the next 2-3 days, after that I’ll upload it here in case somebody is interested …

Thanks for your Help

Felix

(PS: Is there a way to send an Integer as 2 Bytes instead of an ASCII as well?)

Try this:


int x = 0x1234;

printf("%c%c", (x >> 8), (x & 0xff));

That should send 2 bytes: 0x12 then 0x34.

Thanks, but hmm but how do I convert an integer in that format?

The %c thing didn’t work but then I found that in the printf class:

    1. All bytes (8 bits) or bit references (rc_dig_in01) must be type-cast to a
  •  16 bit word.  The %c directive is unsupported.
    

Just in case somebody else needs to deal with that at some point …

Bye

Felix

If one wanted to output actual data in (say) hex, how would one actually go about it then?

I don’t remember off hand if this is available in the standard IFI printf, but is the %x (or %X) option what you’re looking for?

printf("%d => %x => %X", 10,10,10);

Output: 10 => a => A

No, I think that what that is doing is converting the hex values into their ASCII equivalents. That is to say, instead of sending 0xA, it sends the ASCII “A”.

Yes, that’s what Dave’s suggestion is doing. If you want the value to be written directly, then that is the same question that the original poster had, and my first post in this thread tells you how to do it (using %c in the printf format string) or you can bypass the printf library altogether and use whatever lower-level write() function is used to send data directly to the serial port driver.

In this case, the data being sent isn’t really in ‘hex’ format - it’s just raw data. The program on the other end may choose to display it in hex format, but that doesn’t really have anything to do with the sending side. Asking to output the data in ‘hex’ format will be taken to mean that you wish it to be converted to ASCII for viewing (hence the response Dave gave you).

Check out the function, Write_Byte_To_Uart, in printf_lib.c. It sends a “raw” byte of data out the serial port.