-1

我试图用 C++ 编写一个程序,让我的计算机与一个 trinamic steprockerboard 通信。他们已经提供了一个示例文件,让您上路。这很完美,但真的很简单。

现在我想从 USB 设备读取结果,他们为此做了一个函数:

    //Read the result that is returned by the module
    //Parameters: Handle: handle of the serial port, as returned by OpenRS232
    //Address: pointer to variable to hold the reply address returned by the module 
    // Status: pointer to variable to hold the status returned by the module (100 means okay)
    //Value: pointer to variable to hold the value returned by the module
prototype:
   UCHAR GetResult(HANDLE Handle, UCHAR *Address, UCHAR *Status, int *Value)

现在我写了以下内容:

            UCHAR* adr;
            UCHAR* stat;
            int* val;

         SendCmd(RS232Handle, 1, TMCL_MVP, 0, 0, -3200); // move to next position 
         SendCmd(RS232Handle,1, TMCL_GAP, 8, 0, 0); // tell motor to look if position is reached
         GetResult(RS232Handle,adr,stat,val); //ask for the result, value must give a 1 if so 
         printf("results from USB device:   adr=%d, stat=%d, val=%d\n", adr, stat, val);

但是当我运行程序并尝试此选项时,程序会崩溃。

有没有人知道可能导致问题的原因?(我提供的代码只是我的程序的一部分,整个代码可以在下面找到。我并不是要你阅读整个代码,问题应该在上面,但仅供有兴趣的人使用,其余的我也放了在这里)

// TMCLTest.cpp : Show how to communicate with a TMCM module in TMCL
//

#include "stdafx.h"
#include <iostream>

//Opcodes of all TMCL commands that can be used in direct mode
#define TMCL_ROR 1
#define TMCL_ROL 2
#define TMCL_MST 3
#define TMCL_MVP 4
#define TMCL_SAP 5
#define TMCL_GAP 6
#define TMCL_STAP 7

enter code here#define TMCL_RSAP 8 enter code here#define TMCL_SGP 9 #define TMCL_GGP 10 #define TMCL_STGP 11 #define TMCL_RSGP 12 #define TMCL_RFS 13 #define TMCL_SIO 14 #define TMCL_GIO 15 #define TMCL_SCO 30 #define TMCL_GCO 31 #define TMCL_CCO

//Opcodes of TMCL control functions (to be used to run or abort a TMCL program in the module)
#define TMCL_APPL_STOP 128
#define TMCL_APPL_RUN 129
#define TMCL_APPL_RESET 131

//Options for MVP commandds
#define MVP_ABS 0
#define MVP_REL 1
#define MVP_COORD 2

//Options for RFS command
#define RFS_START 0
#define RFS_STOP 1
#define RFS_STATUS 2

#define FALSE 0
#define TRUE 1

//Result codes for GetResult
#define TMCL_RESULT_OK 0
#define TMCL_RESULT_NOT_READY 1
#define TMCL_RESULT_CHECKSUM_ERROR 2


