2013-03-01 4 views
-1

내 컴퓨터가 trinamic steprockerboard와 통신 할 수있게하기 위해 C++로 programm를 작성하려고합니다. 그들은 예제 파일을 제공하여 방해가되지 않도록했습니다. 이것은 완벽하게 작동하지만 정말 단순합니다.USB를 통해 컨트롤러에서 결과 얻기

는 지금은 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 1013 #DEFINE TMCL_SIO 14 # 정의 TMCL_RFS 11 #DEFINE TMCL_RSGP 12 #DEFINE #DEFINE TMCL_STGP TMCL_GIO 15 #define TMCL_SCO 30,515,#DEFINE TMCL_GCO 31 #DEFINE TMCL_CCO 32

//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 // 설정된 대기 전류 ((2백50분의 50) * 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; 
} 
+0

디버거에서 프로그램을 실행하여 충돌을 발견 했습니까? 또한 변수를 검사하여 이들 모두가 정상적으로 보이는지 또는 범인이 될 수있는 null 포인터 (또는 다른 것)가 있는지 확인할 수 있습니다. –

+0

답변 해 주셔서 감사합니다.하지만 이미 문제를 해결했습니다. greeting, Floris – user1876088

답변

1

주소 상태 값은 유효한 변수를 가리켜 야합니다.

UCHAR adr; 
UCHAR stat; 
int val; 

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

은 무엇 지금 일어나고있는 것은 당신의 변수 (ADR, 통계와 val)이 초기화되지 않습니다, 그래서, 그들은 메모리에 임의의 위치를 ​​가리 키 : 그래서, 당신의 코드처럼 보일 것이다. GetResult에 이러한 변수를 전달하면 임의의 위치에 쓰기를 시도합니다.

+0

감사합니다. 빠른 답변을드립니다. 이제 이해가된다! 미안하고 실수 한 실수. – user1876088

관련 문제