C++ 如何在c+;中通过串行端口发送int+;?

C++ 如何在c+;中通过串行端口发送int+;?,c++,char,serial-port,int,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 #incl

我正在为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;
}
//用于内存映射功能
#包括
#包括“sharedmemory.h”
//用于此示例
#包括
#包括
//连载
#包括
#包括“SerialClass.h”
使用名称空间std;
//pCars内存映射文件的名称
#定义映射对象名称“$pcars$”
int main()
{
SetConsoleTitle(“API F1项目车方向盘”);
序列号*波多黎各=新序列号(“COM4”);
//打开内存映射文件
HANDLE fileHandle=OpenFileMapping(页面\只读,FALSE,映射\对象\名称);
if(fileHandle==NULL)
{
printf(“无法打开文件映射对象(%d)。\n”,GetLastError());
返回1;
}
//获取数据结构
常量SharedMemory*sharedData=(SharedMemory*)MapViewOfFile(文件句柄,页面为只读,0,0,大小为(SharedMemory));
if(sharedData==NULL)
{
printf(“无法映射文件(%d)的视图。\n”,GetLastError();
CloseHandle(fileHandle);
返回1;
}
//确保我们已同步到正确的数据版本
如果(共享数据->mVersion!=共享内存版本)
{
printf(“数据版本不匹配\n”);
返回1;
}
//------------------------------------------------------------------------------
//测试显示代码
//------------------------------------------------------------------------------
printf(“ESC退出\n\n”,sharedData->mUnfilteredSteering);
while(true)
{
const bool isvalidParticipandIndex=sharedData->MViewedParticipandIndex!=-1&&sharedData->MViewedParticipandIndexmNumParticipants&&sharedData->MViewedParticipandIndex<存储的参与者\u MAX;
if(isValidParticipantIndex)
{
const ParticipantInfo&viewed ParticipantInfo=sharedData->mparticintinfo[sharedData->mviewed participantindex];
printf(“MPParticipantName:(%s)\n”,viewedParticipantInfo.mName);
printf(“圈距=%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”,共享数据->mRpm);
printf(“mSpeed:(%d)\n”,sharedData->mSpeed);
系统(“cls”);
而(波多黎各->断开连接())
{
波多黎各->WriteData(sharedData->mGear,sizeof(sharedData->mGear));///Envía al-Puerto el texto“Luz_ON”。
打破
}
if(_kbhit()&&&&_getch()==27)//检查转义
{
打破
}
}
//------------------------------------------------------------------------------
//清理
取消存档文件(共享数据);
CloseHandle(fileHandle);
返回0;
}

我想我该换一行了

Puerto->WriteData(sharedData->mGear, sizeof(sharedData->mGear));
为此:

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

在这段代码中,它在哪里不工作?同样,字节通常通过线路发送。int可以由2或4个字节组成,因此代码需要将int转换为字节缓冲区,然后将其发送过来至<代码>串行*波多黎各=新串行(“COM4”)。更少的内存管理混乱。请注意,在没有任何类型(消息或数据包)帧(aka协议)的情况下发送多个字节很容易导致字节同步问题。附录:我相信Arduino是little endian,但通常在计算机之间传送数据时。
ntoh()
hton()
例程有助于主机和网络顺序之间的转换。