I am currently developing a plotter based on the K8097. Everything works fine so far but I have one problem with the .net dll. When I send a run command to the motor I need to wait a certain amount of time until the driver card (K8097) would give back “GetMoving” as true. If I choose this amount to small the plotter gets a new moving command before the actual command is completet and I loose steps. If I choose the amount to big everything needs to long.
I do understand absolutely that you can only check the driver in a certain fixed time period and so on. But for me it would be much more convenient to return true for the “GetMoving” function after calling “Move” for the same motor, until you get a new correct feedback of the K8097. Am I wrong? Is there a posibility to change your dll? Can I get your sources somewhere or a command list to write my own communication protocoll?
GetMoving will only return true if the K8097 has reported that the motor is indeed moving. The dll does not assume it is moving by simply returning true, it must first receive feedback from the K8097. This is probably why it is not yet true immediately after calling Move.
If you want careful timing, you will need to write your own library that handles the status packets. This will allow you to react to them asynchronously to start a new movement immediately after the motor stopped. That way you would have a smooth plotter that does not lose any time.
thank you very much for your reply. I do understand very well, that the GetMoving function only returns true, after getting the explicit feedback of the motor. But I think your logic is simply wrong. You know, that the motor is definitely moving, after calling the Move-Function. Giving back the old value from before calling move is simply not quite correct, because you definitely know that the motor is moving. You told him to. If the motor than sends back a “not moving”, you know that the motor already stopped. Wouldn’t that be much more convenient?
Thank you very much for the code, I will try to write my own classes. Can you please tell me what language your code is written in?
[quote]Giving back the old value from before calling move is simply not quite correct, because you definitely know that the motor is moving. You told him to.[/quote]In fact you are still only assuming that the motor is moving, for various reasons this may not be the case.
The code provided is in Object Pascal. C# is a good choice because the newer versions of .NET supply a SerialPort component that will save you a lot of work.
I do have code that seems as it would work for my case. But unfortunately I am not able to communicate with the K8097. I do receive datapackages and I do look for the starting in the package and I find the end. The only part that is missing right now is to calculate the correct checksum. I am obviously not able to convert the code to c#
The original code is this:
[code]function CalculateChecksum(Data: Pointer; Size: Integer): Byte;
var
i: Integer;
n: Byte;
begin
n := 0;
for i:=0 to Size-1 do
begin
n := n + PByte(Longint(Data) + i)^; <–
end;
Result := (1 + (not ($FF and n)));
end;
[/code]
The line with the <-- creeps me out. If I understand it correctely, it takes the 4 byte inputarray and converts it to a Int32 value. This value is added to “i”. Of the resulting value just the first bit is used and added to n. Having done this for every byte n is added bitwise to 0xff, inverted and summed with 1. This is the returnvalue, right?
if (BitConverter.IsLittleEndian)
Array.Reverse(list);
Int32 data = BitConverter.ToInt32(list, 0);
for (int i = 0; i < list.Length; i++)
{
byte[] bytes = BitConverter.GetBytes((data + i));
if (BitConverter.IsLittleEndian)
Array.Reverse(bytes);
n += bytes[0];
}
return (byte)(1 + ~(0xff & n));
}[/code]
If I am not completely wrong, this code does exactly what I discribed above. Unfortunately the checksum that is calculated does never match the given value of the k8097. So there must be an error somewhere.
If you have an array of bytes representing a packet, you can try to use this C# class, I haven’t tested it yet but it should be correct:
Be careful not to include too many bytes in the checksum calculation (that’s what the size argument is for)
[code]#region “Imports”
using System;
using System.Collections.Generic;
using System.Text;
#endregion
namespace Velleman.Util
{
///
/// The checksum class contains various checksum algorithms.
///
public class Checksum
{
///
/// Calculates the 2’s complement checksum of a specified buffer.
///
/// The buffer.
/// Size of the buffer.
/// Returns the 2’s complement checksum.
public static byte Crc8(byte[] data, int size)
{
byte checksum = 0;
for (int i=0; i<=size; i++)
checksum += data[i];