.NET (C#) sensor poll and callback examples

This commit is contained in:
Stefan Persson 2011-05-19 14:43:00 +00:00
parent c1f3579a72
commit f096faf367
4 changed files with 203 additions and 0 deletions

View file

@ -0,0 +1,26 @@

Microsoft Visual Studio Solution File, Format Version 10.00
# Visual Studio 2008
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SensorPollWin", "SensorPollWin\SensorPollWin.csproj", "{47D37594-BFDD-4A4E-9736-32EDAE2E22F0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{47D37594-BFDD-4A4E-9736-32EDAE2E22F0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{47D37594-BFDD-4A4E-9736-32EDAE2E22F0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{47D37594-BFDD-4A4E-9736-32EDAE2E22F0}.Debug|x86.ActiveCfg = Debug|x86
{47D37594-BFDD-4A4E-9736-32EDAE2E22F0}.Debug|x86.Build.0 = Debug|x86
{47D37594-BFDD-4A4E-9736-32EDAE2E22F0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{47D37594-BFDD-4A4E-9736-32EDAE2E22F0}.Release|Any CPU.Build.0 = Release|Any CPU
{47D37594-BFDD-4A4E-9736-32EDAE2E22F0}.Release|x86.ActiveCfg = Release|x86
{47D37594-BFDD-4A4E-9736-32EDAE2E22F0}.Release|x86.Build.0 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal

View file

@ -0,0 +1,64 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace SensorPollWin
{
unsafe class Program
{
[DllImport("TelldusCore.dll")]
public static extern int tdTurnOn(int deviceId);
[DllImport("TelldusCore.dll")]
public static extern int tdSensor(char* protocol, int protocolLength, char* model, int modelLength, int* id, int* dataTypes);
[DllImport("TelldusCore.dll")]
public static extern int tdSensorValue(char* protocol, char* model, int id, int dataType, char* value, int valueLength, int* timestamp);
static unsafe void Main(string[] args)
{
int protocolstringsize = 20;
int modelstringsize = 20;
int valuestringsize = 20;
char* protocol = (char*)Marshal.AllocHGlobal(Marshal.SystemDefaultCharSize * protocolstringsize);
char* model = (char*)Marshal.AllocHGlobal(Marshal.SystemDefaultCharSize * modelstringsize);
IntPtr id = Marshal.AllocHGlobal(sizeof(int));
IntPtr dataType = Marshal.AllocHGlobal(sizeof(int));
Console.WriteLine("getting sensors");
while (tdSensor(protocol, protocolstringsize, model, modelstringsize, (int*)id, (int*)dataType) == 0)
{
Console.WriteLine("Sensor: " + System.Text.Encoding.UTF8.GetString(System.Text.Encoding.Unicode.GetBytes(new string(protocol))) + " " + System.Text.Encoding.UTF8.GetString(System.Text.Encoding.Unicode.GetBytes(new string(model))));
char* value = (char*)Marshal.AllocHGlobal(Marshal.SystemDefaultCharSize * valuestringsize);
IntPtr timestamp = Marshal.AllocHGlobal(sizeof(int));
if ((Marshal.ReadIntPtr(dataType).ToInt32() & 1) != 0)
{
tdSensorValue(protocol, model, Marshal.ReadIntPtr(id).ToInt32(), 1, value, valuestringsize, (int*)timestamp);
Console.WriteLine("Temperature: " + System.Text.Encoding.UTF8.GetString(System.Text.Encoding.Unicode.GetBytes(new string(value))) + "C, " + datify(Marshal.ReadIntPtr(timestamp).ToInt32()));
}
if ((Marshal.ReadIntPtr(dataType).ToInt32() & 2) != 0)
{
tdSensorValue(protocol, model, Marshal.ReadIntPtr(id).ToInt32(), 2, value, valuestringsize, (int*)timestamp);
Console.WriteLine("Humidity: " + System.Text.Encoding.UTF8.GetString(System.Text.Encoding.Unicode.GetBytes(new string(value))) + "%, " + datify(Marshal.ReadIntPtr(timestamp).ToInt32()));
}
Console.WriteLine("");
Marshal.FreeHGlobal((IntPtr)value);
Marshal.FreeHGlobal(timestamp);
}
Marshal.FreeHGlobal((IntPtr)protocol);
Marshal.FreeHGlobal((IntPtr)model);
Marshal.FreeHGlobal(id);
Marshal.FreeHGlobal(dataType);
}
private static string datify(int timestamp){
System.DateTime dateTime = new System.DateTime(1970, 1, 1, 0, 0, 0, 0);
dateTime = dateTime.AddSeconds(timestamp);
dateTime = dateTime.ToLocalTime();
return dateTime.ToString();
}
}
}

View file

@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("SensorPollWin")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Microsoft")]
[assembly: AssemblyProduct("SensorPollWin")]
[assembly: AssemblyCopyright("Copyright © Microsoft 2011")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("31424c50-2c39-4b7c-a4e8-5a11bd6b8c1d")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View file

@ -0,0 +1,77 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>9.0.30729</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{47D37594-BFDD-4A4E-9736-32EDAE2E22F0}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>SensorPollWin</RootNamespace>
<AssemblyName>SensorPollWin</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x86\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x86</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
<OutputPath>bin\x86\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x86</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Xml.Linq">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Data.DataSetExtensions">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>