C ++ Arduino慢速串行通信

时间:2014-06-30 17:47:52

标签: c++ serial-port arduino processing

我的Arduino UNO附有陀螺仪(MPU6050)。我编写了一个处理脚本来读取陀螺仪的串行数据(获取角度)并且工作正常。 现在我想将它与C ++ OpenCV项目集成。我使用这个教程类进行串行通信:http://playground.arduino.cc/Interfacing/CPPWindows

然而,串行通信非常慢,256个字符串阵列在1.5秒内填充。多达170个字符/秒= 1.36 kbps。

所以问题是:如果串口快速处理,那么使用C ++会慢得多吗?有没有其他方法可以将陀螺仪数据读取到C ++?

这是我从网络摄像头捕获的每一帧之后触发的功能,以获取串行数据

int Serial::ReadandClose(char *buffer, unsigned int nbChar, const char* portName) 
{ 

     wchar_t wtext[20];
 mbstowcs(wtext, portName, strlen(portName)+1);//Plus null
 LPWSTR PortSpecifier = wtext;

 DCB dcb; 
 int retVal; 
 BYTE Byte; 
 DWORD dwBytesTransferred; 
 DWORD dwCommModemStatus; 

 HANDLE hPort = CreateFile( 
 PortSpecifier, 
GENERIC_READ, 
 0, 
 NULL, 
 OPEN_EXISTING, 
 0, 
 NULL 
 ); 
if (!GetCommState(hPort,&dcb)) 
 return 0x100; 
 dcb.BaudRate = CBR_115200; 
 dcb.ByteSize = 8; //8 data bits 
 dcb.Parity = NOPARITY; //no parity 
 dcb.StopBits = ONESTOPBIT; //1 stop 

 if (!SetCommState(hPort,&dcb)) 
 return 0x100; 
  SetCommMask (hPort, EV_RXCHAR | EV_ERR); //receive character event 
 WaitCommEvent (hPort, &dwCommModemStatus, 0); //wait for character 

 if (dwCommModemStatus & EV_RXCHAR) 
     {
         clock_t begin =clock();
    ReadFile (hPort, buffer, 256, &dwBytesTransferred, 0);
    clock_t end = clock();
    double elapsed_secs = double(end-begin) / CLOCKS_PER_SEC;
    cout<<elapsed_secs<<endl;
 }
else if (dwCommModemStatus & EV_ERR) 
 retVal = 0x101; 

 CloseHandle(hPort); 
 return dwBytesTransferred; 
    } 

0 个答案:

没有答案