Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   Send Byte (http://www.chiefdelphi.com/forums/showthread.php?t=32981)

the_undefined 17-01-2005 23:29

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

Dave Flowerday 17-01-2005 23:47

Re: Send Byte
 
Quote:

Originally Posted by the_undefined
So when I send 32 for example I would like to have just one byte send not 2 ...

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.

the_undefined 18-01-2005 00:07

Re: Send Byte
 
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?)

Dave Flowerday 18-01-2005 00:18

Re: Send Byte
 
Quote:

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

Try this:
Code:

int x = 0x1234;

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

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

the_undefined 18-01-2005 00:27

Re: Send Byte
 
Thanks, but hmm but how do I convert an integer in that format?

the_undefined 18-01-2005 19:12

Re: Send Byte
 
Quote:

Originally Posted by Dave Flowerday
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.

The %c thing didn't work but then I found that in the printf class:
* 2. 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

CJO 03-11-2005 12:57

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

Dave Scheck 03-11-2005 14:30

Re: Send Byte
 
Quote:

Originally Posted by CJO
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?
Code:

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

Output: 10 => a => A


CJO 03-11-2005 17:59

Re: Send Byte
 
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".

Dave Flowerday 03-11-2005 18:26

Re: Send Byte
 
Quote:

Originally Posted by CJO
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).

TimCraig 03-11-2005 20:22

Re: Send Byte
 
Quote:

Originally Posted by the_undefined
How can I use the prinft() function to send a single byte (or a couple of them) over the serialport

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


All times are GMT -5. The time now is 02:50.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi