USB Pololu 16 servo controler C++ programm

Hello :slight_smile:
at the beginning i would like to thank for your answers guys!
I red this ----> https://forum.pololu.com/t/problem-with-c-access-to-ssc8/1172/1 topic. It put some light on my project.
I found this program:

   HANDLE comPort = 0;
   DCB comSettings = {0};
   TCHAR *portName = TEXT("COM3");
   comPort = CreateFile(portName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
   
   if(comPort == INVALID_HANDLE_VALUE) {
      cerr << "Error opening COM port!" << endl;
      return 1;
   }

   if(GetCommState(comPort, &comSettings)) {
      comSettings.BaudRate = CBR_19200; //9600;
      if(!SetCommState(comPort, &comSettings)) {
         cerr << "ERROR SETCOMMSTATE!!!!" << endl;
      };
   }

   COMMTIMEOUTS timeouts={0};
   timeouts.ReadIntervalTimeout=1000;
   timeouts.ReadTotalTimeoutConstant=1000;
   timeouts.ReadTotalTimeoutMultiplier=1000;
   timeouts.WriteTotalTimeoutConstant=1000;
   timeouts.WriteTotalTimeoutMultiplier=1000;

   if(!SetCommTimeouts(comPort,&timeouts)){
      cerr << "Timeout Setting Error\n";
      return 1;
   }

   unsigned short int buffer[6];
   DWORD bytesWritten;

   /*buffer[0] = 255;
   buffer[1] = 0;
   buffer[2] = 25;*/

   unsigned short int temp;
   unsigned char pos_hi,pos_low;

   int angle = 3500;

   temp=angle&0x1f80;
   pos_hi=temp>>7;
   pos_low=angle & 0x7f;

   buffer[0]=0x80;//start byte
   buffer[1]=0x01;//device id
   buffer[2]=0x04;//command number
   buffer[3]=0x07;//servo number
   buffer[4]=pos_hi;//data1
   buffer[5]=pos_low;//data2

   BOOL teste = false;

   teste = WriteFile(comPort, &buffer, 6, &bytesWritten, NULL);
   FlushFileBuffers(comPort);

   if(teste)
      cout << "OK!!!" << endl;

   CloseHandle(comPort);[/code]

Correct me if i am wrong:
1) This part of program is responsible for communication with servo controler.
[code]HANDLE comPort = 0;
   DCB comSettings = {0};
   TCHAR *portName = TEXT("COM3");
   comPort = CreateFile(portName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
   
   if(comPort == INVALID_HANDLE_VALUE) {
      cerr << "Error opening COM port!" << endl;
      return 1;
   }

   if(GetCommState(comPort, &comSettings)) {
      comSettings.BaudRate = CBR_19200; //9600;
      if(!SetCommState(comPort, &comSettings)) {
         cerr << "ERROR SETCOMMSTATE!!!!" << endl;
      };
   }

   COMMTIMEOUTS timeouts={0};
   timeouts.ReadIntervalTimeout=1000;
   timeouts.ReadTotalTimeoutConstant=1000;
   timeouts.ReadTotalTimeoutMultiplier=1000;
   timeouts.WriteTotalTimeoutConstant=1000;
   timeouts.WriteTotalTimeoutMultiplier=1000;

   if(!SetCommTimeouts(comPort,&timeouts)){
      cerr << "Timeout Setting Error\n";
      return 1;
   }
  1. This part of program is responsible for sending bytes to the servo controller.
   unsigned short int buffer[6];
   DWORD bytesWritten;

   /*buffer[0] = 255;
   buffer[1] = 0;
   buffer[2] = 25;*/

   unsigned short int temp;
   unsigned char pos_hi,pos_low;

   int angle = 3500;

   temp=angle&0x1f80;
   pos_hi=temp>>7;
   pos_low=angle & 0x7f;

   buffer[0]=0x80;//start byte
   buffer[1]=0x01;//device id
   buffer[2]=0x04;//command number
   buffer[3]=0x07;//servo number
   buffer[4]=pos_hi;//data1
   buffer[5]=pos_low;//data2

2.1) I have problem with understanding this part:

   int angle = 3500;

   temp=angle&0x1f80;
   pos_hi=temp>>7;
   pos_low=angle & 0x7f;

3500 is assigned to the angle variable, then … temp=angle&0x1f80 (do not understand it, what is the result of this operation? ), then we move temp seven bites to the right, and another operation i have problem with understanding (pos_low=angle & 0x7f).

  1. This part tests if the data was sent correctly.
   BOOL teste = false;

   teste = WriteFile(comPort, &buffer, 6, &bytesWritten, NULL);
   FlushFileBuffers(comPort);

   if(teste)
      cout << "OK!!!" << endl;

   CloseHandle(comPort);

Thank you for your help… :slight_smile: