110,533
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Globalization;
using YD_EMS;
using System.Net.Sockets;
using System.Net;
using System.Text;
// 实时数据回传 回调函数
public delegate int RDC_VarOk(
IntPtr handle,
string lpszDbVarName,
string lpszVal,
string lpszdateTime,
int dwUserData //
);
namespace YD_EMS
{
public partial class testApi : System.Web.UI.Page
{
IntPtr m_handle;//句柄
string strIP = "127.0.0.1"; //访问IP
int nPort = 8109; //端口
protected void Page_Load(object sender, EventArgs e)
{
}
private int GetData(IntPtr handle,
string lpszDbVarName,
string lpszVal,
string lpszdateTime,
int dwUserData)
{
if (lpszDbVarName == "d.test1")
test1.Text = lpszVal;
if (lpszDbVarName == "d.test2")
test2.Text = lpszVal;
if (lpszDbVarName == "d.test3")
test3.Text = lpszVal;
return 0;
}
protected void Timer1_Tick(object sender, EventArgs e)
{
//此段拿来测试页面刷新的,可以忽略。
string str = DateTime.Now.ToString("tt") == "上午" ? "AM" : "PM";
lblTime.Text = "当前时间为:" + DateTime.Now.ToString("yyyy年MM月dd日") + " " +
CultureInfo.GetCultureInfo("zh-cn").DateTimeFormat.GetDayName(DateTime.Now.DayOfWeek) + " " +
DateTime.Now.ToLongTimeString() + " " + str;
}
protected void Button1_Click(object sender, EventArgs e)
{
RDC_VarOk myCallBack = new RDC_VarOk(GetData);
m_handle = App_Code.RDCAPI.RDC_Open(strIP, nPort, myCallBack, 0);// 打开数据连接
App_Code.RDCAPI.RDC_AddVar(m_handle, "d,test1", 2);//增加标签
App_Code.RDCAPI.RDC_AddVar(m_handle, "d,test2", 2);//增加标签
App_Code.RDCAPI.RDC_AddVar(m_handle, "d,test3", 2);//增加标签
int n = App_Code.RDCAPI.RDC_StartRun(m_handle);
}
//**********************
// 连接
//**********************
private static Socket ConnectSocket(string server, int port)
{
Socket socket = null;
IPHostEntry iphostentry = null;
foreach (IPAddress address in iphostentry.AddressList)
{
IPEndPoint IPEPoint = new IPEndPoint(address, port);
Socket newSocket = new Socket(IPEPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
newSocket.Connect(IPEPoint);
if (newSocket.Connected)
{
socket = newSocket;
break;
}
else
{
continue;
}
}
return socket;
}
//**********************
// 发送与接收
//**********************
private static string SocketSendReceive(string server, int port)
{
string request = "get var name";
Byte[] btSend = Encoding.ASCII.GetBytes(request);
Byte[] btReceived = new Byte[256];
Socket socket = ConnectSocket(server, port);
if (socket == null)
return ("连接失败");
socket.Send(btSend, btSend.Length, 0);
int intContent = 0;
string strContent = "";
do
{
intContent = socket.Receive(btReceived, btReceived.Length, 0);
strContent += Encoding.ASCII.GetString(btReceived, 0, intContent);
}
while (intContent > 0);
return strContent;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.Runtime.InteropServices;
using System.IO;
// 实时数据回传 回调函数
public delegate int RDC_VarOk(
IntPtr handle,
string lpszDbVarName,
string lpszVal,
string lpszdateTime,
int dwUserData //
);
namespace WindowsFormsApplication6
{
public partial class Form1 : Form
{
IntPtr m_handle;
string strIP = "127.0.0.1";
int nPort = 8109;
public Form1()
{
InitializeComponent();
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
WindowsFormsApplication6.dll.RDCAPI.RDC_StopRun(m_handle);
WindowsFormsApplication6.dll.RDCAPI.RDC_Close(m_handle);
}
private void button6_Click(object sender, EventArgs e)//点击开始自动更新数据
{
RDC_VarOk myCallBack = new RDC_VarOk(GetData);
m_handle = WindowsFormsApplication6.dll.RDCAPI.RDC_Open(strIP, nPort, myCallBack, 0);
WindowsFormsApplication6.dll.RDCAPI.RDC_AddVar(m_handle, "d.test1", 2);
WindowsFormsApplication6.dll.RDCAPI.RDC_AddVar(m_handle, "d.test2", 2);
WindowsFormsApplication6.dll.RDCAPI.RDC_AddVar(m_handle, "d.test3", 2);
int n = WindowsFormsApplication6.dll.RDCAPI.RDC_StartRun(m_handle);
}
private int GetData(IntPtr handle,
string lpszDbVarName,
string lpszVal,
string lpszdateTime,
int dwUserData)
{
if (lpszDbVarName == "d.test1")
textBox1.Text = lpszVal;
if (lpszDbVarName == "d.test2")
textBox2.Text = lpszVal;
if (lpszDbVarName == "d.test3")
textBox3.Text = lpszVal;
return 0;
}
}
}
//**********************
// 连接
//**********************
private static Socket ConnectSocket(string server, int port)
{
Socket socket = null;
IPHostEntry iphostentry = null;
foreach (IPAddress address in iphostentry.AddressList)
{
IPEndPoint IPEPoint = new IPEndPoint(address,port);
Socket newSocket = new Socket(IPEPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
newSocket.Connect(IPEPoint);
if (newSocket.Connected)
{
socket = newSocket;
break;
}
else
{
continue;
}
}
return socket;
}
//**********************
// 发送与接收
//**********************
private static string SocketSendReceive(string server,int port)
{
string request="get var name";
Byte[] btSend=Encoding.ASCII.GetBytes(request);
Byte[] btReceived =new Byte[256];
Socket socket=ConnectSocket(server,port);
if(socket==null)
return ("连接失败");
socket.Send(btSend,btSend.Length,0);
int intContent=0;
string strContent="";
do
{
intContent =socket.Receive(btReceived,btReceived.Length,0);
strContent+=Encoding.ASCII.GetString(btReceived,0,intContent);
}
while(intContent>0);
return strContent;
}
}
}
以上的在winform的项目里面运行至正常的