using System.Runtime.InteropServices;
using System.Configuration;
using System.Globalization;
using System.Data.SqlClient;
using System.Net.Sockets;
string serverProgID = ConfigurationManager.AppSettings["opcID"];
private OpcServer theSrv;
private static float[] currentValues;
private static string responseStringG ="";
private static HttpListener listener = new HttpListener();
private static string consoleOut = ConfigurationManager.AppSettings["consoleOutput"];
private static string answerType = ConfigurationManager.AppSettings["answerType"];
private static string portNumb = ConfigurationManager.AppSettings["portNumber"];
private static int timeref = Int32.Parse(ConfigurationManager.AppSettings["refreshTime"]);
private static string[] tagsNames = ConfigurationManager.AppSettings["tagsNames"].Split(',');
private static string[] ratios = ConfigurationManager.AppSettings["ratios"].Split(',');
private static string sqlSend = ConfigurationManager.AppSettings["sqlSend"];
private static string udpSend = ConfigurationManager.AppSettings["udpSend"];
private static string webSend = ConfigurationManager.AppSettings["webSend"];
private static string table_name = ConfigurationManager.AppSettings["table"];
private static string column_name = ConfigurationManager.AppSettings["column"];
private static int sendtags = Int32.Parse(ConfigurationManager.AppSettings["tags2send"]);
private static IPAddress remoteIPAddress = IPAddress.Parse(ConfigurationManager.AppSettings["remoteIP"]);
private static int remotePort = Convert.ToInt16(ConfigurationManager.AppSettings["remotePort"]);
public static SqlConnection myConn = new SqlConnection(ConfigurationManager.ConnectionStrings["connstr"].ConnectionString);
SqlCommand myCommand = new SqlCommand("Command String", myConn);
theSrv = new OpcServer();
theSrv.Connect(serverProgID);
theGrp = theSrv.AddGroup("OPCCSharp-Group", false, timeref);
string[] tags = ConfigurationManager.AppSettings["tags"].Split(',');
if (sendtags > tags.Length) sendtags = tags.Length;
var itemDefs = new OPCItemDef[tags.Length];
for (var i = 0; i < tags.Length; i++)
itemDefs[i] = new OPCItemDef(tags[i], true, i, VarEnum.VT_EMPTY);
theGrp.AddItems(itemDefs, out rItm);
if (HRESULTS.Failed(rItm[0].Error) || HRESULTS.Failed(rItm[1].Error))
Console.WriteLine("OPC Tester: AddItems - some failed"); theGrp.Remove(true); theSrv.Disconnect(); return;
var handlesSrv = new int[itemDefs.Length];
for (var i = 0; i < itemDefs.Length; i++)
handlesSrv[i] = rItm[i].HandleServer;
currentValues = new Single[itemDefs.Length];
theGrp.DataChanged += new DataChangeEventHandler(this.theGrp_DataChange);
theGrp.ReadCompleted += new ReadCompleteEventHandler(this.theGrp_ReadComplete);
theGrp.Read(handlesSrv, 55667788, out CancelID, out aE);
HttpListenerContext context = listener.GetContext();
HttpListenerRequest request = context.Request;
HttpListenerResponse response = context.Response;
context.Response.AddHeader("Access-Control-Allow-Origin", "*");
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseStringG);
response.ContentLength64 = buffer.Length;
System.IO.Stream output = response.OutputStream;
output.Write(buffer, 0, buffer.Length);
Console.WriteLine("************************************** hit <return> to close...");
theGrp.ReadCompleted -= new ReadCompleteEventHandler(this.theGrp_ReadComplete);
theGrp.RemoveItems(handlesSrv, out aE);
public void theGrp_DataChange(object sender, DataChangeEventArgs e)
foreach (OPCItemState s in e.sts)
if (HRESULTS.Succeeded(s.Error))
Console.WriteLine(" ih={0} v={1} q={2} t={3}", s.HandleClient, s.DataValue, s.Quality, s.TimeStamp);
currentValues[s.HandleClient] = Convert.ToSingle(s.DataValue) * Single.Parse(ratios[s.HandleClient], CultureInfo.InvariantCulture.NumberFormat);
Console.WriteLine(" ih={0} ERROR=0x{1:x} !", s.HandleClient, s.Error);
string responseString = "{";
if (answerType == "table")
responseString = "<HTML><head><meta charset=\"UTF-8\"><meta http-equiv=\"Refresh\" content=\"" + timeref / 1000 + "\"/></head>" +
"<BODY><table border><tr><td>" + string.Join("<br>", tagsNames) + "</td><td >" + string.Join("<br>", currentValues) + "</td></tr></table></BODY></HTML>";
responseStringG = responseString;
for (int i = 0; i < currentValues.Length - 1; i++) responseString = responseString + "\"tag" + i + "\":\"" + currentValues[i] + "\", ";
responseString = responseString + "\"tag" + (currentValues.Length - 1) + "\":\"" + currentValues[currentValues.Length - 1] + "\"}";
responseStringG = responseString;
byte[] byteArray = new byte[sendtags * 4];
Buffer.BlockCopy(currentValues, 0, byteArray, 0, byteArray.Length);
SqlCommand cmd = new SqlCommand("INSERT INTO " + table_name + " (" + column_name + ") values (@bindata)", myConn);
var param = new SqlParameter("@bindata", SqlDbType.Binary)
cmd.Parameters.Add(param);
Console.WriteLine("SQL-exception: " + err.ToString());
if (udpSend == "yes") UDPsend(byteArray);
private static void UDPsend(byte[] datagram)
UdpClient sender = new UdpClient();
IPEndPoint endPoint = new IPEndPoint(remoteIPAddress, remotePort);
sender.Send(datagram, datagram.Length, endPoint);
Console.WriteLine("Возникло исключение: " + ex.ToString() + "\n " + ex.Message);
public void theGrp_ReadComplete(object sender, ReadCompleteEventArgs e)
Console.WriteLine("ReadComplete event: gh={0} id={1} me={2} mq={3}", e.groupHandleClient, e.transactionID, e.masterError, e.masterQuality);
foreach (OPCItemState s in e.sts)
if (HRESULTS.Succeeded(s.Error))
Console.WriteLine(" ih={0} v={1} q={2} t={3}", s.HandleClient, s.DataValue, s.Quality, s.TimeStamp);
Console.WriteLine(" ih={0} ERROR=0x{1:x} !", s.HandleClient, s.Error);
static void Main(string[] args)
string prefix = String.Format("{0}:{1}/", url, port);
listener.Prefixes.Add(prefix);
Tester tst = new Tester();