wiki:Docs/Prog/Manual/DeviceSupport/Weighing

Version 7 (modified by Don Wilson, 14 years ago) ( diff )

--

Weighing

The 825 OS provides access to weight data by means of the mnbd-comm device driver. This device driver provides device files: /dev/wt1 - /dev/wt10 corresponding to scale 1 - scale 10. Device file /dev/wttot is used for total weight if the totalizer feature is enabled.

Get Weight

Command prompt testing

Make sure no applications are running or the weight server. Otherwise, the application or weight server may grab the response before you are able to retrieve it from the command prompt.

From the 825 command prompt type:

echo "W" > /dev/wt1
cat /dev/wt1
WA01004456A486000000004456A4860000

The response "WA" indicates the response for the "W" weight request is acknowledged "A". The following characters are hexadecimal encoded values for the floating point gross, tare, net weights and weight status information. Because this data represents floating point math values it is difficult to interpret. You may change the weight on the scale and repeat the test to see that the values change.

Access the weight data from a program

The lib825ev library provides features to facilitate access the weight information from an application program.

// Simple get weight sample

#include <stdio.h>
#include <stdlib.h>

#include "mnbdcomm.h"
#include "lcd.h"
#include "util.h"

int main()
{
   int n;
   char gross_wt_str[41];
   CSleep slp(0, 5);

   InitLCD();
   MnBdStartup();

   while(1)
   {
      n = MnBdProcess();
      if(n == mnbdProcWtRcv)
      {
          FormatGrossWt(GetProcessWt(), gross_wt_str);
          DisplayText(0, 0, gross_wt_str);
      }
      slp.Pause();
   }
   return 0;
}

This displays the gross weight at the upper left corner of the display. The weight will update as changes are made to the scale. This program as an endless loop. If you started the program from the command line without using the "&" parameter you can press CTRL-C to end the program. Otherwise use PS to see the process list and KILL to terminate the appropriate process number.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

#include <string.h>

#include <math.h>
#include <time.h>

#include <signal.h>

#include "lcd.h"

#include "touch.h"
#include "kypdbeep.h"
#include "form.h"
#include "mnbdcomm.h"
#include "login.h"

extern struct mnbd_wt_struct wtdata[MAX_SCALES];

extern uint32 g_nRepWtIntv;

extern int* pwim;

extern int g_keycnt;
extern uint8 g_keybuf[10];

#define ON_THRES	3
#define OFF_THRES	1

int main ()
{
	int i, x, stat, n = 0;
	struct timespec delaytm;
	char szMsg[41];
	time_t tnow;
	struct tm *tmnow;
	int prev_sec = -1;
	
	delaytm.tv_sec = 0;
	delaytm.tv_nsec = 20000000;  // 20 ms

	read_touch_cal();

	OpenBeeper();

	OpenMnBd(0); // Mainboard settings
	
	OpenTouch();
	
	InitLCD();

	read_current_operator();

	OpenMnBd(0);
	
	OpenMnBd(TOT_SCALE);

	
	printf("Calling SetWIM %d\r\n", TOT_SCALE);
	
	SetWIM(TOT_SCALE, ON_THRES, OFF_THRES, 200);

	printf("after Calling SetWIM\r\n");
	
	g_nRepWtIntv = REP_INTV_SAMPLE_RATE; 
	MnBdRequest(REQ_SCALE(0), MNBD_REQ_REP_WT, NO_WAIT_ACK);

	stat = 0;
	
	time(&tnow);
	tmnow = localtime(&tnow);
	
	while(1)
	{
		ReadKeypad();
		
		if(g_keycnt > 0)
		{
			g_keycnt = 0;
			if(g_keybuf[0] == BKSP || g_keybuf[0] == ESC)
				break;
		}
		
		nanosleep(&delaytm, NULL);

		x = CheckWIM(TOT_SCALE);
		if(x > 1)
		{
			if(stat == 0 && pwim[0] > ON_THRES) // Item on scale
			{
				stat = 1;
				printf("On Scale\r\n");
			}
			
			printf("WIM samples %d\r\n", x);
			for(i = 0; i < x; i += 2)
			{
				printf("%d %.3f %.3f\r\n", i, *(float*)&pwim[i], *(float*)&pwim[i+1]);
				n++;
			}
			sprintf(szMsg, "%10.3f %10.3f", *(float*)&pwim[x - 2], *(float*)&pwim[x - 1]);
			
			LocateLCD(0, 0);
			PrintLCD(szMsg);
			
			time(&tnow);
			tmnow = localtime(&tnow);

			if(tmnow->tm_sec != prev_sec)
			{
				LocateLCD(FONT_WIDTH * 30,0);
				sprintf(szMsg, "%02d %4d", tmnow->tm_sec, n);
				PrintLCD(szMsg);
				
				prev_sec = tmnow->tm_sec;
				n = 0;
			}
		}
		else if(CheckWtRcv(0))
		{
			if(stat != 0)
			{
				if(wtdata[0].grosswt < OFF_THRES)
				{
					printf("Off Scale\r\n");
					
					stat = 0;
				}
			}
		}
	}
	
	g_nRepWtIntv = 0; 
	MnBdRequest(REQ_SCALE(0), MNBD_REQ_REP_WT, NO_WAIT_ACK);
	
	printf("Done\r\n");
	
	return 0;
}


This sample demonstrates high speed weigh in motion using to the total scale to read two scales at a time.

Note: See TracWiki for help on using the wiki.