串行 COM 编程:随机数
Serial COM Programming : Random Numbers
我的编码需要一些帮助。如果我们要编写一个字符串并通过 com 端口发送,请参阅下面的编码。如果我们要生成随机字符串并通过 com 发送它会怎样端口.. 我实际上必须在 "this->serialPort1->WriteLine(message);" 中更改什么?尝试了 google 中的几个代码。其中 none 个有效
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";
}
//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);
我的编码需要一些帮助。如果我们要编写一个字符串并通过 com 端口发送,请参阅下面的编码。如果我们要生成随机字符串并通过 com 发送它会怎样端口.. 我实际上必须在 "this->serialPort1->WriteLine(message);" 中更改什么?尝试了 google 中的几个代码。其中 none 个有效
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";
}
//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);