//Open serial interface
//Usage: ComHandle=OpenRS232("COM1", CBR_9600)
HANDLE OpenRS232(const char* ComName, DWORD BaudRate)
{
HANDLE ComHandle;
DCB CommDCB;
COMMTIMEOUTS CommTimeouts;

ComHandle=CreateFile(ComName, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING,                 FILE_ATTRIBUTE_NORMAL, NULL);
if(GetLastError()!=ERROR_SUCCESS) return INVALID_HANDLE_VALUE;
else
{
    GetCommState(ComHandle, &CommDCB);

    CommDCB.BaudRate=BaudRate;
    CommDCB.Parity=NOPARITY;
    CommDCB.StopBits=ONESTOPBIT;
    CommDCB.ByteSize=8;

    CommDCB.fBinary=1;  //Binary Mode only
    CommDCB.fParity=0;
    CommDCB.fOutxCtsFlow=0;
    CommDCB.fOutxDsrFlow=0;
    CommDCB.fDtrControl=0;
    CommDCB.fDsrSensitivity=0;
    CommDCB.fTXContinueOnXoff=0;
    CommDCB.fOutX=0;
    CommDCB.fInX=0;
    CommDCB.fErrorChar=0;
    CommDCB.fNull=0;
    CommDCB.fRtsControl=RTS_CONTROL_TOGGLE;
    CommDCB.fAbortOnError=0;

    SetCommState(ComHandle, &CommDCB);

    //Set buffer size
    SetupComm(ComHandle, 100, 100);

//Set up timeout values (very important, as otherwise the program will be very slow)
    GetCommTimeouts(ComHandle, &CommTimeouts);

    CommTimeouts.ReadIntervalTimeout=MAXDWORD;
    CommTimeouts.ReadTotalTimeoutMultiplier=0;
    CommTimeouts.ReadTotalTimeoutConstant=0;

    SetCommTimeouts(ComHandle, &CommTimeouts);

    return ComHandle;
  }
}


//Close the serial port
//Usage: CloseRS232(ComHandle);
void CloseRS232(HANDLE Handle)
{
CloseHandle(Handle);
}


//Send a binary TMCL command
//e.g.  SendCmd(ComHandle, 1, TMCL_MVP, MVP_ABS, 1, 50000);   will be MVP ABS, 1, 50000 for module with address 1
//Parameters: Handle: Handle of the serial port (returned by OpenRS232).
//            Address: address of the module (factory default is 1).
//            Command: the TMCL command (see the constants at the begiining of this file)
//            Type:    the "Type" parameter of the TMCL command (set to 0 if unused)
//            Motor:   the motor number (set to 0 if unused)
//            Value:   the "Value" parameter (depending on the command, set to 0 if unused)
void SendCmd(HANDLE Handle, UCHAR Address, UCHAR Command, UCHAR Type, UCHAR Motor, INT Value)
{
UCHAR TxBuffer[9];
DWORD BytesWritten;
int i;

TxBuffer[0]=Address;
TxBuffer[1]=Command;
TxBuffer[2]=Type;
TxBuffer[3]=Motor;
TxBuffer[4]=Value >> 24;
TxBuffer[5]=Value >> 16;
TxBuffer[6]=Value >> 8;
TxBuffer[7]=Value & 0xff;
TxBuffer[8]=0;
for(i=0; i<8; i++)
    TxBuffer[8]+=TxBuffer[i];

//Send the datagram
WriteFile(Handle, TxBuffer, 9, &BytesWritten, NULL);
}


//Read the result that is returned by the module
//Parameters: Handle: handle of the serial port, as returned by OpenRS232
//            Address: pointer to variable to hold the reply address returned by the module
//            Status: pointer to variable to hold the status returned by the module (100 means okay)
//            Value: pointer to variable to hold the value returned by the module
//Return value: TMCL_RESULT_OK: result has been read without errors
//              TMCL_RESULT_NOT_READY: not enough bytes read so far (try again)
//              TMCL_RESULT_CHECKSUM_ERROR: checksum of reply packet wrong
UCHAR GetResult(HANDLE Handle, UCHAR *Address, UCHAR *Status, int *Value)
{
UCHAR RxBuffer[9], Checksum;
DWORD Errors, BytesRead;
COMSTAT ComStat;
int i;

//Check if enough bytes can be read
ClearCommError(Handle, &Errors, &ComStat);
if(ComStat.cbInQue>8)
{
    //Receive
    ReadFile(Handle, RxBuffer, 9, &BytesRead, NULL);

    Checksum=0;
    for(i=0; i<8; i++)
        Checksum+=RxBuffer[i];

    if(Checksum!=RxBuffer[8]) return TMCL_RESULT_CHECKSUM_ERROR;

    *Address=RxBuffer[0];
    *Status=RxBuffer[2];
    *Value=(RxBuffer[4] << 24) | (RxBuffer[5] << 16) | (RxBuffer[6] << 8) | RxBuffer[7];
} else return TMCL_RESULT_NOT_READY;

return TMCL_RESULT_OK;
}


