2017-01-19 13 views
-1

私はベースのシリアル通信オブジェクトのために使用しているlibオブジェクトを持っています。他のC++アプリケーションでは、そのオブジェクトのインスタンスに作成します。 1つはcom 4に、もう1つはcom 6に接続します。2つの異なるデバイスにデータを送信する場合。 1つのシリアルポートから送信されたデータが他のデバイスに表示されています。私はこれが私のCreateFile設定と関係があるかどうか、あるいはこれが起こる原因となるものがあるかどうかを調べようとしています。私はcreatefileの設定とオプションを変更しようとしましたが、クロストークはまだ発生します。シリアル通信とのクロストーク。

ここに私が現在使用しているサンプルコードがあります。誰かが私が見ることができる場所についてのヒントがあれば、それは非常に高く評価されるだろう。

ser_disconnect(); 
    //We're not yet connected 
    connected = false; 

    //std::wstring stemp(L"\\\\.\\COM"); 
    std::wstring stemp(L"COM"); 
    stemp = stemp + std::to_wstring(Port); 
    LPCWSTR sw = stemp.c_str(); 
    //Try to connect to the given port throuh CreateFile 
    hSerial = CreateFile(sw, 
     GENERIC_READ | GENERIC_WRITE, 
     0, 
     NULL, 
     CREATE_NEW,//OPEN_EXISTING, 
     FILE_ATTRIBUTE_NORMAL, 
     NULL); 

    //Check if the connection was successfull 
    if (hSerial == INVALID_HANDLE_VALUE) 
    { 
     //If not success full display an Error 
     if (GetLastError() == ERROR_FILE_NOT_FOUND) { 
      //Print Error if neccessary 
      set_Last_Error("ERROR: Handle was not attached. Reason: Port not available.\n", errMsg); 
      return 0x01; 
     } 
     else 
     { 
      set_Last_Error("Serial Handel ERROR!!!", errMsg); 
      return 0x02; 
     } 
    } 
    else 
    { 
     //If connected we try to set the comm parameters 
     DCB dcbSerialParams = { 0 }; 

     //Try to get the current 
     if (!GetCommState(hSerial, &dcbSerialParams)) 
     { 
      //If impossible, show an error 
      set_Last_Error("failed to get current serial parameters!", errMsg); 
      return 0x03; 
     } 
     else 
     { 
      //Define serial connection parameters for the arduino board 
      dcbSerialParams.BaudRate = BaudRate; 
      dcbSerialParams.ByteSize = Data; 
      dcbSerialParams.StopBits = StopBits; 
      dcbSerialParams.Parity = Parity; 
      //Setting the DTR to Control_Enable ensures that the Arduino is properly 
      //reset upon establishing a connection 
      dcbSerialParams.fDtrControl = DTR_CONTROL_ENABLE; 

      //Set the parameters and check for their proper application 
      if (!SetCommState(hSerial, &dcbSerialParams)) 
      { 
       set_Last_Error("ALERT: Could not set Serial Port parameters", errMsg); 
       return 0x04; 
      } 
      else 
      { 
       //If everything went fine we're connected 
       connected = true; 
       //Flush any remaining characters in the buffers 
       PurgeComm(hSerial, PURGE_RXCLEAR | PURGE_TXCLEAR); 
      } 
     } 
    } 
+0

COM3と6のケーブルを分離しようとしましたか? (これはSWの問題だと確信していますか?) – idanp

+0

シリアルの変数がどのように格納されていたのかが問題になっていたようです。これはcppファイル内のグローバル変数です。これをクラスの領域に入れたらすぐにうまくいきました。 –

答えて

-1

シリアルの変数がどのように格納されていたのかが問題だったようです。これはcppファイル内のグローバル変数です。私はクラスの領域内にこれを置くとすぐにうまくいった