1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Net; 6 using System.IO; 7 using System.Timers; 8 using System.Data; 9 using Newtonsoft; 10 11 namespace Tlink_Send 12 { 13 class Program 14 { 15 public static string HttpPost(string Url, string postDataStr) 16 { 17 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url); 18 request.Method = "POST"; 19 request.ContentType = "application/x-www-form-urlencoded"; 20 request.ContentLength = Encoding.UTF8.GetByteCount(postDataStr); 21 Stream myRequestStream = request.GetRequestStream(); 22 StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312")); 23 myStreamWriter.Write(postDataStr); 24 myStreamWriter.Close(); 25 26 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 27 Stream myResponseStream = response.GetResponseStream(); 28 StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8")); 29 string retString = myStreamReader.ReadToEnd(); 30 myStreamReader.Close(); 31 myResponseStream.Close(); 32 33 return retString; 34 } 35 36 static void Main(string[] args) 37 { 38 Console.WriteLine("江苏xx电气公司OPC-Tlink转发工具"); 39 //Console.ReadKey(); 40 System.Timers.Timer maxTimer = new System.Timers.Timer(); 41 maxTimer.Elapsed += new ElapsedEventHandler(Tmr_Elapsed); 42 // 设置引发时间的时间间隔 此处设置为1秒(1000毫秒) 43 maxTimer.Interval = 1000; 44 maxTimer.Enabled = true; 45 46 47 //Console.WriteLine(album.ToString()); 48 System.Threading.Thread.Sleep(1000); 49 Console.ReadKey(); 50 51 } 52 53 static void Tmr_Elapsed(object sender, ElapsedEventArgs e) 54 { 55 Newtonsoft.Json.Linq.JObject jsonObject = new Newtonsoft.Json.Linq.JObject(); 56 57 jsonObject.Add("deviceNo", "这里替换成你自己的设备编号"); 58 59 dynamic album = jsonObject; 60 61 album.sensorDatas = new Newtonsoft.Json.Linq.JArray() as dynamic; 62 63 dynamic song = new Newtonsoft.Json.Linq.JObject(); 64 song.sensorsId = 20000; 65 song.value = (System.DateTime.Now.Millisecond-100).ToString(); 66 Console.WriteLine("江苏xx电气公司OPC-Tlink转发工具"); 67 Console.WriteLine("传感器1的采集值为"+ song.value); 68 album.sensorDatas.Add(song); 69 70 song = new Newtonsoft.Json.Linq.JObject(); 71 song.sensorsId = 20001; 72 song.value = (System.DateTime.Now.Millisecond+324).ToString(); 73 Console.WriteLine("江苏xx电气公司OPC-Tlink转发工具"); 74 Console.WriteLine("传感器2的采集值为" + song.value); 75 album.sensorDatas.Add(song); 76 77 song = new Newtonsoft.Json.Linq.JObject(); 78 song.sensorsId = 200052; 79 song.value = (System.DateTime.Now.Millisecond-666).ToString(); 80 Console.WriteLine("江苏xx电气公司OPC-Tlink转发工具"); 81 Console.WriteLine("传感器3的采集值为" + song.value); 82 album.sensorDatas.Add(song); 83 84 //Console.WriteLine(album.ToString()); 85 HttpPost("http://api.tlink.io/tlink_interface/api/device/createDataPonit.htm", album.ToString()); 86 Console.ReadKey(); 87 } 88 } 89 }