如何在c ++中通过串口发送int?

时间:2017-11-24 22:35:53

标签: c++ char serial-port int

我正在开发Project Cars的游戏模拟器,我有从PC的共享内存中获取数据的代码,但现在我必须通过串口发送它,它必须发送到arduino。我已经搜索了如何做到这一点,但我还没有找到如何将一个Int(这是API从游戏中获取的数据)发送到串口。

这是我的代码

// Used for memory-mapped functionality
#include <windows.h>
#include "sharedmemory.h"

// Used for this example
#include <stdio.h>
#include <conio.h>

//Serial
#include <iostream>
#include "SerialClass.h"
using namespace std;

// Name of the pCars memory mapped file
#define MAP_OBJECT_NAME "$pcars$"

int main()
{
    SetConsoleTitle("API F1 Steering Wheel for Project Cars");

    Serial* Puerto = new Serial("COM4");

    // Open the memory-mapped file
    HANDLE fileHandle = OpenFileMapping( PAGE_READONLY, FALSE, MAP_OBJECT_NAME );
    if (fileHandle == NULL)
    {
        printf( "Could not open file mapping object (%d).\n", GetLastError() );
        return 1;
    }

    // Get the data structure
    const SharedMemory* sharedData = (SharedMemory*)MapViewOfFile( fileHandle, PAGE_READONLY, 0, 0, sizeof(SharedMemory) );
    if (sharedData == NULL)
    {
        printf( "Could not map view of file (%d).\n", GetLastError() );

        CloseHandle( fileHandle );
        return 1;
    }

    // Ensure we're sync'd to the correct data version
    if ( sharedData->mVersion != SHARED_MEMORY_VERSION )
    {
        printf( "Data version mismatch\n");
        return 1;
    }

    //------------------------------------------------------------------------------
    // TEST DISPLAY CODE
    //------------------------------------------------------------------------------
    printf( "ESC TO EXIT\n\n", sharedData->mUnfilteredSteering );
    while (true)
    {
        const bool isValidParticipantIndex = sharedData->mViewedParticipantIndex != -1 && sharedData->mViewedParticipantIndex < sharedData->mNumParticipants && sharedData->mViewedParticipantIndex < STORED_PARTICIPANTS_MAX;
        if ( isValidParticipantIndex )
        {
            const ParticipantInfo& viewedParticipantInfo = sharedData->mParticipantInfo[sharedData->mViewedParticipantIndex];
            printf( "mParticipantName: (%s)\n", viewedParticipantInfo.mName );
            printf( "lap Distance = %f \n", viewedParticipantInfo.mCurrentLapDistance );
            printf( "mWorldPosition: (%f,%f,%f)\n", viewedParticipantInfo.mWorldPosition[0], viewedParticipantInfo.mWorldPosition[1], viewedParticipantInfo.mWorldPosition[2] );
        }
        printf( "mGameState: (%d)\n", sharedData->mGameState );
        printf( "mSessionState: (%d)\n", sharedData->mSessionState );
        printf( "mRaceState: (%d)\n", sharedData->mRaceState );
        printf( "mGear: (%d)\n", sharedData-> mGear);
        printf( "mRpm: (%d)\n", sharedData->mRpm);
        printf( "mSpeed: (%d)\n", sharedData->mSpeed);
        system("cls");


        while (Puerto->IsConnected())
        {
            Puerto->WriteData(sharedData->mGear, sizeof(sharedData->mGear)); // Envía al puerto el texto "Luz_ON".
            break;
        }

        if ( _kbhit() && _getch() == 27 ) // check for escape
        {
            break;
        }
    }
    //------------------------------------------------------------------------------

    // Cleanup
    UnmapViewOfFile( sharedData );
    CloseHandle( fileHandle );

    return 0;
}

1 个答案:

答案 0 :(得分:1)

我的猜测是改变这一行

Puerto->WriteData(sharedData->mGear, sizeof(sharedData->mGear));

到此:

Puerto->WriteData(&sharedData->mGear, sizeof(sharedData->mGear));