Click here to Skip to main content
15,918,889 members
Home / Discussions / C#
   

C#

 
GeneralRe: Threads and Windows Forms Pin
kikeman13-Apr-09 11:10
kikeman13-Apr-09 11:10 
QuestionArray of Structs overwrite problem Pin
Bruce Coward13-Apr-09 5:41
Bruce Coward13-Apr-09 5:41 
AnswerRe: Array of Structs overwrite problem Pin
Luc 64801113-Apr-09 6:06
Luc 64801113-Apr-09 6:06 
GeneralRe: Array of Structs overwrite problem Pin
Bruce Coward13-Apr-09 6:24
Bruce Coward13-Apr-09 6:24 
GeneralRe: Array of Structs overwrite problem Pin
Luc 64801113-Apr-09 7:17
Luc 64801113-Apr-09 7:17 
AnswerRe: Array of Structs overwrite problem Pin
Gideon Engelberth13-Apr-09 8:51
Gideon Engelberth13-Apr-09 8:51 
GeneralRe: Array of Structs overwrite problem Pin
Luc 64801113-Apr-09 9:01
Luc 64801113-Apr-09 9:01 
AnswerRe: Array of Structs overwrite problem Pin
Alan N13-Apr-09 9:49
Alan N13-Apr-09 9:49 
Hi Bruce,
I find a circular buffer much easier to understand and manage with a control variable storing the current data amount. With this additional piece of information in place the buffer full/empty state is obvious and the empty slot can be discarded.

Not tested but something like this:

class CAN {
  // Members
  public struct CANTXSTRUCT {
    public int PGN;
    public byte DLC;
    public byte[] TxData;
  }

  private const int CANArraySize = 20;
  // MODIFIED initial value
  static private int CANFront = 0;
  static private int CANRear = 0;
  // NEW control variable
  static private int currentCount = 0;

  // Create static private circular array of CAN Tx messages
  // MODIFIED
  static private CANTXSTRUCT[] arCANTx = new CANTXSTRUCT[CANArraySize];

  // Methods
  //*********************************************************************
  // static public void QueueCANTxMsg()
  // This method is called to place a CAN Tx message on the rear of the
  // arCANTx circular array.
  //*********************************************************************
  static public void QueueCANTxMsg(int PGN, byte DLC, byte[] TxData) {
    // Just warn and return if arCANTx message array is full

    // MODIFIED
    if (currentCount < arCANTx.Length) {
      // Modulus Increment rear pointer
      CANRear = (CANRear + 1) % arCANTx.Length;

      // Store data in array
      // ...

      // NEW update contents counter
      currentCount++;
    } else {
      // you screwed up!
    }
  }

  static public void RemoveMsg() {
    if (currentCount > 0) {
      // Remove data here
      // ...

      // Modulus increment front pointer
      CANFront = (CANFront + 1) % arCANTx.Length;
      // update current contents counter
      currentCount--;
    } else {
      // you screwed up!
    }
  }
}


Alan.
GeneralRe: Array of Structs overwrite problem Pin
Luc 64801113-Apr-09 10:29
Luc 64801113-Apr-09 10:29 
QuestionCross-thread error. Pin
Fired.Fish.Gmail13-Apr-09 4:58
Fired.Fish.Gmail13-Apr-09 4:58 
AnswerRe: Cross-thread error. Pin
Colin Angus Mackay13-Apr-09 5:06
Colin Angus Mackay13-Apr-09 5:06 
GeneralRe: Cross-thread error. Pin
Fired.Fish.Gmail13-Apr-09 5:20
Fired.Fish.Gmail13-Apr-09 5:20 
GeneralRe: Cross-thread error. Pin
Henry Minute13-Apr-09 5:22
Henry Minute13-Apr-09 5:22 
AnswerRe: Cross-thread error. Pin
Luc 64801113-Apr-09 5:39
Luc 64801113-Apr-09 5:39 
Questionstruct or class? Pin
Jammer13-Apr-09 4:27
Jammer13-Apr-09 4:27 
AnswerRe: struct or class? Pin
DaveyM6913-Apr-09 4:34
professionalDaveyM6913-Apr-09 4:34 
GeneralRe: struct or class? Pin
Colin Angus Mackay13-Apr-09 4:38
Colin Angus Mackay13-Apr-09 4:38 
AnswerRe: struct or class? Pin
PIEBALDconsult13-Apr-09 5:14
mvePIEBALDconsult13-Apr-09 5:14 
GeneralRe: struct or class? Pin
Colin Angus Mackay13-Apr-09 5:22
Colin Angus Mackay13-Apr-09 5:22 
QuestionSql Backup Pin
nagendra.vk13-Apr-09 4:24
nagendra.vk13-Apr-09 4:24 
AnswerRe: Sql Backup Pin
Colin Angus Mackay13-Apr-09 4:43
Colin Angus Mackay13-Apr-09 4:43 
QuestionHandling Exceptions from another thread Pin
Fayu13-Apr-09 4:02
Fayu13-Apr-09 4:02 
AnswerRe: Handling Exceptions from another thread Pin
Fayu13-Apr-09 6:17
Fayu13-Apr-09 6:17 
GeneralRe: Handling Exceptions from another thread Pin
Luc 64801113-Apr-09 7:22
Luc 64801113-Apr-09 7:22 
Questionmaster window Pin
EmZan13-Apr-09 3:20
EmZan13-Apr-09 3:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.