Gamestudio Links
Zorro Links
Newest Posts
Zorro Beta 2.61: PyTorch
by jcl. 06/10/24 14:42
New FXCM FIX Plugin
by flink. 06/04/24 07:30
AlpacaZorroPlugin v1.3.0 Released
by kzhao. 05/22/24 13:41
Free Live Data for Zorro with Paper Trading?
by AbrahamR. 05/18/24 13:28
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
0 registered members (), 1,118 guests, and 8 spiders.
Key: Admin, Global Mod, Mod
Newest Members
AemStones, LucasJoshua, Baklazhan, Hanky27, firatv
19058 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
float to byte? serial input of float #336144
08/01/10 21:21
08/01/10 21:21
Joined: Aug 2002
Posts: 164
Houston
Nicholas Offline OP
Member
Nicholas  Offline OP
Member

Joined: Aug 2002
Posts: 164
Houston
I am trying to get a float value input through serial, but it's not working. I try a float value in Arduino and setup float value in 3dgs, but doesn't work. it only seems to work when both of them are setup as bytes.

Any idea how I can get this to work? I thought of converting the float to it's byte values and recombining them in lite-c after reading them in, but I don't know how to do this.
It would be a lot easier to just read the float directly.


Black holes are where God divided by zero.
Re: float to byte? serial input of float [Re: Nicholas] #336148
08/01/10 21:36
08/01/10 21:36
Joined: May 2007
Posts: 2,043
Germany
Lukas Offline

Programmer
Lukas  Offline

Programmer

Joined: May 2007
Posts: 2,043
Germany
If I undestood your problem right, you can do this:

float x;
...
char byte1 = ((char*)(&x))[0];
char byte2 = ((char*)(&x))[1];
char byte3 = ((char*)(&x))[2];
char byte4 = ((char*)(&x))[3];

And you can write into the bytes of your floats accessing them the same way.

Re: float to byte? serial input of float [Re: Lukas] #336153
08/01/10 22:49
08/01/10 22:49
Joined: Aug 2002
Posts: 164
Houston
Nicholas Offline OP
Member
Nicholas  Offline OP
Member

Joined: Aug 2002
Posts: 164
Houston
I can't seem to get that to work. it gives me 0 for each of those bytes, but I am trying to call them in a digits panel.
Is there a way to verify that? and I'm not sure how to recombine them back. Would you be able to show me the code please.

I've also tried memcpy but not sure if it's working either
byte bytez[4];
float f = 852140;
var fa = f;
memcpy( bytez, &f, sizeof(f) );


Black holes are where God divided by zero.
Re: float to byte? serial input of float [Re: Nicholas] #336156
08/01/10 23:14
08/01/10 23:14
Joined: May 2007
Posts: 2,043
Germany
Lukas Offline

Programmer
Lukas  Offline

Programmer

Joined: May 2007
Posts: 2,043
Germany
I tried your code with memcpy and for me it works. Try this:
Code:
#include <acknex.h>
#include <default.c>

void main ()
{
	wait(1);
	byte bytez[4];
	float f = 852140;
	var fa = f;
	memcpy( bytez, &f, sizeof(f) );
	var x;
	int i;
	for(i=0;i<4;i++)
	{
		x = bytez[i];
		error(str_for_num(0,x));
	}
}


I get 192,10,80,73.

When displaying your bytes in a digits panel, note that digits only accept vars. So you should always use vars to set their value to the value of your byte and then display the vars.

Re: float to byte? serial input of float [Re: Lukas] #336157
08/01/10 23:27
08/01/10 23:27
Joined: Aug 2002
Posts: 164
Houston
Nicholas Offline OP
Member
Nicholas  Offline OP
Member

Joined: Aug 2002
Posts: 164
Houston
I was wondering about that, but wasn't sure.
thanks, that definitly helps break it down, but how do I get them back into a float. I've heard that a union might work for that, but can't find a good example.


Black holes are where God divided by zero.
Re: float to byte? serial input of float [Re: Nicholas] #336158
08/01/10 23:30
08/01/10 23:30
Joined: May 2007
Posts: 2,043
Germany
Lukas Offline

Programmer
Lukas  Offline

Programmer

Joined: May 2007
Posts: 2,043
Germany
Isn't that obvious? If you can transform a float to bytes like this:
memcpy( bytez, &f, sizeof(f) );
Then you can do the opposite thing like this:
memcpy( &f, bytez, sizeof(f) );
wink

Re: float to byte? serial input of float [Re: Lukas] #336159
08/01/10 23:38
08/01/10 23:38
Joined: Aug 2002
Posts: 164
Houston
Nicholas Offline OP
Member
Nicholas  Offline OP
Member

Joined: Aug 2002
Posts: 164
Houston
I didn't think it would be that easy. I hoped it would be but whenever I tried looking it up, nobody was giving memcpy as an example of how to recombine it.

thanks for the help. I'm not used to this type of thing.
grin


Black holes are where God divided by zero.

Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1