手里有个项目需要labview与C#使用http进行通讯,简单写了个demo,C#是个半吊子水平,记录一下过程方便以后查阅
1.labview webservice的创建和编写
有关使用labview创建webservice的方法与实例网上有很多,这里主要介绍Post,Get方法的创建和使用,如果是之前没有相关的通讯经验,推荐简单了解一下基本概念再进行程序编写
HTTP 教程 | 菜鸟教程HTTP 教程 HTTP 协议一般指 HTTP(超文本传输协议)。 超文本传输协议(英语:HyperText Transfer Protocol,缩写:HTTP)是一种用于分布式、协作式和超媒体信息系统的应用层协议,是因特网上应用最为广泛的一种网络传输协议,所有的 WWW 文件都必须遵守这个标准。 HTTP 是为 Web 浏览器与 Web 服务器之间的通信而设计的,但也可以用于其他目的。 HTTP 是一个基于 TCP/IP 通信协议..https://www.runoob.com/http/http-tutorial.html这边创建两个方法,一个Post一个Get,实现一个简单的加减乘除计算器
在web资源新建vi后,在右键菜单里选择这个vi是什么方法
Get方法只是简单的把当前Post的数据返回,这里使用json字符串进行传递,传递的结构如下
需要注意两点,首先生成exe之后,存在于webservice中方法vi里的全局变量好像失效了(需要的朋友们自己再验证一下,我这里只是简单的测试),所以使用了功能型全局变量,也就是类似labview中自带的值改变函数(2017以及之后才有),使用while循环里移位寄存器进行存储
这是labview自带的值改变函数
这是我自己写的功能型全局变量
下面是Post方法,读取传入的ison字符解析后进行计算,然后返回
这样,post,get方法就创建好了,其中一些东西需要在webservice右键属性进行修改
这里同样可以修改vi的方法,以及url的构成方式
这个界面如果选择ison,会把输出的response在用json转换一层,所以我还是用了简单的方法,自己生成json,直接输出text,下面的数据流选项如果不勾选,需要在方法vi的接线板上连接response,才能有response的输出
至此,labview上web服务器的创建就已经完成,右键发布再右键开始即可进行其他客户端的代码调试
2.labview客户端编写
先贴界面
后面板截图如下,简单的使用封装vi进行通讯,就不详细说明了
3.C#客户端的编写
界面与labview客户端基本一致
MainWindow代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using Newtonsoft.Json;
namespace HttpTest
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void windowload(object sender, RoutedEventArgs e)
{
string[] op = new string[] {"Add", "Subtract", "Multiply", "Divide" };
ipaddress.Text = "127.0.0.1";
port.Text = "8080";
timeout.Text = "2000";
A.Text = "";
B.Text = "";
operation.ItemsSource = op;
operation.Text = op[0];
rev.Text = "";
}
private HttpTest.http.httpser webser = new http.httpser();
class httpjsonstruct
{
public string? a { get; set; }
public string? b { get; set; }
public string? operation { get; set; }
}
private void post_Click(object sender, RoutedEventArgs e)//Response=333.00
{
httpjsonstruct ob = new httpjsonstruct() { a = A.Text, b = B.Text, operation = operation.Text };
string jsonString = JsonConvert.SerializeObject(ob, new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore});
int time = Convert.ToInt32(timeout.Text);
rev.Text = webser.HttpPost("http://"+ipaddress.Text+ ":"+port.Text+ "/BasicOperation/Update_Operand", jsonString, time);//http://127.0.0.1:8001/BasicOperation/Update_Operand
}
private void get_Click(object sender, RoutedEventArgs e)//Response={"a":"12","b":"24","operation":"Add"}
{
int time = Convert.ToInt32(timeout.Text);
string getstring = webser.HttpGet("http://" + ipaddress.Text + ":" + port.Text + "/BasicOperation/Get_Content", time);
getstring = getstring.Substring(getstring.IndexOf('=')+1);
httpjsonstruct? getcontent = JsonConvert.DeserializeObject(getstring, typeof(httpjsonstruct)) as httpjsonstruct;
rev.Text = "a=" + getcontent?.a+"\r\n"+"b=" + getcontent?.b+ "\r\n"+"operation=" + getcontent?.operation+ "\r\n";
}
}
}
添加了一个自定义的类,用来写http的post和get方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Net;
using System.IO;
namespace HttpTest
{
public static class http
{
public static string receive = "";
public class httpser
{
public string HttpPost(string Url, string postDataStr, int timeout)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.Timeout = timeout;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
Encoding encoding = Encoding.UTF8;
byte[] postData = encoding.GetBytes(postDataStr);
request.ContentLength = postData.Length;
Stream HttpRequestStream = request.GetRequestStream();
HttpRequestStream.Write(postData, 0, postData.Length);
HttpRequestStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream HttpResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(HttpResponseStream, encoding);
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
HttpResponseStream.Close();
return retString;
}
public string HttpGet(string Url, int timeout)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.Timeout = timeout;
request.Method = "GET";
request.ContentType = "text/html;charset=UTF-8";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream HttpResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(HttpResponseStream, Encoding.GetEncoding("utf-8"));
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
HttpResponseStream.Close();
return retString;
}
}
}
}
全部完成后,最终实现的效果如下
源代码地址