I have two related qestions on the code included belo开发者_JAVA百科w
1) I am trying to read from a serial port that is part of a Visual C++ Form. I want to create a thread in the InitializeComponent function but I get this error on the form page when I include the call to start the thread:
"Warning 1 Could not find type 'Thread'. Please make sure that the assembly that contains this type is referenced. If this type is a part of your development project, make sure that the project has been successfully built."
2) The thread will run in the static function Read. Read needs to resolve the serial port that is in the main form (serial port is named arduino), but it apparently can't resolve them: "left of .ReadLine' must have class/struct/union"
Suggestions?
using namespace System::IO::Ports;
using namespace System::Threading;
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
private: void static Read(void)
{
while (1)
{
try
{
String^ message = arduino.ReadLine();
// this->ArduinoOutputTextBox->Text = message;
}
catch (TimeoutException ^) { }
}
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ USB_button;
private: System::IO::Ports::SerialPort^ arduino;
private: System::Windows::Forms::TextBox^ ArduinoOutputTextBox;
private: System::ComponentModel::IContainer^ components;
protected:
private:
/// <summary>
/// Required designer variable.
/// </summary>
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
Thread^ readThread = gcnew Thread(gcnew ThreadStart(Read));
this->components = (gcnew System::ComponentModel::Container());
this->USB_button = (gcnew System::Windows::Forms::Button());
this->arduino = (gcnew System::IO::Ports::SerialPort(this->components));
this->ArduinoOutputTextBox = (gcnew System::Windows::Forms::TextBox());
this->SuspendLayout();
arduino
is a reference to an object, not an actual object.
You ned to write arduino->readLine()
.
精彩评论