2013年12月29日 星期日

[openCV] 連結Arduino using Visual C++

要用CLR主控台應用程式,非Win32主控台應用程式,這樣才可以用System命名空間

  • using namespace System;
  • using namespace System::IO::Ports;


















宣告comport,並指定portName與baudrate

  • int baudRate = 9600;
  • String^ portName = "COM5";
  • SerialPort^ arduino;
  • arduino = gcnew SerialPort(portName, baudRate);

用try catch處理Serialport

  1. try
  2. {
  3. ....
  4. }
  5. catch (IO::IOException^ e  ) 
  6. Console::WriteLine(e->GetType()->Name+": Port is not ready");
  7. }
  8. catch (ArgumentException^ e)
  9. {
  10. Console::WriteLine(e->GetType()->Name+": incorrect port name syntax, must start with COM/com");
  11. }

打開SerialPort

  • arduino->Open();

傳送字串

  • arduino->WriteLine(String);

關閉SerialPort

  • arduino->Close();
Reference












沒有留言:

張貼留言