fork download
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Data.Common;
  7. using System.Drawing;
  8. using System.Text;
  9. using System.Windows.Forms;
  10. using System.Threading;
  11. using System.Data.SQLite;
  12. using TwinCAT.Ads;
  13.  
  14. namespace SQLiteDataloggerCE
  15. {
  16. public partial class DataLogger : Form
  17. {
  18. // Software version
  19. private const string sSQL_Version = "HCP_2013-02_v1.0";
  20.  
  21. // SQL database connections string
  22. private DbConnection db = new SQLiteConnection(@"Data Source = \Hard Disk2\SQLData.db3");
  23.  
  24. // Create table SQL string
  25. string SQLText =
  26. @"pragma foreign_keys = off;
  27. begin transaction;
  28. pragma auto_vacuum=0;
  29. pragma default_cache_size=2000;
  30. pragma encoding='UTF-8';
  31. pragma page_size=1024;
  32. create table if not exists [tblLogData] (
  33. [ID] INTEGER PRIMARY KEY AUTOINCREMENT,
  34. [DateTime] TIMESTAMP,
  35. [TankNr] INTEGER,
  36. [BatchNr] INTEGER,
  37. [Temperatur] FLOAT,
  38. [Volume] FLOAT,
  39. [ErrorCode] VARCHAR(8),
  40. [Approved] BOOL(1));
  41. commit transaction;
  42. pragma foreign_keys = on;";
  43.  
  44.  
  45. // TwinCAT Ads client
  46. private TcAdsClient AdsClient;
  47. private TcAdsSymbolInfoLoader AdsSymbolInfoLoader;
  48. private TcAdsSymbolInfo SqlDataAdsSymbolInfo;
  49. private int hWrite;
  50. private int hDone;
  51. private int hWatchDog;
  52.  
  53. public DataLogger()
  54. {
  55. InitializeComponent();
  56. }
  57.  
  58. private void Form1_Load(object sender, EventArgs e)
  59. {
  60. try
  61. {
  62. AdsClientOpen();
  63. }
  64. catch (Exception err)
  65. {
  66. MessageBox.Show("Form1_Load() - " + err.Message);
  67. }
  68. }
  69.  
  70. private void Form1_Closing(object sender, CancelEventArgs e)
  71. {
  72. try
  73. {
  74. AdsClientClose();
  75. db.Close();
  76. db.Dispose();
  77. }
  78. catch (Exception err)
  79. {
  80. MessageBox.Show("Form1_FormClosing() - " + err.Message);
  81. }
  82. }
  83.  
  84. private void AdsClientOpen()
  85. {
  86. try
  87. {
  88. AdsClient = new TcAdsClient();
  89. AdsClient.Connect("", 801);
  90.  
  91. AdsSymbolInfoLoader = AdsClient.CreateSymbolInfoLoader();
  92. SqlDataAdsSymbolInfo = AdsSymbolInfoLoader.FindSymbol(("P_Datalogger.stSQLData").ToUpper());
  93. hWatchDog = AdsClient.CreateVariableHandle("P_Datalogger.bWatchDog");
  94. hDone = AdsClient.CreateVariableHandle("P_Datalogger.bDone");
  95.  
  96. AdsClient.AdsNotificationEx += new AdsNotificationExEventHandler(AdsClient_AdsNotificationEx);
  97. hWrite = AdsClient.AddDeviceNotificationEx("P_Datalogger.bWrite", AdsTransMode.OnChange, 10, 0, null, typeof(bool));
  98.  
  99. }
  100. catch
  101. {
  102. // MessageBox.Show("AdsClientOpen() - " + err.Message);
  103. }
  104. }
  105.  
  106. private void AdsClientClose()
  107. {
  108. try
  109. {
  110. // Close db
  111. if (db.State == ConnectionState.Open)
  112. db.Close();
  113.  
  114. AdsClient.DeleteVariableHandle(hWatchDog);
  115. AdsClient.DeleteVariableHandle(hDone);
  116. AdsClient.DeleteDeviceNotification(hWrite);
  117.  
  118. }
  119. catch
  120. {
  121. // MessageBox.Show("AdsClientClose() - " + err.Message);
  122. }
  123. finally
  124. {
  125. AdsClient.Dispose();
  126. }
  127. }
  128.  
  129. private void AdsClient_AdsNotificationEx(object sender, AdsNotificationExEventArgs e)
  130. {
  131. try
  132. {
  133. if (e.NotificationHandle == hWrite)
  134. {
  135. if (Convert.ToBoolean(e.Value) == false)
  136. AdsClient.WriteAny(hDone, Convert.ToBoolean(false));
  137. else
  138. if (SqlWriteData() == true)
  139. AdsClient.WriteAny(hDone, Convert.ToBoolean(true));
  140. }
  141. }
  142. catch (Exception err)
  143. {
  144. MessageBox.Show("AdsClient_AdsNotificationEx() - " + err.Message);
  145. }
  146. }
  147.  
  148. private void CheckIfSQLTableExist()
  149. {
  150. try
  151. {
  152. // Check db state
  153. if (db.State != ConnectionState.Open)
  154. db.Open();
  155.  
  156. using (DbCommand cmd = db.CreateCommand())
  157. {
  158. cmd.CommandText = SQLText;
  159. cmd.ExecuteNonQuery();
  160. cmd.Dispose();
  161. }
  162.  
  163. // Clean up
  164. if (db.State == ConnectionState.Open)
  165. db.Close();
  166.  
  167. if (SqlWriteData() == true)
  168. AdsClient.WriteAny(hDone, Convert.ToBoolean(true));
  169.  
  170. }
  171. catch (Exception err)
  172. {
  173. MessageBox.Show("CheckSQLTable() - " + err.Message);
  174. }
  175. }
  176.  
  177. private Boolean SqlWriteData()
  178. {
  179. try
  180. {
  181. // Check symbol info
  182. if (SqlDataAdsSymbolInfo != null)
  183. {
  184. AdsStream DataStream = new AdsStream(SqlDataAdsSymbolInfo.Size);
  185. AdsBinaryReader BinReader = new AdsBinaryReader(DataStream);
  186. StringBuilder sb = new StringBuilder("", 100);
  187.  
  188. // Read ADS logData variabels
  189. AdsClient.Read(SqlDataAdsSymbolInfo.IndexGroup, SqlDataAdsSymbolInfo.IndexOffset, DataStream);
  190.  
  191. // Check db state
  192. if (db.State != ConnectionState.Open)
  193. db.Open();
  194.  
  195. if (db.State == ConnectionState.Open)
  196. {
  197. // Write data to SQL file
  198. using (DbTransaction dbTrans = db.BeginTransaction())
  199. {
  200. using (DbCommand cmd = db.CreateCommand())
  201. {
  202. try
  203. {
  204. // Struct values
  205. TcAdsSymbolInfo subSymbol = SqlDataAdsSymbolInfo.FirstSubSymbol;
  206.  
  207. cmd.Transaction = dbTrans;
  208.  
  209. sb.Append(string.Format("INSERT INTO tblLogData VALUES(null,"));
  210.  
  211. if (SqlDataAdsSymbolInfo.SubSymbolCount > 0)
  212. {
  213. for (int i = 1; i <= SqlDataAdsSymbolInfo.SubSymbolCount; i++)
  214. {
  215. sb.Append(string.Format("{0}", GetBinReadValues(subSymbol, BinReader)));
  216. sb.Append(",");
  217. subSymbol = subSymbol.NextSymbol;
  218. }
  219.  
  220. // Remove last ","
  221. sb.Remove(sb.Length - 1, 1);
  222. sb.Append(");");
  223.  
  224. cmd.CommandText = sb.ToString();
  225. cmd.ExecuteNonQuery();
  226.  
  227. dbTrans.Commit();
  228. cmd.Dispose();
  229. }
  230. }
  231. catch
  232. {
  233. dbTrans.Rollback();
  234.  
  235. // Close db
  236. if (db.State == ConnectionState.Open)
  237. db.Close();
  238.  
  239. CheckIfSQLTableExist();
  240. return (false);
  241. }
  242. }
  243. }
  244. }
  245. // Close db
  246. if (db.State == ConnectionState.Open)
  247. db.Close();
  248.  
  249. return (true);
  250. }
  251. else
  252. return (false);
  253. }
  254. catch
  255. {
  256. CheckIfSQLTableExist();
  257. return (false);
  258. }
  259. }
  260.  
  261. private string GetBinReadValues(TcAdsSymbolInfo AdsSymbolInfo, AdsBinaryReader BinReader)
  262. {
  263. try
  264. {
  265. switch (AdsSymbolInfo.Datatype)
  266. {
  267. case AdsDatatypeId.ADST_BIT: return BinReader.ReadBoolean().ToString();
  268. case AdsDatatypeId.ADST_INT16: return BinReader.ReadInt16().ToString();
  269. case AdsDatatypeId.ADST_INT32: return BinReader.ReadInt32().ToString();
  270. case AdsDatatypeId.ADST_INT64: return BinReader.ReadInt64().ToString();
  271. case AdsDatatypeId.ADST_INT8: return BinReader.ReadSByte().ToString();
  272. case AdsDatatypeId.ADST_REAL32: return BinReader.ReadSingle().ToString().Replace(",", ".");
  273. case AdsDatatypeId.ADST_REAL64: return BinReader.ReadDouble().ToString().Replace(",", ".");
  274. case AdsDatatypeId.ADST_STRING: return "'" + BinReader.ReadPlcString(AdsSymbolInfo.Size).ToString() + "'";
  275. case AdsDatatypeId.ADST_UINT16: return BinReader.ReadUInt16().ToString();
  276. case AdsDatatypeId.ADST_UINT32: return BinReader.ReadUInt32().ToString();
  277. case AdsDatatypeId.ADST_UINT64: return BinReader.ReadUInt64().ToString();
  278. case AdsDatatypeId.ADST_UINT8: return BinReader.ReadByte().ToString();
  279. case AdsDatatypeId.ADST_WSTRING: return "'" + BinReader.ReadPlcString(AdsSymbolInfo.Size).ToString() + "'";
  280. default: return "'" + BinReader.ReadPlcString(AdsSymbolInfo.Size).ToString() + "'";
  281. }
  282. }
  283. catch (Exception err)
  284. {
  285. MessageBox.Show("GetBinReadValues() - " + err.Message);
  286. }
  287. return ("");
  288. }
  289.  
  290. private void WatchDog_Tick(object sender, EventArgs e)
  291. {
  292. try
  293. {
  294. // Check watchdog
  295. if ((bool)(AdsClient.ReadAny(hWatchDog, typeof(bool))) == false)
  296. {
  297. AdsClientClose();
  298. AdsClientOpen();
  299. }
  300.  
  301. // Set watchdog false
  302. AdsClient.WriteAny(hWatchDog, Convert.ToBoolean(false));
  303. }
  304. catch
  305. {
  306. AdsClientClose();
  307. AdsClientOpen();
  308. }
  309.  
  310. }
  311.  
  312. }
  313. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cs(5,14): error CS0234: The type or namespace name `Data' does not exist in the namespace `System'. Are you missing an assembly reference?
prog.cs(6,14): error CS0234: The type or namespace name `Data' does not exist in the namespace `System'. Are you missing an assembly reference?
prog.cs(9,14): error CS0234: The type or namespace name `Windows' does not exist in the namespace `System'. Are you missing an assembly reference?
prog.cs(11,14): error CS0234: The type or namespace name `Data' does not exist in the namespace `System'. Are you missing an assembly reference?
prog.cs(12,7): error CS0246: The type or namespace name `TwinCAT' could not be found. Are you missing a using directive or an assembly reference?
prog.cs(22,17): error CS0246: The type or namespace name `DbConnection' could not be found. Are you missing a using directive or an assembly reference?
prog.cs(46,17): error CS0246: The type or namespace name `TcAdsClient' could not be found. Are you missing a using directive or an assembly reference?
prog.cs(47,17): error CS0246: The type or namespace name `TcAdsSymbolInfoLoader' could not be found. Are you missing a using directive or an assembly reference?
prog.cs(48,17): error CS0246: The type or namespace name `TcAdsSymbolInfo' could not be found. Are you missing a using directive or an assembly reference?
prog.cs(129,65): error CS0246: The type or namespace name `AdsNotificationExEventArgs' could not be found. Are you missing a using directive or an assembly reference?
prog.cs(261,41): error CS0246: The type or namespace name `TcAdsSymbolInfo' could not be found. Are you missing a using directive or an assembly reference?
prog.cs(261,72): error CS0246: The type or namespace name `AdsBinaryReader' could not be found. Are you missing a using directive or an assembly reference?
Compilation failed: 12 error(s), 0 warnings
stdout
Standard output is empty