我有一个通过串行端口与设备通信的程序。然而,我在一个部分遇到了困难。当我运行计算出的十六进制命令时,我得到了一个看起来像直角符号的东西。这是代码的两个部分(主要部分和函数)。
主要部分:
private: System::Void poll_Click(System::Object^ sender, System::EventArgs^ e)
{
int i, end;
double a = 1.58730159;
String^ portscan = "port";
String^ translate;
std::string portresponse [65];
std::fill_n(portresponse, 65, "Z");
for (i=0;i<63;i++)
{
if(this->_serialPort->IsOpen)
{
// Command 0 generator
int y = 2;
y += i;
std::string command0[10] = {"0xFF", "0xFF", "0xFF", "0xFF", "0xFF", "0x02", dectohex(i), "0x00", "0x00", dectohex(y)};
// The two "dectohex" values above is where I get the odd symbol
for (end=0;end<10;end++)
{
portscan = marshal_as<String^>( command0[end] );
this->_serialPort->WriteLine(portscan);
}
translate = (this->_serialPort->ReadLine());
MarshalString(translate, portresponse [i]);
if(portresponse [i] != "Z")
{
comboBox7->Items->Add(i);
}
this->progressBar1->Value=a;
a += 1.58730159;
}
}
}
功能:
string dectohex(int i)
{
string hexidecimal = "";
char hex_string[10] = "";
hexidecimal = sprintf (hex_string, "0x%02X", i);
return hexidecimal;
}
我假设我缺少一些相当简单的东西。任何和所有的帮助表示赞赏。
解决方案:根据 David Yaw 的指导。
string dectohex(int i)
{
char hex_array[10];
sprintf (hex_array, "0x%02X", i);
string hex_string(hex_array);
return string(hex_string);
}