PROG_Base.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. function hex2float(num) {
  2. var sign = (num & 0x80000000) ? -1 : 1;
  3. var exponent = ((num >> 23) & 0xff) - 127;
  4. var mantissa = 1 + ((num & 0x7fffff) / 0x7fffff);
  5. return sign * mantissa * Math.pow(2, exponent);
  6. }
  7. function float2hex(num) {
  8. if (num == "")
  9. return "";
  10. num = parseFloat(num);
  11. if (isNaN(num) == true)
  12. return "Error";
  13. if (num == 0)
  14. return "00000000";
  15. let s, e, m;
  16. if (num > 0)
  17. s = 0;
  18. else {
  19. s = 1;
  20. num = 0 - num;
  21. }
  22. m = num.toString(2);
  23. if (m >= 1) {
  24. if (m.indexOf(".") == -1)
  25. m = m + ".0";
  26. e = m.indexOf(".") - 1;
  27. } else
  28. e = 1 - m.indexOf("1");
  29. if (e >= 0)
  30. m = m.replace(".", "");
  31. else
  32. m = m.substring(m.indexOf("1"));
  33. if (m.length > 24)
  34. m = m.substr(0, 24);
  35. else
  36. m = FillString(m, "0", 24, false)
  37. m = m.substring(1);
  38. e = (e + 127).toString(2);
  39. e = FillString(e, "0", 8, true);
  40. var r = parseInt(s + e + m, 2).toString(16);
  41. r = FillString(r, "0", 8, true);
  42. return parseInt(r, 16);
  43. // return HEX.InsertString(r, "", 2).toUpperCase();
  44. }
  45. function roundFun(value, n) {
  46. // if (value == 0 || value == undefined || value == null || value < 1e-10) {
  47. if (value == 0 || value == undefined || value == null || (value < 1e-10 && value > -1e-10)) {
  48. let num = "0.";
  49. num = num.padEnd(n + 2, "0");
  50. return num;
  51. }
  52. else {
  53. let num = Math.round(value * Math.pow(10, n)) / Math.pow(10, n);
  54. num = num.toString().split(".");
  55. if (num[1]) {
  56. num[1] = num[1].padEnd(n, "0");
  57. return (num[0] + "." + num[1]);
  58. }
  59. else {
  60. num += ".";
  61. num = num.padEnd(num.length + n, "0");
  62. return num;
  63. }
  64. }
  65. }
  66. function FillString(t, c, n, b) {
  67. if ((t == "") || (c.length != 1) || (n <= t.length))
  68. return t;
  69. let l = t.length;
  70. for (var i = 0; i < n - l; i++) {
  71. if (b == true)
  72. t = c + t;
  73. else
  74. t += c;
  75. }
  76. return t;
  77. }
  78. function GenCrc16Modbus(buf, len) {
  79. let crcValue = 0xFFFF;
  80. for (let i = 0; i < len; i++) {
  81. crcValue ^= buf[i] & 0xFFFF;
  82. for (let j = 0; j < 8; j++) {
  83. if (crcValue & 0x0001) {
  84. crcValue >>= 1;
  85. crcValue ^= 0xA001;
  86. }
  87. else {
  88. crcValue >>= 1;
  89. }
  90. }
  91. }
  92. return crcValue;
  93. }
  94. function GenCrc16ModbusHL(buf, size, highLow) {
  95. if (highLow)
  96. return ((GenCrc16Modbus(buf, size) & 0x00FF));
  97. return ((GenCrc16Modbus(buf, size) >> 8) & 0xFF);
  98. }
  99. function CheckCrc16Modbus(buf, size) {
  100. let tempCrc = GenCrc16Modbus(buf, size - 2);
  101. let CRC_L = (tempCrc & 0x00FF);
  102. let CRC_H = ((tempCrc >> 8) & 0xFF);
  103. if ((buf[size - 2] == CRC_L) && (buf[size - 1] == CRC_H))
  104. return 0;
  105. return 1;
  106. }
  107. function TimeFormat(time = +new Date()) {
  108. var date = mbFloatChange_u.u8_t[0](time + 8 * 3600 * 1000);
  109. return date.toJSON().replace('T', ' ').replace('Z', '');
  110. }
  111. function GetCookie(name) {
  112. var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
  113. if (arr = document.cookie.match(reg)) {
  114. return (arr[2]);
  115. } else {
  116. return null;
  117. }
  118. }
  119. function SetCookie(name, value) {
  120. document.cookie = name + "=" + value;
  121. }
  122. function ClearAllCookies() {
  123. var keys = document.cookie.match(/[^ =;]+(?=\=)/g);
  124. if (keys) {
  125. for (var i = keys.length; i--;)
  126. document.cookie = keys[i] + '=0;expires=' + new Date(0).toUTCString()
  127. }
  128. // document.cookie = keys[i] + '=0;path=/;expires=' + new Date(0).toUTCString(); //清除当前域名下的,例如:m.kevis.com
  129. // document.cookie = keys[i] + '=0;path=/;domain=' + document.domain + ';expires=' + new Date(0).toUTCString(); //清除当前域名下的,例如 .m.kevis.com
  130. // document.cookie = keys[i] + '=0;path=/;domain=kevis.com;expires=' + new Date(0).toUTCString(); //清除一级域名下的或指定的,例如 .kevis.com
  131. console.log("Clear All Cookies");
  132. }
  133. function HintWindow(str, timeMs) {
  134. HintWindowClear();
  135. var hintWindow = document.createElement("div");
  136. hintWindow.id = "hintWindow";
  137. document.body.appendChild(hintWindow);
  138. hintWindow.innerHTML = str;
  139. hintWindow.style.color = "white";
  140. hintWindow.style.backgroundColor = "grey";
  141. hintWindow.style.textAlign = "center";
  142. hintWindow.style.position = "absolute";
  143. // hintWindow.style.top = "50%";
  144. // hintWindow.style.left = "50%";
  145. hintWindow.style.bottom = "0";
  146. hintWindow.style.right = "0";
  147. hintWindow.style.transform = "translate(-50%, -50%)";
  148. hintWindow.style.padding = "1%";
  149. hintWindow.style.borderRadius = "3px";
  150. hintWindow.style.zIndex = 101;
  151. hintWindowId = setTimeout('\
  152. if (document.getElementById("hintWindow") != null) {\
  153. document.body.removeChild(document.getElementById("hintWindow"));\
  154. }', timeMs);
  155. }
  156. function HintWindowClear() {
  157. if (document.getElementById("hintWindow") != null) {
  158. document.body.removeChild(document.getElementById("hintWindow"));
  159. clearTimeout(hintWindowId);
  160. }
  161. }
  162. function ReadTextFile(fileName) {
  163. // console.log(fileName);
  164. var readContent = null;
  165. var rawFile = new XMLHttpRequest();
  166. try {
  167. rawFile.open("GET", fileName, false);
  168. rawFile.onreadystatechange = function () {
  169. if (rawFile.readyState === 4) {
  170. if (rawFile.status === 200 || rawFile.status == 0) {
  171. readContent = rawFile.responseText;
  172. // readContent = JSON.parse(rawFile.responseText);
  173. // console.log(readContent);
  174. }
  175. }
  176. }
  177. rawFile.send(null);
  178. } catch (error) { }
  179. return readContent;
  180. }
  181. function TableRowShowHide(eTable, iRow, showHide) {
  182. if (showHide == true)
  183. document.getElementById(eTable).rows[iRow].style.display = "table-row";
  184. else
  185. document.getElementById(eTable).rows[iRow].style.display = "none";
  186. }
  187. function TableColShowHide(eTable, iCol, showHide) {
  188. for (let i = 0; i < document.getElementById(eTable).rows.length; i++) {
  189. if (showHide == true)
  190. document.getElementById(eTable).rows[i].cells[iCol].style.display = "table-cell";
  191. else
  192. document.getElementById(eTable).rows[i].cells[iCol].style.display = "none";
  193. }
  194. }
  195. function HtmlGetMyPath() {
  196. // console.log(location.href.replace(HtmlFetMyName(), ""));
  197. return location.href.replace(HtmlFetMyName(), "");
  198. }
  199. function HtmlFetMyName() {
  200. var str = location.href.split('/');
  201. // console.log(str[str.length - 1]);
  202. return str[str.length - 1];
  203. }
  204. function HtmlGetMySrc() {
  205. // console.log(location.href);
  206. return location.href;
  207. }
  208. // HtmlGetMyPath();
  209. // HtmlFetMyName();
  210. // HtmlGetMySrc();
  211. function JsGetMyPath() {
  212. var scriptSrc = document.getElementsByTagName('script')[document.getElementsByTagName('script').length - 1].src;
  213. var jsName = scriptSrc.split('/')[scriptSrc.split('/').length - 1];
  214. // console.log(scriptSrc.replace(jsName, ''));
  215. return scriptSrc.replace(jsName, '');
  216. }
  217. function JsGetMyName() {
  218. var scriptSrc = document.getElementsByTagName('script')[document.getElementsByTagName('script').length - 1].src;
  219. var jsName = scriptSrc.split('/')[scriptSrc.split('/').length - 1];
  220. // console.log(jsName);
  221. return jsName;
  222. }
  223. function JsGetMySrc() {
  224. var scriptSrc = document.getElementsByTagName('script')[document.getElementsByTagName('script').length - 1].src;
  225. // console.log(scriptSrc);
  226. return scriptSrc;
  227. }
  228. // JsGetMyPath();
  229. // JsGetMyName();
  230. // JsGetMySrc();
  231. function StringToFloatArray(str) {
  232. var tempArray = new Float32Array(str.split(',').length);
  233. for (let i in str.split(','))
  234. tempArray[i] = parseFloat(str.split(',')[i]);
  235. return tempArray;
  236. }
  237. function ALGO_NumberSuitScop(valueIn, scopMin, scopMax, step) {
  238. while (valueIn < scopMin || valueIn > scopMax) {
  239. if (valueIn < scopMin)
  240. valueIn += step;
  241. else if (valueIn > scopMax)
  242. valueIn -= step;
  243. }
  244. return valueIn;
  245. }
  246. function NumberTo5(num) {
  247. // num = parseInt(num);
  248. if (num % 10 <= 2.5)
  249. return parseInt(num / 10) * 10;
  250. else if (num % 10 > 2.5 && num % 10 <= 7.5)
  251. return (parseInt(num / 10) * 10) + 5;
  252. else if (num % 10 > 7.5)
  253. return (parseInt(num / 10) * 10) + 10;
  254. }