UE5编译并以插件形式集成cserialport串口库3-尝试调用-以Actor形式

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#ifdef _WIN32
#include <windows.h>
#define imsleep(microsecond) Sleep(microsecond) // ms
#else
#include <unistd.h>
#define imsleep(microsecond) usleep(1000 * microsecond) // ms
#endif

#include <vector>

#include "CSerialPort/SerialPort.h"
#include "CSerialPort/SerialPortInfo.h"
using namespace itas109;

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "TestActor.generated.h"

class TestListener;

UCLASS()
class CSERIALPORT_API ATestActor : public AActor
{
    GENERATED_BODY()
    
public:    
    // Sets default values for this actor's properties
    ATestActor();

    UFUNCTION(BlueprintCallable)
    bool StartSerialPort();

    UFUNCTION(BlueprintCallable)
    bool returnPortInfo();

    FString char2hexstr(const char* str,int len);

    UFUNCTION(BlueprintCallable)
    bool Listen(FString PortName);

    UFUNCTION(BlueprintCallable)
    bool Read();
    
protected:
    // Called when the game starts or when spawned
    virtual void BeginPlay() override;

    CSerialPort sp;

    TestListener* ListenerPtr;

public:    
    // Called every frame
    virtual void Tick(float DeltaTime) override;

    int countRead = 0;
};




int countRead = 0;

class TestListener : public CSerialPortListener,public CSerialPortHotPlugListener
{
public:
    TestListener(CSerialPort *sp): p_sp(sp){};

    void onReadEvent(const char* portName, unsigned readBufferLen) override;

    void onHotPlugEvent(const char* portName, int isAdd) override;

    bool StartListen();

    bool ShowPortInfo();

    bool SetPortName(char* PortName);

    FString ReadPort();

private:
    CSerialPort *p_sp;
    CSerialPort M_SerialPort;
    char* M_PortName;
};

 

posted @ 2025-01-20 19:39  mcwhirr  阅读(13)  评论(0)    收藏  举报