PROG_Base.js 7.8 KB

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