Aerotech系列文章(2)C++例子运行

该代码示例展示了如何使用QTC++与A3200设备交互,包括建立连接、处理BufferQueue以执行命令以及注册MessageCallback来处理MSGDISPLAY消息。在BufferQueue部分,代码加载并执行命令,同时处理缓冲区满的情况。在MessageCallback部分,实现了对特定消息类型的回调处理,并在接收到MSGDISPLAY时释放主线程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. BufferQueue

1)工程文件

QT = core

CONFIG += c++17 cmdline

# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
        main.cpp

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../SDK/Lib64/ -lA3200C64
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../SDK/Lib64/ -lA3200C64

INCLUDEPATH += $$PWD/../SDK/Include
INCLUDEPATH += $$PWD/../SDK/Bin64
INCLUDEPATH += $$PWD/../SDK/Bin64/ExamplesScript
DEPENDPATH += $$PWD/../SDK/Include

2)main

#include <stdio.h>
#include <tchar.h>

// Main include file
#include "A3200.h"

#include <QDebug>

// See the pre-build event to see the System Library dlls being copied to the output directory

#define NUM_LINES 10
#define LINE_SIZE 128

// This function will print whatever the latest error was
void PrintError();

int main(int argc, char **argv)
{
    bool sameline=false;
    int linecurrent =0;
    int lineprev =0;
    // Handle to give access to A3200
    A3200Handle handle = NULL;
    FILE * file = NULL;
    char tmp[LINE_SIZE], command[LINE_SIZE * NUM_LINES];
    int i;
    // Number of lines left in the queue to execute
    DOUBLE queueLineCount;

    printf("Connecting to A3200. Initializing if necessary.\n");
    if(!A3200Connect(&handle)) { PrintError(); goto cleanup; }

    printf("Putting task into Queue mode.\n");
    if(!A3200ProgramInitializeQueue(handle, TASKID_01)) { PrintError(); goto cleanup; }

    file = fopen("file.txt", "r");
    if(NULL == file)
    {
        printf("Failed to open file\n");
        goto cleanup;
    }

    printf("Loading Queue with commands.\n");
    qDebug() << "loading queue with commands";
    while (!feof(file)) {
        command[0] = '\0';
        for(i = 0; i < NUM_LINES; i++) {
            if(NULL == fgets(tmp, sizeof(tmp), file)) {
                break;
            }
            strcat(command, tmp);
        }
        qDebug() << "command = " << command;

        qDebug() << "before execute commands";

        while(!A3200CommandExecute(handle, TASKID_01, command, NULL)) {
            if (A3200GetLastError().Code == ErrorCode_QueueBufferFull) {
                printf("\tBuffer is full, waiting for it to empty.\n");
                qDebug() << "\tBuffer is full, waiting for it to empty.\n";
                Sleep(1000);
            } else { PrintError(); goto cleanup; }
        }

        qDebug() << "end execute commands";
    }

    printf("Sleeping while remaining commands execute.\n");
    qDebug() << "Sleeping while remaining commands execute.\n";
    if(!A3200StatusGetItem(handle, TASKID_01, STATUSITEM_QueueLineCount, 0, &queueLineCount)) { PrintError(); goto cleanup; }

    while(0 != (int)queueLineCount ) {
        Sleep(10);
        if(!A3200StatusGetItem(handle, TASKID_01, STATUSITEM_QueueLineCount, 0, &queueLineCount)) { PrintError(); }
        qDebug() << "queueLineCount" << queueLineCount;
    }

cleanup:
    if(NULL != file)
    {
        fclose(file);
    }
    if(NULL != handle) {
        printf("Stopping using Queue mode.\n");
        qDebug() << "Stopping using Queue mode.\n";
        if(!A3200ProgramStop(handle, TASKID_01)) { PrintError(); }
        if(!A3200MotionDisable(handle, TASKID_01, AXISMASK_00)) { PrintError(); }
        if(!A3200Disconnect(handle)) { PrintError(); }
    }

#ifdef _DEBUG
    printf("Press ENTER to exit...\n");
    qDebug() << "Press ENTER to exit...\n";
    getchar();
#endif
    return 0;
}

void PrintError() {
    CHAR data[1024];
    A3200GetLastErrorString(data, 1024);
    printf("Error : %s\n", data);
    qDebug() << data;
}

2.MessageCallback

1)工程文件

QT = core

CONFIG += c++17 cmdline

# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
        main.cpp

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target


win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../SDK/Lib64/ -lA3200C64
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../SDK/Lib64/ -lA3200C64

INCLUDEPATH += $$PWD/../SDK/Include
INCLUDEPATH += $$PWD/../SDK/Bin64
INCLUDEPATH += $$PWD/../SDK/Bin64/ExamplesScript
DEPENDPATH += $$PWD/../SDK/Include

2)主程序

#include <stdio.h>
#include <tchar.h>

// Main include file
#include "A3200.h"

// See the pre-build event to see the System Library dlls being copied to the output directory

// This function will print whatever the latest error was
void PrintError();

// This function will handle the MSGDISPLAY callback
ErrorData myMessageDisplay(A3200Handle, TASKID, MessageDisplayData*);

int main(int argc, char **argv)
{
    // Handle to give access to A3200
    A3200Handle handle = NULL;
    // Structure with function pointers
    MessageCallbackHandlers handlers;

    printf("Connecting to A3200. Initializing if necessary.\n");
    if(!A3200Connect(&handle)) { PrintError(); goto cleanup; }

    printf("Initialing the structure, and registering only for MSGDISPLAY handling\n");
    if(!A3200CallbackInitializeMessageHandlers(&handlers, sizeof(MessageCallbackHandlers))) { PrintError(); goto cleanup; }
    handlers.messageDisplay = myMessageDisplay;
    // multiple different handlers can be added here, messageDisplay is just an example

    printf("Waiting for MSGDISPLAY on Task #1\n");
    // This call is blocking
    if(!A3200CallbackHandleMessageCallbacks(handle, TASKID_01, &handlers)) { PrintError(); goto cleanup; }

cleanup:
    if(handle != NULL) {
        if(!A3200Disconnect(handle)) { PrintError(); }
    }

#ifdef _DEBUG
    printf("Press ENTER to exit...\n");
    getchar();
#endif
    return 0;
}

ErrorData myMessageDisplay(A3200Handle handle, TASKID taskId, MessageDisplayData* data) {
    // Print what we recieved
    printf("MSGDISPLAY from task %d with priority %d : %s\n", taskId, data->priority, data->text);

    // Release the main thread that is waiting for callbacks since we do not want to handle any more MSGDISPLAYs
    if(!A3200CallbackWaitCancel(handle)) { PrintError(); }

    // Any error we return from here will result in a task error
    return A3200GetLastError();
}

void PrintError() {
    CHAR data[1024];
    A3200GetLastErrorString(data, 1024);
    printf("Error : %s\n", data);
}

如果,Motion Compser打开,就会测试出报错信息:

Connecting to A3200. Initializing if necessary.
Initialing the structure, and registering only for MSGDISPLAY handling
Waiting for MSGDISPLAY on Task #1
Error : A callback error occurred: The callback was already registered, and only single registration is supported.
Press ENTER to exit...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值