using System.Collections.Generic;
public static Dictionary<byte[], int> ComboCommmands = new Dictionary<byte[], int>(){ { new byte[]{ 1, 139 } , 500 }, { new byte[] { 35, 138 }, 501 } , { new byte[] { 137, 126 } , 502 }};
public static void Main()
ComboCommandProgram program = new ComboCommandProgram(1, "P1",
Console.WriteLine("Steps: {0}", program.Steps.Count);
for (int i = 0; i < program.Steps.Count; i++)
Console.WriteLine("{0} {1} {2}", i + 1, program.Steps[i].CommandCode, BitConverter.ToString(program.Steps[i].BytesStep, 0, program.Steps[i].BytesStep.Length));
Console.WriteLine("Bytes Index of Steps: {0} = {1}", 5, program.BytesIndexOfStep(5));
Console.WriteLine("Bytes Index: {0} To Steps = {1}", program.BytesIndexOfStep(5), program.BytesIndexToIndexOfStep(program.BytesIndexOfStep(5)));
public class ComboCommandProgram
public string ProgramName;
private byte[] bytesStep;
public List<ComboCommandProgramStep> Steps;
public ComboCommandProgram()
ProgramNo = 0; ProgramName = string.Empty;
Steps = new List<ComboCommandProgramStep>();
public ComboCommandProgram(int programNo, string programName, byte[] bytesStep, int index)
ProgramNo = programNo; ProgramName = programName;
Refresh(bytesStep, index);
public int BytesIndexOfStep(int indexOfStep)
if (indexOfStep > Steps.Count - 1) return -1;
int bytesIndex = 0; for(int i = 0; i < indexOfStep; i++) bytesIndex += Steps[i].BytesStep.Length;
public int BytesIndexToIndexOfStep(int bytesIndex)
for(int i = 0; i < Steps.Count; i++)
bytesIndex -= Steps[i].BytesStep.Length;
if ( bytesIndex < 0) return i;
public void Copy(ComboCommandProgram program)
ProgramNo = program.ProgramNo;
ProgramName = program.ProgramName;
BytesStep = program.BytesStep;
public ComboCommandProgram Clone()
ComboCommandProgram program = new ComboCommandProgram();
public int InsertAt(int index, ComboCommandProgramStep step)
if (index < 0) return -1;
if (index > Steps.Count - 1)
Steps.Insert(index, step);
refreshBytesStepUponProgramSteps();
public int ReplaceAt(int index, ComboCommandProgramStep step)
if (index < 0 || index > Steps.Count - 1) return -1;
Steps.Insert(index, step);
refreshBytesStepUponProgramSteps();
public int RemoveAt(int index)
if (index < 0 || index > Steps.Count - 1) return -1;
refreshBytesStepUponProgramSteps();
private void refreshBytesStepUponProgramSteps()
for(int i = 0; i < Steps.Count; i++) dataSize += Steps[i].BytesStep.Length;
this.bytesStep = new byte[dataSize];
for(int i = 0; i < Steps.Count; i++)
Buffer.BlockCopy(Steps[i].BytesStep, 0, this.bytesStep, dataSize, Steps[i].BytesStep.Length);
dataSize += Steps[i].BytesStep.Length;
public void Refresh(byte[] bytesStep, int index)
if (!(bytesStep.Length - index > 0))
this.bytesStep = new byte[0];
this.bytesStep = new byte[bytesStep.Length - index];
if(this.bytesStep.Length > 0) Buffer.BlockCopy(bytesStep, index, this.bytesStep, 0, this.bytesStep.Length);
int numSteps = this.bytesStep.Length / 4;
for(int i = 0 ; i < numSteps ; i++)
foreach (KeyValuePair<byte[], int> kvp in ComboCommmands)
for (int b = 0; b < kvp.Key.Length; b++)
if(4* (i + b) > this.bytesStep.Length - 1) break;
if(this.bytesStep[4 * (i + b)] != kvp.Key[b]) break;
if (b == kvp.Key.Length - 1)
Steps.Add(new ComboCommandProgramStep(kvp.Value, this.bytesStep, 4 * i , 4 * kvp.Key.Length));
if(!found) Steps.Add(new ComboCommandProgramStep(this.bytesStep[4 * i], this.bytesStep, 4 * i, 4));
public class ComboCommandProgramStep{
private byte[] bytesStep;
public ComboCommandProgramStep(int commandCode, byte[] bytesStep, int index, int count)
CommandCode = commandCode;
this.bytesStep = new byte[count];
Buffer.BlockCopy(bytesStep, index, this.bytesStep, 0, count);