记录下以前的一个项目中用到的串口通信
当时需求是一个HTC的VR项目,但是VR中的交互不再是HTC的手柄了,需要外接一个其他的硬件设备,通过获取那个硬件设备中发来的数据在VR中应用展示出来。因此是通过串口来后去外接设备的数据,其实说白了像HTC,COSMOS这种VR设备通过数据线链接电脑来传输数据展示的设备,都是通过串口来进行通信的,因此串口通信使用范围还是很广泛的。
接下来说下我使用的脚本来自于:https://github.com/RVillani/UE4Duino/releases/tag/2.2.5
提一句,脚本本人测试是4.23和4.24可用,其他版本还没有过多的进行测试。其他版本如果遇什么问题或者报错,可能需要自行调整下代码。(代码会在最后提供)
首先需要将头文件跟源文件放在项目的Source的项目里,或者自己随便新建一个类然后把.h和.cpp中的代码对应的复制进去,然后编译下,等编译通过就行。(所有方法都可以在蓝图中调用)
需要注意的是要一开始(在begin里)打开串口,Port端口号,BaudRate波特率,然后使用时需要判断一下变量Serial是否有值,如果没值请查看是否链接上,或者端口号是否填写正确。打开串口有两种方式,见下图:
需要注意的时,退出应用时或者你切换Level后不需要串口了一定要调用ClosePort,否则不关闭的话,这个会一直被占用着,除非你直接拔掉数据线或者关机,否则不会自动关闭。
具体的内容和注意事项就这些,下面直接上Git上的代码。
以下是Serial.h的代码
#pragma once
#define FC_DTRDSR 0x01
#define FC_RTSCTS 0x02
#define FC_XONXOFF 0x04
#define ASCII_BEL 0x07
#define ASCII_BS 0x08
#define ASCII_LF 0x0A
#define ASCII_CR 0x0D
#define ASCII_XON 0x11
#define ASCII_XOFF 0x13
#include "CoreTypes.h"
#include "Serial.generated.h"
// Forward declaration
typedef struct _OVERLAPPED OVERLAPPED;
UENUM(BlueprintType, Category = "UE4Duino")
enum class ELineEnd : uint8
{
rn UMETA(DisplayName = "\r\n"),
n UMETA(DisplayName = "\n"),
r UMETA(DisplayName = "\r"),
nr UMETA(DisplayName = "\n\r")
};
UCLASS(BlueprintType, Category = "UE4Duino", meta = (Keywords = "com arduino serial"))
class TEST05_API USerial : public UObject
{
GENERATED_BODY()
public:
/** Determines the line ending used when writing lines to serial port with PrintLine. */
UPROPERTY(BlueprintReadWrite, Category = "UE4Duino | String")
ELineEnd WriteLineEnd;
public:
USerial();
~USerial();
/**
* Open a serial port and return the created Serial instance.
* Don't forget to close the port before exiting the game.
*
* @param bOpened If the serial port was successfully opened.
* @param Port The serial port to open.
* @param BaudRate BaudRate to open the serial port with.
* @return A Serial instance to work with the opened port.
*/
UFUNCTION(BlueprintCallable, meta = (DisplayName = "Open Serial Port"), Category = "UE4Duino", meta = (Keywords = "com arduino serial start"))
static USerial* OpenComPort(bool &bOpened, int32 Port = 1, int32 BaudRate = 9600);
/**
* Utility function to convert 4 bytes into an Integer. If the input array's length is not 4, returns 0.
*
* @param Bytes A byte array with 4 values representing the integer in little-endian format.
* @return The final integer value or 0 for an invalid array.
*/
UFUNCTION(BlueprintPure, meta = (DisplayName = "Bytes to Int"), Category = "UE4Duino", meta = (Keywords = "cast concatenate group bit bitwise"))
static int32 BytesToInt(TArray<uint8> Bytes);
/**
* Utility function to get the 4 bytes that make an integer.
*
* @param Int The integer value to be converted.
* @return A byte array containing the 4 bytes that make the integer, starting from the least significant one (little endian).
*/
UFUNCTION(BlueprintPure, meta = (DisplayName = "Int to Bytes"), Category = "UE4Duino", meta = (Keywords = "cast separate bit bitwise"))
static TArray<uint8> IntToBytes(const int32 &Int);
/**
* Utility function to convert 4 bytes into a float. If the input array's length is not 4, returns 0.0.
*
* @param Bytes A byte array with 4 values representing the float in IEEE 754 standard format.
* @return The final float value or 0.0 for an invalid array.
*/
UFUNCTION(BlueprintPure, meta = (DisplayName = "Bytes to Float"), Category = "UE4Duino", meta = (Keywords = "cast concatenate group bit bitwise"))
static float BytesToFloat(TArray<uint8> Bytes);
/**
* Utility function to get the 4 bytes that make a float.
*
* @param Float The float value to be converted.
* @return A byte array containing the 4 bytes that make the float, in IEEE 754 standard format.
*/
UFUNCTION(BlueprintPure, meta = (DisplayName = "Float to Bytes"), Category = "UE4Duino", meta = (Keywords = "cast separate bit bitwise"))
static TArray<uint8> FloatToBytes(const float &Float);
/**
* Open a serial port. Don't forget to close the port before exiting the game.
* If this Serial instance has already an opened port,
* return false and doesn't change the opened port number.
*
* @param Port The serial port to open.
* @param BaudRate BaudRate to open the serial port with.
* @return If the serial port was successfully opened.
*/
UFUNCTION(BlueprintCallable, meta = (DisplayName = "Open Port"), Category = "UE4Duino", meta = (Keywords = "com start init"))
bool Open(int32 Port = 2, int32 BaudRate = 9600);
/**
* Close and end the communication with the serial port. If not open, do nothing.
*/
UFUNCTION(BlueprintCallable, meta = (DisplayName = "Close Port"), Category = "UE4Duino", meta = (Keywords = "com end finish release"))
void Close();
/**
* Will read characters from Serial port until \0 (null char) is found or there are no
* characters left to read.
*
* @param bSuccess If there was anything to read.
* @return The read string
*/
UFUNCTION(BlueprintCallable, meta = (DisplayName = "Read String", keywords = "get read receive string words text characters"), Category = "UE4Duino")
FString ReadString(bool &bSuccess);
/**
* Will read characters from Serial port until \r\n (Arduino println line end) is found.
*
* @param bSuccess If there was anything to read.
* @return The read string
*/
UFUNCTION(BlueprintCallable, meta = (DisplayName = "Read Line", keywords = "get read receive string words text characters"), Category = "UE4Duino")
FString Readln(bool &bSuccess);
/**
* Reads the string until a specific char is met.
* The Terminator char won't be included in the result string.
*/
//U