wiki:Docs/Prog/Manual/ApplicationLibraries/lib825ev/Weight/StartRepeatMulti

StartRepeatMulti

Start repeating communications from main board to report weights, I/O status, COM3, COM4 and optionally input counters.

int StartRepeatMulti(uint32 interval, struct mnbd_multi_rep_counters_struct& counters);
int StartRepeatMulti(uint32 interval, struct mnbd_multi_rep_counters_struct* counters = NULL);

Parameters

  • interval - update interval in milliseconds
  • counters - structure to define any DIO counters to be reported

Return Value

  • Returns the result - OK success

Remarks

The main board or option card must already be opened by calling OpenMnBd first. The call does a wait for acknowledgement. If the application is using main board communication events these events may be called during the wait for acknowledgement. COM3 (and COM4 on new style 2012 main board) receive data is also reported.

An application should never perform weight or I/O requests such as calling MnBdProcess while repeat multi is enabled.

The specified MultiRcv function is called with the current weight and I/O status repeatedly at the interval specified when StartRepeatMulti was called. Should an application perform other initialization such as MnBdSetWIM the MultiRcv function will be called when SWIM information is available. It is important in this case to check the repeat_multi_record global variable. This will be zero when the weight and I/O information is available, this will be 0x45 when SWIM data is available.

Examples

if(StartRepeatMulti(100) == OK)
    DisplayText(0, 0, "Wt and I/O 10 times per second");
int bd = 1;
int inp = 8;

SetDIOCounter(bd, inp, dioSetCount, 0, WAIT_ACK);
SetDIOCounter(bd, inp, dioSetPrescaler, 1, WAIT_ACK);
SetDIOCounter(bd, inp, dioSetCountUp, 0, WAIT_ACK);
SetDIOCounter(bd, inp, dioSetCountLowToHigh, 0, WAIT_ACK);
SetDIOCounter(bd, inp, dioEnableCounter, 0, WAIT_ACK);

struct mnbd_multi_rep_counters_struct counters;
memset(&counters, 0, sizeof(counters));

counters.cnt1_bd = bd;
counters.cnt1_input = inp;

if(StartRepeatMulti(100, counters) == OK)
    DisplayText(0, 0, "Wt, I/O, cnt 10 times per second");

// Counter input may be read in MNBD_EVENT(MultiRcv) - printf("cnt1 %d\r\n", g_multi_counters.cnt1);

Example of reading counter data:

int g_nPrevCnt[4] = { 0, 0, 0, 0 }
int current_cycle_counts = 0;

MNBD_EVENT(MultiRcv)
{
   int n;

   if(repeat_multi_record == 0)
   {
      for(n = 0; n < GetNumScales(); n++)
      {
          // Update scale weight display
      }

      if(g_multi_counters.cnt1 != g_nPrevCnt[0])
      {
          current_cycle_counts += (g_multi_counters.cnt1 - g_nPrevCnt[0]);
          
          g_nPrevCnt[0] = g_multi_counters.cnt1;
      }
   }
}


Here is an example of a complete sample application:

repeatmulti.h:

#ifndef REPEATMULTI_H_
#define REPEATMULTI_H_

EVENT(MainScreenShow);

BACKGROUND_EVENT(Comm);

MNBD_EVENT(MultiRcv);
EVENT(Exit);

int MnBdSerialDataRcv(int nPort, int nLen, unsigned char* data);


#endif /* REPEATMULTI_H_ */

repeatmulti.cpp:

#include "cardinal825.h"

#include "repeatmulti.h"

int main()
{
	InitLCD();

	SetCurColor(COLOR_ATTENTION);
	DisplayText(0, 0, "Repeat Multi Example");

	ReadTouchCal();
	OpenBeeper();
	OpenTouch();

	MnBdStartup();

	MnBdSetEvent(MNBD_REQ_REP_MULTI, MnBdEventMultiRcv);
        
        // Set the comm event to our MnBdSerialDataRcv function to receive serial port data from COM3 and COM4
        // this is unnecessary if using CCommSerial communications.
        MnBdSetCommEvent(MnBdSerialDataRcv);

	SetBackgroundEvent(BackgroundEventComm);

	if(StartRepeatMulti(100) != OK)
	{
		DisplayText(0, FONT_HEIGHT, "Error starting repeat multi");
		return 0;
	}

	FORM_INIT(Main, EventMainScreenShow, NULL, NULL, NULL, nFormFlgNone);

	FORM_ADD_BUTTON(Main, Exit, 0, FONT_HEIGHT * 12, " Exit ", 'X', EventExit, FORM_BUTTON_INVERT);

	FORM_SHOW(Main);
	FORM_RUN(Main);
	FORM_HIDE(Main);

        MnBdClearCommEvent();

	StopRepeatMulti();
	SetBackgroundEvent(NULL);

	MnBdShutdown();

	return 0;
}

EVENT(MainScreenShow)
{
	ClearLCD();
	SetCurColor(COLOR_ATTENTION);
	DisplayText(0, 0, "Repeat Multi Example");

	ResetPrevGrossWt();
	return 0;
}

EVENT(Exit)
{
	return 1; // return non-zero to exit form
}

MNBD_EVENT(MultiRcv)
{
	int n;
	for(n = 1; n <= GetNumScales(); n++)
	{
		if(IsGrossWtChanged(n))
		{
			DisplayText(0, (FONT_HEIGHT * n) + FONT_HEIGHT, GetChangedGrossWt(n));
		}
	}
	return 0;
}

BACKGROUND_EVENT(Comm)
{
	// Use MnBdRead directly to prevent overhead of MnBdProcess
	// MnBdRead will call event MultiRcv that we registered previously when scale and DIO status is received

	int n;
	// in case we have more I/O cards than scales change this
	for(n = 0; n <= GetNumScales(); n++)
	{
		// 0 = MNBD_DEV, 1 .. Scales and I/O cards
		MnBdRead(n);
	}

	return 0;
}

// The following is unnecessary for COM3, COM4 if using CCommSerial communications.

struct mnbd_serial_struct
{
   char buffer[100];
   int pos;
} mnbd_serial[2] = { { "", 0 }, { "", 0 } };

// Called automatically during MnBdRead if serial data is read
int MnBdSerialDataRcv(int nPort, int nLen, unsigned char* data)
{
     int n;
     for(n = 0; n < nLen; n++)
     {
          if(data[n] == '\r')
          {
               mnbd_serial[nPort].buffer[mnbd_serial[nPort].pos] = '\0'; // Terminate the string
               printf("rcv com %d [%s]\r\n", nPort + 3, mnbd_serial[nPort].buffer);
               mnbd_serial[nPort].pos = 0;
          }
          else if(mnbd_serial[nPort].pos < 99) // prevent buffer overflow
          {
              mnbd_serial[nPort].buffer[mnbd_serial[nPort].pos++] = data[n];
          } 
     }
     return 0;
}

See Also

Last modified 22 months ago Last modified on 07/18/22 08:49:37
Note: See TracWiki for help on using the wiki.