• 大小: 285KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-06-14
  • 语言: 其他
  • 标签: UE4  串口  C++  

资源简介

UE4串口里面封装了蓝图可调用接收数据,与读写数据,详细里面有说明,4.24.3版本亲测可用,串口编码等等

资源截图

代码片段和文件信息

//based off the “Arduino and C++ (for Windows)“ code found at: http://playground.arduino.cc/Interfacing/CPPWindows

#include “Serial.h“

#include “Windows/AllowWindowsPlatformTypes.h“
#include “Windows/MinWindows.h“
#include “Windows/HideWindowsPlatformTypes.h“
#include “..\Public\Serial.h“
#include
 #include
 #include
 using namespace std;
#define BOOL2bool(B) B == 0 ? false : true

USerial* USerial::OpenComPort(bool &bOpened int32 Port int32 BaudRate)
{
USerial* Serial = Newobject();
bOpened = Serial->Open(Port BaudRate);
return Serial;
}

int32 USerial::BytesToInt(TArray Bytes)
{
if (Bytes.Num() != 4)
{
return 0;
}

return *reinterpret_cast(Bytes.GetData());
}

TArray USerial::IntToBytes(const int32 &Int)
{
TArray Bytes;
Bytes.Append(reinterpret_cast(&Int) 4);
return Bytes;
}

float USerial::BytesToFloat(TArray Bytes)
{
if (Bytes.Num() != 4)
{
return 0;
}

return *reinterpret_cast(Bytes.GetData());
}

TArray USerial::FloatToBytes(const float &Float)
{
TArray Bytes;
Bytes.Append(reinterpret_cast(&Float) 4);
return Bytes;
}

USerial::USerial()
: WriteLineEnd(ELineEnd::n)
 m_hIDComDev(nullptr)
 m_OverlappedRead(nullptr)
 m_OverlappedWrite(nullptr)
 m_Port(-1)
 m_Baud(-1)
{
// Allocate the OVERLAPPED structs
m_OverlappedRead = new OVERLAPPED();
m_OverlappedWrite = new OVERLAPPED();

FMemory::Memset(m_OverlappedRead 0 sizeof(OVERLAPPED));
FMemory::Memset(m_OverlappedWrite 0 sizeof(OVERLAPPED));
}

USerial::~USerial()
{
Close();

// Delete allocated OVERLAPPED structs
delete m_OverlappedRead;
delete m_OverlappedWrite;
}

bool USerial::Open(int32 nPort int32 nBaud)
{
if (nPort < 0)
{
UE_LOG(LogTemp Error TEXT(“Invalid port number: %d“) nPort);
return false;
}
if (m_hIDComDev)
{
UE_LOG(LogTemp Warning TEXT(“Trying to use opened Serial instance to open a new one. “
“Current open instance port: %d | Port tried: %d“) m_Port nPort);
return false;
}

FString szPort;
if (nPort < 10)
szPort = FString::Printf(TEXT(“COM%d“) nPort);
else
szPort = FString::Printf(TEXT(“\\\\.\\COM%d“) nPort);
DCB dcb;

m_hIDComDev = CreateFile(*szPort GENERIC_READ | GENERIC_WRITE 0 NULL OPEN_EXISTING FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED NULL);
if (m_hIDComDev == NULL)
{
unsigned long dwError = GetLastError();
UE_LOG(LogTemp Error TEXT(“Failed to open port COM%d (%s). Error: %08X“) nPort *szPort dwError);
return false;
}

FMemory::Memset(m_OverlappedRead 0 sizeof(OVERLAPPED));
FMemory::Memset(m_OverlappedWrite 0 sizeof(OVERLAPPED));

COMMTIMEOUTS CommTimeOuts;
//CommTimeOuts.ReadIntervalTimeout = 10;
CommTimeOuts.ReadIntervalTimeout = 0xFFFFFFFF;
CommTimeOuts.ReadTotalTimeoutMultiplier = 0;
CommTimeOuts.ReadTotalTimeoutConsta

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2020-04-17 09:39  SerialPortUE4\
     文件       61970  2017-05-29 10:18  SerialPortUE4\Example.jpg
     文件       52429  2017-05-29 09:08  SerialPortUE4\Example2.jpg
     文件       45113  2017-05-29 10:02  SerialPortUE4\Example3.JPG
     文件      208635  2017-05-29 10:30  SerialPortUE4\Example4.JPG
     文件      131491  2017-06-07 09:29  SerialPortUE4\Example5.JPG
     文件         995  2020-04-17 09:28  SerialPortUE4\Read.txt
     文件        9350  2020-04-17 09:05  SerialPortUE4\Serial.cpp
     文件       10192  2020-04-17 09:05  SerialPortUE4\Serial.h

评论

共有 条评论