int main(int argc, char* argv[])
{
int i;
int Type, Motor, Velocity, Position,ref1;
UCHAR Address, Status;
int Value, Timeout;

HANDLE RS232Handle;

RS232Handle=OpenRS232("COM3", 9600);

// set parameters
SendCmd(RS232Handle, 1, TMCL_SAP, 140, 0, 5); //SAP 140, 0, 5              // set             microsteps to 32 (32 additional steps per step of 1.8 degr.)
//SAP 4, 0, 500              //set max vel.
//SAP 5, 0, 100              //set max acc.
//SAP 6, 0, 255              //set abs. max current to  2.8 ampere

//SAP 7, 0, 50 //设置待机电流 ((50/250)*2.8A)

printf("VPI Test Setup\n \n" );
do
{
    printf("1 - Rotate clockwise (10 rotations)\n");
    printf("2 - Rotate counter-clockwise (10 rotations)\n");
    printf("3 - Stop motor\n");
    printf("4 - Start test (First 100 rotations clockwise, \n then 100 rotations counter clockwise)\n");

    printf("\n99 - End\n");

    scanf("%d", &i);
    switch(i)
    {
        case 1:
            SendCmd(RS232Handle, 1, TMCL_MVP, 0, 0, 32000); //ABS(4th parameter) = 0
            break;

        case 2:
            SendCmd(RS232Handle, 1, TMCL_MVP, 0, 0, -32000); //ABS(4th parameter) = 0
            break;

        case 3:
            SendCmd(RS232Handle, 1, TMCL_MST, 0, 0, 0);
            break;

        case 4:
//SendCmd(RS232Handle, 1, TMCL_RFS,0,0,0);
            printf("Test started \n" );

            //UCHAR done;
            UCHAR* adr;
            UCHAR* stat;
            int* val;

            //SendCmd(RS232Handle, 1, TMCL_SAP, 193, 1, 2); //SAP 193,1,2
           SendCmd(RS232Handle, 1, TMCL_MVP, 0, 0, -3200); //ABS(4th parameter) = 0
           SendCmd(RS232Handle,1, TMCL_GAP, 8, 0, 0);
           GetResult(RS232Handle,adr,stat,val);
           printf("results from USB device:   adr=%d, stat=%d, val=%d\n", adr, stat, val);
           //CHAR GetResult(HANDLE Handle, UCHAR *Address, UCHAR *Status, int *Value)

          // if(done != 2)
          // {
          //       printf("rotation backwards started \n");
           //      SendCmd(RS232Handle, 1, TMCL_MVP, 0, 0, 3200);
          // }

          // }


            break;
    }
SendCmd(RS232Handle, 1, TMCL_ROL, 0, Motor, Velocity);
SendCmd(RS232Handle, 1, TMCL_ROL, 0, Motor, Velocity);
    if(i==1 || i==2 || i==3 || i==4)
    {
        Address=0;
        Status=0;
        Value=0;
        Timeout=GetTickCount();
        while(GetResult(RS232Handle, &Address, &Status, &Value)             ==TMCL_RESULT_NOT_READY && abs(GetTickCount()-Timeout)<1000);
        printf("Result: Address=%d, Status=%d, Value=%d\n", Address, Status, Value);
    }
}
while(i!=99);

CloseRS232(RS232Handle);

return 0;
}
4

1 回答 1

1

地址、状态和值应指向有效变量。因此,您的代码应类似于:

UCHAR adr;
UCHAR stat;
int val;

GetResult(RS232Handle,&adr,&stat,&val);

现在发生的情况是您的变量(adr、stat 和 val)未初始化,因此它们指向内存中的随机位置。当您将这些变量传递给 GetResult 时,它会尝试写入这些随机位置。

于 2013-03-01T12:42:41.483 回答