C#传感器输入数据模拟命令行接收
作者:互联网
C#传感器输入数据模拟命令行接收
Workstation.dll程序为实现封装好的
首先设置textbox,并在设置栏中将背景设置为黑色,字体设置为白色,尽可能还原命令行。
botton4为显示按钮
botton5为退出按钮
// An highlighted block
using sun.management;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using System.Runtime.InteropServices;
using System.Net.Sockets;
using System.Net;
using System.IO;
namespace WindowsFormsApptest
{
public partial class console : Form
{
[DllImport("Workstation.dll", EntryPoint = "getIP",
CallingConvention = CallingConvention.Cdecl)]
public unsafe static extern IntPtr getIP();
[DllImport("Workstation.dll", EntryPoint = "initSensors",
CallingConvention = CallingConvention.Cdecl)]
public static extern int initSensors();
[DllImport("Workstation.dll", EntryPoint = "getCodedDataPackage_ModeNormal",
CallingConvention = CallingConvention.Cdecl)]
public unsafe static extern IntPtr getCodedDataPackage_ModeNormal(int sensorID);
[DllImport("Workstation.dll", EntryPoint = "decodeDataPackage_ModeNormal",
CallingConvention = CallingConvention.Cdecl)]
public unsafe static extern int* decodeDataPackage_ModeNorma(string codeDataPackage);
static int num2;
public console(int num)
{
InitializeComponent();
num2 = num;
}
unsafe public static int[] receive()
{
int[] m = new int[1018];
int* recDecodeBuff; //定义解码数据缓冲区
IntPtr recCodedBuff = getCodedDataPackage_ModeNormal(num2);
string strun = Marshal.PtrToStringAnsi(recCodedBuff);
recDecodeBuff = decodeDataPackage_ModeNorma(strun);
for (int i = 0; i < 1018; i++)
{
if (recDecodeBuff != null)
{ m[i] = recDecodeBuff[i]; }
else
{ m = null; }
}
return m;
}
private void button4_Click(object sender, EventArgs e)
{
int[] sensor = new int[1018];
sensor = receive();
if (sensor == null)
{
switch (num2)
{
case 1:
{
MessageBox.Show(" 温度传感器数据异常 ", " 警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
}
break;
case 2:
{
MessageBox.Show(" 湿度传感器数据异常 ", " 警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
}
break;
case 3:
{
MessageBox.Show(" PM2.5传感器数据异常 ", " 警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
}
break;
case 4:
{
MessageBox.Show(" 光照传感器数据异常 ", " 警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
}
break;
case 5:
{
MessageBox.Show(" CO传感器数据异常 ", " 警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
}
break;
default:
{
MessageBox.Show(" 请选择传感器类型 ", " 警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
}
break;
}
}
else
{
this.textBox1.Text = this.textBox1.Text + "ask for protocol successfully!" + "\r\n";
this.textBox1.Text = this.textBox1.Text + "creat socket successfully!" + "\r\n";
this.textBox1.Text = this.textBox1.Text + "connect successfully!" + "\r\n";
for (int i = 0; i < sensor.Length - 1; i++)
{
this.textBox1.Text = this.textBox1.Text + sensor[i] + ',';
}
this.textBox1.Text = "\r\n" + this.textBox1.Text + "请按任意键继续...";
}
}
private void button5_Click(object sender, EventArgs e)
{
System.Environment.Exit(0);
}
}
}
使用效果如图所示
标签:命令行,textBox1,C#,Text,System,int,CallingConvention,传感器,using 来源: https://blog.csdn.net/weixin_45754712/article/details/106795406