⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 phsensor-simple.c

📁 基于phidget标准C程序样例
💻 C
字号:
// - PHSensor simple -
// This simple example simply opens a PH sensor phidget and waits for one to be attached.  The program will then wait for
// user input to terminate.  While waiting it will display the data generated by the events, such as the PH change event
// which will display the currently measured PH without sensitivity modifications.  For a more detailed example where
// sensitivity can be adjusted and all the functionalities of a PH sensor are demonstrated, see PHSensor-full 
// example.
// 
// Please note that this example was designed to work with only one Phidget PHSensor connected. For an example using multiple 
// Phidget PHSensors, please see a "multiple" example in the PHSensor Examples folder.
//
// Copyright 2007 Phidgets Inc.  All rights reserved.


#include <stdio.h>
#include <phidget21.h>

int AttachHandler(CPhidgetHandle PH, void *userptr)
{
	int serialNo;
	const char *name;

	CPhidget_getDeviceName (PH, &name);
	CPhidget_getSerialNumber(PH, &serialNo);
	printf("%s %10d attached!\n", name, serialNo);

	return 0;
}

int DetachHandler(CPhidgetHandle PH, void *userptr)
{
	int serialNo;
	const char *name;

	CPhidget_getDeviceName (PH, &name);
	CPhidget_getSerialNumber(PH, &serialNo);
	printf("%s %10d detached!\n", name, serialNo);

	return 0;
}

int ErrorHandler(CPhidgetHandle PH, void *userptr, int ErrorCode, const char *Description)
{
	printf("Error handled. %i - %s\n", ErrorCode, Description);
	return 0;
}

int PHChangeHandler(CPhidgetPHSensorHandle PH, void *usrptr, double Value)
{
	double potential;
	CPhidgetPHSensor_getPotential(PH, &potential);

	printf("PH: %f -- Potential: %f\n", Value, potential);
	return 0;
}
//Display the properties of the attached phidget to the screen.  We will be displaying the name, serial number and version of the attached device.
int display_properties(CPhidgetPHSensorHandle phid)
{
	int serialNo, version;
	double trigger, potential;
	const char* ptr;

	CPhidget_getDeviceType((CPhidgetHandle)phid, &ptr);
	CPhidget_getSerialNumber((CPhidgetHandle)phid, &serialNo);
	CPhidget_getDeviceVersion((CPhidgetHandle)phid, &version);

	CPhidgetPHSensor_getPHChangeTrigger(phid, &trigger);
	CPhidgetPHSensor_getPotential(phid, &potential);

	printf("%s\n", ptr);
	printf("Serial Number: %10d\nVersion: %8d\n", serialNo, version);
	printf("Trigger Sensitivity: %f\nPotential: %f\n", trigger, potential);

	return 0;
}

int ph_simple()
{
	int result;
	const char *err;

	//Declare an PH Sensor handle
	CPhidgetPHSensorHandle ph = 0;

	//create the PH Sensor object
	CPhidgetPHSensor_create(&ph);

	//Set the handlers to be run when the device is plugged in or opened from software, unplugged or closed from software, or generates an error.
	CPhidget_set_OnAttach_Handler((CPhidgetHandle)ph, AttachHandler, NULL);
	CPhidget_set_OnDetach_Handler((CPhidgetHandle)ph, DetachHandler, NULL);
	CPhidget_set_OnError_Handler((CPhidgetHandle)ph, ErrorHandler, NULL);

	//Registers a callback that will run if the PH changes by more than the PH trigger.
	//Requires the handle for the PHSensor, the function that will be called, and a arbitrary pointer that will be supplied to the callback function (may be NULL).
	CPhidgetPHSensor_set_OnPHChange_Handler(ph, PHChangeHandler, NULL);

	//open the PH Sensor for device connections
	CPhidget_open((CPhidgetHandle)ph, -1);

	//get the program to wait for an PH Sensor device to be attached
	printf("Waiting for PH Sensor to be attached....");
	if((result = CPhidget_waitForAttachment((CPhidgetHandle)ph, 10000)))
	{
		CPhidget_getErrorDescription(result, &err);
		printf("Problem waiting for attachment: %s\n", err);
		return 0;
	}

	//Display the properties of the attached textlcd device
	display_properties(ph);

	//read led event data
	printf("Reading.....\n");

	//increase the sensitivity
	printf("Increasing sensitivity to 10.00, Press any key to continue\n");
	getchar();

	CPhidgetPHSensor_setPHChangeTrigger (ph, 10.00);

	//end
	printf("Press any key to end\n");
	getchar();

	//since user input has been read, this is a signal to terminate the program so we will close the phidget and delete the object we created
	printf("Closing...\n");
	CPhidget_close((CPhidgetHandle)ph);
	CPhidget_delete((CPhidgetHandle)ph);

	//all done, exit
	return 0;
}

int main(int argc, char* argv[])
{
	ph_simple();
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -