Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过usb故障从控制器获取结果 我试图用C++编写一个程序,让我的计算机与一个三层的程序板通信。 他们已经提供了一个示例文件,让您可以开始。这很完美,但实际上过于简单化了_C++_Controller - Fatal编程技术网

通过usb故障从控制器获取结果 我试图用C++编写一个程序,让我的计算机与一个三层的程序板通信。 他们已经提供了一个示例文件,让您可以开始。这很完美,但实际上过于简单化了

通过usb故障从控制器获取结果 我试图用C++编写一个程序,让我的计算机与一个三层的程序板通信。 他们已经提供了一个示例文件,让您可以开始。这很完美,但实际上过于简单化了,c++,controller,C++,Controller,现在我想从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 //

现在我想从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);
但当我运行程序,并尝试此选项时,程序会崩溃

有人知道是什么导致了这个问题吗?(我提供的代码只是我程序的一部分,整个代码可以在下面找到。 我无意让您阅读整个代码,问题应该在上面,但仅针对感兴趣的人,我还将其余部分放在这里)

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

printf(“VPI测试设置\n\n”);
做
{
printf(“1-顺时针旋转(10圈)\n”);
printf(“2-逆时针旋转(10圈)\n”);
printf(“3-停止电机”);
printf(“4-开始测试(先顺时针旋转100次,\n然后逆时针旋转100次)\n”);
printf(“\n99-End\n”);
scanf(“%d”、&i);
开关(一)
{
案例1:
SendCmd(RS232Handle,1,TMCL_MVP,0,0,32000);//ABS(第四个参数)=0
打破
案例2:
SendCmd(RS232Handle,1,TMCL_MVP,0,0,-32000);//ABS(第四个参数)=0
打破
案例3:
SendCmd(RS232Handle,1,TMCL_MST,0,0,0);
打破
案例4:
//SendCmd(RS232Handle,1,TMCL_RFS,0,0,0);
printf(“测试已开始\n”);
//UCHAR done;
UCHAR*adr;
乌查尔*统计;
int*val;
//SendCmd(RS232Handle,1,TMCL_SAP,193,1,2);//SAP 193,1,2
SendCmd(RS232Handle,1,TMCL_MVP,0,0,-3200);//ABS(第四个参数)=0
SendCmd(RS232Handle,1,TMCL_间隙,8,0,0);
获取结果(RS232Handle、adr、stat、val);
printf(“来自USB设备的结果:adr=%d,stat=%d,val=%d\n”,adr,stat,val);
//CHAR GetResult(句柄句柄、UCHAR*地址、UCHAR*状态、int*值)
//如果(完成!=2)
// {
//printf(“向后旋转开始\n”);
//SendCmd(RS232Handle,1,TMCL_MVP,0,0,3200);
// }
// }
打破
}
SendCmd(RS232手柄,1,TMCL控制,0,电机,速度);
SendCmd(RS232手柄,1,TMCL控制,0,电机,速度);
如果(i==1 | | i==2 | | i==3 | | i==4)
{
地址=0;
状态=0;
数值=0;
超时=GetTickCount();

而(GetResult(RS232Handle,&Address,&Status,&Value)==TMCL\u RESULT\u NOT\u READY&&abs(GetTickCount()-Timeout)地址、状态和值应指向有效变量。因此,您的代码应类似于:

UCHAR adr;
UCHAR stat;
int val;

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

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

您是否尝试在调试器中运行程序以找出崩溃的位置?然后您还可以检查变量,看看它们是否都正常,或者是否有任何空指针(或其他事情)那可能是罪魁祸首。谢谢你的回复,但我已经解决了问题。你好,花匠。非常感谢你的快速回复!我现在明白了!对不起,草率的错误。
//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
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;
}
UCHAR adr;
UCHAR stat;
int val;

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