Binary numbers on Raspberry Pi
-
wrote on 11 Nov 2015, 19:56 last edited by
Hello together,
I have been trying for some time now to get my Raspberry PI Project to run but I have not yet found a solution to the following problem:
For the use of the WiringPI SPI Library I need to transmit data - over the SPI - in the format of unsigned chars bytewise. To adress the Register i only have hex or binary values. How can I convert binary or hex values to be unsigned chars so I have the correct type for the parameter to call the function ?
-
wrote on 11 Nov 2015, 20:09 last edited by
i guess, u have to develop protocol.. u can choose bigendien or littleendian and than if u have qint16 than cut 2 bytes and make uint16.
-
i guess, u have to develop protocol.. u can choose bigendien or littleendian and than if u have qint16 than cut 2 bytes and make uint16.
-
Hi,
Can you show what you want to use from the WiringPI library ?
-
wrote on 12 Nov 2015, 13:52 last edited by
@SGaist said:
Hi,
Can you show what you want to use from the WiringPI library ?
Hi,
i want to use the method
int wiringPiSPIDataRW (int channel, unsigned char *data, int len)
where I neet to pass an unsigned char pointer. When I try to set one up like this
unsigned char *data = 0b10000000
I get the warning that conversion from int to unsigned char is not possible.
@vish said:
@Dan90 like this
qFromBigEndian<uint16>((const uchar*) byteArray.data())
this will give u uint16 type from ur bytearray buffer.I understand that this funtion gets an unsigned char and converts it to uint16 ? But as I said, I need an unsigned char in the first place.
-
Then you can use a QByteArray to build your data and then use its data function to access them. You can cast data to unsigned char *
-
Then you can use a QByteArray to build your data and then use its data function to access them. You can cast data to unsigned char *
wrote on 13 Nov 2015, 09:43 last edited by Dan90Thanks for the hint with QByteArray. I checked it and the problem I still have ist that the data function of my QByteArray object returns a char*, but the wiringPiSPIDataRW function needs an unsigned char*, so I still get an error out of this.
EDIT:
I just tried an explicit type conversion and it worked. Could this have a negative effect on my data by any means ? -
Do a type cast:
wiringPiSPIDataRW (static_cast<unsigned char *>(myByteArray.data()), myBateArry.size());
-
wrote on 13 Nov 2015, 12:36 last edited by
Thanks a lot for the input. It gave no error messages while compiling, however it did not work. I switched to another Library (BCM2835) and i got it to work in just a few Minutes. This library is much better documented than wiringPI SPI.
Thanks again.
-
@SGaist said:
Hi,
Can you show what you want to use from the WiringPI library ?
Hi,
i want to use the method
int wiringPiSPIDataRW (int channel, unsigned char *data, int len)
where I neet to pass an unsigned char pointer. When I try to set one up like this
unsigned char *data = 0b10000000
I get the warning that conversion from int to unsigned char is not possible.
@vish said:
@Dan90 like this
qFromBigEndian<uint16>((const uchar*) byteArray.data())
this will give u uint16 type from ur bytearray buffer.I understand that this funtion gets an unsigned char and converts it to uint16 ? But as I said, I need an unsigned char in the first place.
@Dan90 You are assigning a value to a pointer, but data is actually an array of "unsigned char":
unsigned char *data = 0b10000000;
Instead you should do (C++11):
unsigned char data[] = { (unsigned char)0b10000000 };
9/11