我需要对我的编码提供一点帮助..如果我们要写一个字符串并通过一个com端口发送,请参阅下面的编码。如果我们要生成随机字符串并通过com端口发送它,该怎么办?我在“this-> serialPort1-> WriteLine(message);”中实际需要改变什么?“ ?尝试了谷歌的几个代码..它们都没有工作
private: System::Void button4_Click(System::Object^ sender, System::EventArgs^ e) {
//add sender name
String^ name = this->serialPort1->PortName;
// grab text and store in send buffer
String^ message = this->textBox2->Text;
// write to serial
if(this->serialPort1->IsOpen)
//this->_serialPort->WriteLine(String::Format("<{0}>: {1}",name,message));
this->serialPort1->WriteLine(message);
else
this->textBox2->Text="Port Not Opened";
}
答案 0 :(得分:0)
//Sorry for the bad format. Must learn how to use it correctly.
void createRandom(std::string & randString, const int len)
{
static const std::string theCharacters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
for (int i = 0; i < len; ++i)
{
//generate a random number which not bigger that max size of the available characters, then add it to the string.
randString += theCharacters[rand() % (sizeof(theCharacters) - 1)];
}
}
createRandom(message);
this->serialPort1->WriteLine(message);