K8097 - Reading issue C++

Hi all.

I am working on a gui based app for the K8097 card permiting the user to define a changable set of commands. The gui app is written in Visual Studio 2013 in c++. The Move function looks as follows:

void Move(int motor, int steps, int direction, int speed, System::IO::Ports::SerialPort^  serialPort)
      {
             //forming the message sent to the motor driver
             array<unsigned char, 1> ^WriteBuffer = { 0xFF, 0x1C, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };
             //steps
             WriteBuffer[2 + motor * 6] = steps & 0xFF;
             WriteBuffer[3 + motor * 6] = (steps >> 8) & 0xFF;
             WriteBuffer[4 + motor * 6] = (steps >> 16) & 0xFF;
             WriteBuffer[5 + motor * 6] = (steps >> 24) & 0xFF;
             //speed
             WriteBuffer[6 + motor * 6] = speed & 0xFF;
             //direction
             WriteBuffer[7 + motor * 6] = direction & 0xFF;

             //message checksum
             unsigned char checksum = 0x1C;
             for (int i = 0; i<6; i++)
             {
                checksum += WriteBuffer[2 + motor * 6 + i];
             }
             checksum = (~checksum) + 1;
             WriteBuffer[26] = checksum;
// sending the message
             serialPort->Write(WriteBuffer,0,WriteBuffer->Length);

             array<System::Byte, 1> ^ReadBuffer = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
             Sleep(100);
             int BytesRead = 7;
             int remainder = 1;

             while (remainder == 1))
             {
                Sleep(50);
                
                BytesRead = serialPort->Read(ReadBuffer, 0, ReadBuffer->Length);
                remainder = (ReadBuffer[2] >> motor) % 2;

             }
}

Yet, I have a problem that after the first reading I get the ReadBuffer equal to:

ReadBuffer ={0xFF, 0x7, 0x0, 0x0, 0xBF, 0x3A, 0x0}

So my question is why do i get a 0x00 in the third byte of status command while the motor is running?

[quote=“Maxiim”]

// sending the message
             serialPort->Write(WriteBuffer,0,WriteBuffer->Length);
           
             array<System::Byte, 1> ^ReadBuffer = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
             Sleep(100);

So my question is why do i get a 0x00 in the third byte of status command while the motor is running?[/quote]

Hello,

You are reading right after the write. That’s to fast. You have to wait for the new packet to arrive.
Packets are transmitted at fixed intervals. This way, if your motor is moving a limited number of steps the board may even report that the motor is running while it has stopped already.

I have the same problem and have learned not to rely too much on the board to tell me that a motor is running. Currently I am building a CNC machine and have the motors running as little as 12 steps at a time. I have a timer running that will start the movement of the motor. The interval is at 100ms and this will start the motor. It will not start the next movement for 100ms or if the motor is still running at that time.

The first movement, I assume that the motor is running because I told it to move, the second time you can check this via your readbuffer. For me it works better this way.
There is more to read on this forum about this subject if you do a search on K8097.

Greetz,

Deekay

I have considered the time needed to process the command and sending the feedback by the motor card. That is why I wait 100 ms (the Sleep(100) command) after sending the move command (and to be precise I initialy wait 150ms) before reading the buffer. I tried to prolong the pause up to 250 ms and verified in debug mode that the card sends a zero in the status command. What is surprising I experience a different situation in my console app built in eclipse cdt, where I usualy get the proper status of the motor reading the buffer. And this is very confusing… What is more disturbing I do not get a 100% repeatability in the card behaviour in either of apps. The motor sometimes turns quicker than it is suppose to. But lets leave it at that. Primarly, I would like to know why I am receiving z 0 in the status command despite waiting 250ms before reading and the motor running.