如何将double或string转换为unsigned char * [英] How to convert double or string to unsigned char*
问题描述
Hello Experts,
我试图将数据字节传递到CAN(SDS)帧。数据形式为双倍(4.00 - 20.00)。数据必须作为unsigned char *发送。使用下面的代码,只有微控制器读取第一个数字。
感谢任何帮助。
谢谢
mraaf
我的尝试:
Hello Experts,
I am trying to pass bytes of data in to CAN (SDS) frame. The data is in form of double (4.00 - 20.00). The data has to be sent as unsigned char*. With below code, only the first digit is read by the microcontroller.
Appreciate any helps.
Thank you
mraaf
What I have tried:
CString strTT126;
double dTT126 = (m_nSliderTT126/8.4)-3.81;
strTT126.Format("%.02f", dTT126);
LPBYTE pByte = (LPBYTE)strTT126.GetBuffer(strTT126.GetLength());
SDSWriteFIFO(Ident, (unsigned char*)pByte); // pass mA value size of 1 byte in SDS frame
推荐答案
我猜您的项目设置为Unicode。
然后CString
类基于wchar_t
。这些Unicode字符为16位宽,ASCII字符(代码0x00至0x7f)清除了高字节。但是您的微控制器可能需要ASCII字符,清除的高字节被视为字符串指示符的结尾。
最简单的解决方案是使用CStringA
类。A
表示它是ANSI字符串:
I guess that your project is set to Unicode.
Then theCString
class iswchar_t
based. These Unicode chars are 16 bit wide and ASCII characters (codes 0x00 to 0x7f) have the high byte cleared. But your micro controller probably expects ASCII characters and the cleared high byte is treated as the end of string indicator.
The simplest solution would be using theCStringA
class. TheA
indicates that it is an ANSI string:
CStringA strTT126;
double dTT126 = (m_nSliderTT126/8.4)-3.81;
strTT126.Format("%.02f", dTT126);
SDSWriteFIFO(Ident, (const unsigned char*)strTT126.GetString());
还要注意我使用了 GetString()
传递字符串的成员函数。当 SDSWriteFIFO()
参数的类型为 const unsigned char *
时,可以使用此方法。
如果参数不是const并且是您编写的函数,请尝试使该参数为const。否则你必须使用 GetBuffer()
而不是已经使用过。但是之后别忘了打电话给 ReleaseBuffer()
:
Note also that I have used the GetString()
member function to pass the string. This can be used when the SDSWriteFIFO()
parameter is of type const unsigned char*
.
If the parameter is not const and that is a function written by you, try to make that parameter const. Otherwise you have to use GetBuffer()
instead as already used. But then don't forget to call ReleaseBuffer()
afterwards:
LPSTR buf = strTT126.GetBuffer(strTT126.GetLength());
SDSWriteFIFO(Ident, (unsigned char*)buf);
strTT126.ReleaseBuffer();
你有可能使用原语吗?
你可以创建一个联盟:
Is it possible for you to use primitives?
You can create a union:
union {
*double number;
*unsigned char letters;
};
然后你可以通过信件读取数字和数字。
You can then read the number in to number and out via letters.
所以我最终得到了这个解决方案,用于制作SDS框架的双重内容。数据限制为每帧1个字节。通过物理测量验证输出是真的。
So I end up with this solution for crafting double in to the SDS frame. The data is limited to 1 byte per frame. The output is verified true with physical measurement.
int CSoftingCANL2::SDSWriteFIFO(unsigned long Ident, double nDigit){
CString strAllDigits;
unsigned char pData[4] = {NULL};
strAllDigits.Format("%.2f", nDigit);
int nLength = 3;
for(int i = 0; i < strAllDigits.GetLength(); i++)
{
pData[0] = 0x00; // EOID 0
pData[1] = 0xCE; // Attribute 206 for transmit
pData[2] = (unsigned char)strAllDigits.GetAt(i); // Get all ASCII character of the double
int nfrc = CANL2_send_data(hcan1, Ident, 0, nLength, (unsigned char*)pData);
}
return nfrc;}
这篇关于如何将double或string转换为unsigned char *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!