Device-LabJack

 view release on metacpan or  search on metacpan

linux-labjack/liblabjack/ljackul.c  view on Meta::CPAN

		return errorcode;
	}

	//Fill sendBuffer with the proper values.
	sendBuffer[6]=80;
	sendBuffer[7]=(unsigned char)(address/256);
	sendBuffer[8]=(unsigned char)(address%256);

	//Write & Read data to/from the LabJack
	
	errorcode = WriteRead(localID,READ_TIMEOUT_MS,sendBuffer,readBuffer);
	if(errorcode)
	{
	CloseAll(localID);
	return errorcode;
	}

	//Parse the response
	if(readBuffer[1] != 80)
	{
		errorcode=RESPONSE_ERROR_LJ;
		CloseAll(localID);
		return errorcode;
	}
	if(sendBuffer[7] != readBuffer[7])
	{
		errorcode=ECHO_ERROR_LJ;
		CloseAll(localID);
		return errorcode;
	}
	if(sendBuffer[8] != readBuffer[8])
	{
		errorcode=ECHO_ERROR_LJ;
		CloseAll(localID);
		return errorcode;
	}

	*data3 = readBuffer[2];
	*data2 = readBuffer[3];
	*data1 = readBuffer[4];
	*data0 = readBuffer[5];
	
	errorcode = CloseAll(localID);

	return errorcode;
}


//======================================================================
// WriteMem: Writes 4 bytes to the LabJack's nonvolatile memory at a
//			 specified address.  The data is read back and verified
//			 after the write.  Memory 0-511 is used for configuration
//			 and calibration data.  Memory from 512-1023 is unused by the
//			 the LabJack and available for the user (this corresponds to
//			 starting addresses from 512-1020).  Memory 1024-8191 is
//			 used as a data buffer in hardware timed AI modes.
//
//	Returns:	LabJack errorcodes or 0 for no error (I32).
//	Inputs:		*idnum		-Local ID, Serial Number, or -1 for first
//							 found (I32).
//				unlocked	-If >0, addresses 0-511 are unlocked for
//							 writing (I32).
//				address		-Starting address for writing 0-8188 (I32).
//				data3		-Byte for address (I32).
//				data2		-Byte for address+1 (I32).
//				data1		-Byte for address+2 (I32).
//				data0		-Byte for address+3 (I32).
//	Outputs:	*idnum		-Returns the Local ID or -1 if no LabJack is
//							 found (I32).
//
//	Time:		20 ms
//----------------------------------------------------------------------
long WriteMem(long *idnum,
			  long unlocked,
			  long address,
			  long data3,
			  long data2,
			  long data1,
			  long data0)
{
	long errorcode;
	long localID;
	unsigned char sendBuffer[9]={0,0,0,0,0,0,0,0,0};
	unsigned char readBuffer[9]={0,0,0,0,0,0,0,0,0};
	long calData[20]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
	long serialnum=0;

	//Make sure inputs are valid
	if(unlocked)
	{
		if ((address<0) || (address>8188))
		{
			errorcode=ILLEGAL_INPUT_ERROR_LJ;
			*idnum=-1;
			return errorcode;
		}
	}
	else
	{
		if ((address<512) || (address>8188))
		{
			errorcode=ILLEGAL_INPUT_ERROR_LJ;
			*idnum=-1;
			return errorcode;
		}
	}
	if ((data3<0) || (data3>255))
	{
		errorcode=ILLEGAL_INPUT_ERROR_LJ;
		*idnum=-1;
		return errorcode;
	}
	if ((data2<0) || (data2>255))
	{
		errorcode=ILLEGAL_INPUT_ERROR_LJ;
		*idnum=-1;
		return errorcode;
	}
	if ((data1<0) || (data1>255))
	{
		errorcode=ILLEGAL_INPUT_ERROR_LJ;
		*idnum=-1;
		return errorcode;
	}
	if ((data0<0) || (data0>255))
	{
		errorcode=ILLEGAL_INPUT_ERROR_LJ;
		*idnum=-1;
		return errorcode;
	}

	//Open the specified or first found LabJack
	localID = OpenLabJack(&errorcode,LABJACK_VENDOR_ID,LABJACK_U12_PRODUCT_ID,idnum,&serialnum,calData);
	if(errorcode)
	{
		*idnum=-1;
		return errorcode;
	}

	//Fill sendBuffer with the proper values.
	sendBuffer[6]=81;
	sendBuffer[7]=(unsigned char)(address/256);
	sendBuffer[8]=(unsigned char)(address%256);
	sendBuffer[1]=((unsigned char)data3);
	sendBuffer[2]=((unsigned char)data2);
	sendBuffer[3]=((unsigned char)data1);
	sendBuffer[4]=((unsigned char)data0);

	//Write & Read data to/from the LabJack



( run in 1.318 second using v1.01-cache-2.11-cpan-39bf76dae61 )