balanceAlert.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. <template>
  2. <div class="content">
  3. <div class="title" v-if="information.indexOf('毛重') > -1">毛重</div>
  4. <div class="title" v-else>皮重</div>
  5. <div class="contentInfo">
  6. <div class="uncertain">浮动重量</div>
  7. <div class="number" v-if="information.indexOf('毛重') > -1">
  8. {{ grossWeightVal }} kg
  9. </div>
  10. <div class="number" v-else>{{ tareVal }} kg</div>
  11. </div>
  12. <div class="btn" @click="sendVal">确定</div>
  13. </div>
  14. </template>
  15. <script>
  16. export default {
  17. name: 'balanceAlert',
  18. props: ['deptBudgetList', 'information'],
  19. data() {
  20. return {
  21. param: 9600,
  22. grossWeightVal: '',
  23. tareVal: ''
  24. }
  25. },
  26. activated() {},
  27. mounted() {
  28. console.log('11111111111111111111111111111111111')
  29. console.log(this.deptBudgetList)
  30. this.openPort()
  31. },
  32. computed: {},
  33. created() {},
  34. methods: {
  35. sendVal() {
  36. console.log(this.grossWeightVal)
  37. console.log(this.tareVal)
  38. console.log(this.deptBudgetList)
  39. if (this.information.indexOf('毛重') > -1) {
  40. this.$emit('balanceListen', this.grossWeightVal)
  41. } else {
  42. this.$emit('balanceListen', this.tareVal)
  43. }
  44. },
  45. async closePort() {
  46. console.log('closePort')
  47. this.reader.cancel()
  48. },
  49. async openPort() {
  50. console.log('openPort', navigator)
  51. if ('serial' in navigator) {
  52. if (!this.$store.state.app.reader) {
  53. // The Web Serial API is supported.
  54. console.log('the Web Serial API is supported.')
  55. const port = await navigator.serial.requestPort()
  56. if (
  57. this.deptBudgetList.warehouseName == '白城内陆港' ||
  58. this.deptBudgetList.warehouseName == '肇东金信库'||
  59. this.deptBudgetList.warehouseName == '洮南胡力吐粮食储备库'||
  60. this.deptBudgetList.warehouseName == '洮南向阳乡金昊'
  61. ) {
  62. this.param = 1200
  63. } else if (this.deptBudgetList.warehouseName == '鲅鱼圈祥腾库') {
  64. this.param = 2400
  65. } else {
  66. this.param = 9600
  67. }
  68. console.log(this.param)
  69. await port.open({
  70. baudRate: this.param
  71. }) // set baud rate
  72. this.reader = port.readable.getReader()
  73. console.log('port ', port)
  74. this.$store.dispatch('app/setReader', this.reader)
  75. console.log('reader ', this.reader)
  76. } else {
  77. console.log('afterport', this.$store.state.app.reader)
  78. this.reader = this.$store.state.app.reader
  79. }
  80. // 监听来自串行设备的数据
  81. while (true) {
  82. const { value, done } = await this.reader.read()
  83. // console.log('value',value);
  84. if (done) {
  85. // 允许稍后关闭串口。
  86. this.reader.releaseLock()
  87. break
  88. }
  89. var result = ''
  90. //2。获取16进制字符串
  91. // var receData = HexConvert.ByteToString(value);
  92. // console.log("receData",receData);
  93. var flag = false
  94. if (
  95. this.deptBudgetList.warehouseName == '克东润津库'&&
  96. value.length < 9
  97. ) {
  98. continue
  99. }
  100. if (value.length > 2) {
  101. if (value.length <= 6) {
  102. // continue
  103. }
  104. if (
  105. (this.deptBudgetList.warehouseName == '肇东金信库' || this.deptBudgetList.warehouseName == '甘南宏旗库') &&
  106. value.length < 10
  107. ) {
  108. var start = new Date().getTime()
  109. while (new Date().getTime() - start < 100) {}
  110. continue
  111. }
  112. if (
  113. this.deptBudgetList.warehouseName == '克东瑞信达'&&
  114. value.length < 10
  115. ) {
  116. var start = new Date().getTime()
  117. while (new Date().getTime() - start < 30) {}
  118. continue
  119. }
  120. var zzz = ''
  121. for (var i = 0; i < value.length; i++) {
  122. zzz += String.fromCharCode(value[i])
  123. }
  124. console.log('value23:', value)
  125. console.log('valuezzz:', zzz)
  126. } else if (
  127. this.deptBudgetList.warehouseName == '白城内陆港' ||
  128. this.deptBudgetList.warehouseName == '洮南胡力吐粮食储备库' ||
  129. this.deptBudgetList.warehouseName == '鲅鱼圈祥腾库'||
  130. this.deptBudgetList.warehouseName == '洮南向阳乡金昊'
  131. ) {
  132. console.log('value:', value)
  133. var start = new Date().getTime()
  134. while (new Date().getTime() - start < 400) {}
  135. continue
  136. }
  137. else if (
  138. this.deptBudgetList.warehouseName == '克东瑞信达'&&
  139. value.length < 10
  140. ) {
  141. var start = new Date().getTime()
  142. while (new Date().getTime() - start < 30) {}
  143. continue
  144. }
  145. var zzz = ''
  146. for (var i = 0; i < value.length; i++) {
  147. zzz += String.fromCharCode(value[i])
  148. }
  149. console.log('value23:', value)
  150. console.log('valuezzz:', zzz)
  151. if (
  152. this.deptBudgetList.warehouseName &&
  153. (this.deptBudgetList.warehouseName == '山东诸城迈饶库' ||
  154. this.deptBudgetList.warehouseName == '山东园丰库' ||
  155. this.deptBudgetList.warehouseName == '克东千红库' ||
  156. this.deptBudgetList.warehouseName == '鲅鱼圈祥腾库' ||
  157. this.deptBudgetList.warehouseName == '哈尔滨依兰库' ||
  158. this.deptBudgetList.warehouseName == '龙江金信库' ||
  159. this.deptBudgetList.warehouseName == '白城内陆港' ||
  160. this.deptBudgetList.warehouseName == '洮南胡力吐粮食储备库' ||
  161. this.deptBudgetList.warehouseName == '洮南向阳乡金昊' ||
  162. this.deptBudgetList.warehouseName == '甘南林峰库' ||
  163. this.deptBudgetList.warehouseName == '肇东金信库' ||
  164. this.deptBudgetList.warehouseName == '呼兰大金仓'||
  165. this.deptBudgetList.warehouseName == '克东润津库'||
  166. this.deptBudgetList.warehouseName == '洮南新友谊兴旺库'||
  167. this.deptBudgetList.warehouseName == '通榆瑞祥源库'||
  168. this.deptBudgetList.warehouseName == '榆树民胜库'||
  169. this.deptBudgetList.warehouseName == '洮南蛟流河烘干塔'||
  170. this.deptBudgetList.warehouseName == '克东瑞信达')
  171. ) {
  172. for (var i = 0; i < value.length; i++) {
  173. var tmp = String.fromCharCode(value[i])
  174. if (tmp == '+') {
  175. flag = true
  176. }
  177. if (flag && result.length < 6 && tmp != '+') {
  178. result += tmp
  179. }
  180. }
  181. if (this.information.indexOf('毛重') > -1) {
  182. if (parseInt(result) || parseInt(result) == 0) {
  183. this.grossWeightVal = parseInt(result)
  184. }
  185. } else {
  186. if (parseInt(result) || parseInt(result) == 0) {
  187. this.tareVal = parseInt(result)
  188. }
  189. }
  190. } else
  191. if (
  192. this.deptBudgetList.warehouseName &&
  193. (this.deptBudgetList.warehouseName == '顺诚粮库' ||
  194. this.deptBudgetList.warehouseName == '鲅鱼圈金信库')
  195. ) {
  196. for (var i = value.length - 1; i >= 0; i--) {
  197. var tmp = String.fromCharCode(value[i])
  198. console.log(tmp)
  199. if (String.fromCharCode(value[0]) == '.') {
  200. flag = true
  201. }
  202. if (flag && result.length < 9 && tmp != '=' && tmp != '.') {
  203. result += tmp
  204. }
  205. }
  206. if (this.information.indexOf('毛重') > -1) {
  207. if (parseInt(result) || parseInt(result) == 0) {
  208. this.grossWeightVal = parseInt(result)
  209. }
  210. } else {
  211. if (parseInt(result) || parseInt(result) == 0) {
  212. this.tareVal = parseInt(result)
  213. }
  214. }
  215. } else{
  216. if (value.length > 10) {
  217. for (var i = 0; i < value.length; i++) {
  218. var tmp = String.fromCharCode(value[i])
  219. // if (value[0] != 49 && value[0] != 2) {
  220. // // if (
  221. // // value[value.length - 1] == 48 &&
  222. // // value[value.length - 2] == 48
  223. // // ) {
  224. // // flag1 = true
  225. // // } else {
  226. // // break
  227. // // }
  228. // // flag1 = true
  229. // if (i == 0) {
  230. // this.result1 = tmp + '0'
  231. // }
  232. // }
  233. // else{
  234. // if (tmp == String.fromCharCode(32)) {
  235. // flag = true
  236. // }
  237. // }
  238. if (tmp == String.fromCharCode(32)) {
  239. flag = true
  240. }
  241. if (
  242. flag &&
  243. result.length < 7 &&
  244. tmp != String.fromCharCode(32)
  245. // &&
  246. // !(
  247. // value[value.length - 1] == 48 && value[value.length - 2] == 48
  248. // )
  249. ) {
  250. if (i > 0 && value[i] == 48 && value[i - 1] == 32 && result) {
  251. break
  252. } else {
  253. result += tmp
  254. }
  255. }
  256. // if (flag1 && tmp != String.fromCharCode(32)) {
  257. // // if (
  258. // // value[value.length - 1] == 48 &&
  259. // // value[value.length - 2] == 48
  260. // // ) {
  261. // // if (i == 0) {
  262. // // this.result1 = tmp + '0'
  263. // // }
  264. // // }
  265. // if (i == 0) {
  266. // this.result1 = tmp + '0'
  267. // }
  268. // }
  269. }
  270. }
  271. if (this.information.indexOf('毛重') > -1) {
  272. if (parseInt(result) || parseInt(result) == 0) {
  273. this.grossWeightVal = parseInt(result + this.result1)
  274. }
  275. } else {
  276. if (parseInt(result) || parseInt(result) == 0) {
  277. this.tareVal = parseInt(result + this.result1)
  278. }
  279. }
  280. }
  281. // else {
  282. // for (var i = 0; i < value.length; i++) {
  283. // var tmp = String.fromCharCode(value[i])
  284. // if (tmp == '+') {
  285. // flag = true
  286. // }
  287. // if (flag && result.length < 6 && tmp != '+') {
  288. // result += tmp
  289. // }
  290. // }
  291. // if (this.information.indexOf('毛重') > -1) {
  292. // if (parseInt(result) || parseInt(result) == 0) {
  293. // this.grossWeightVal = parseInt(result)
  294. // }
  295. // } else {
  296. // if (parseInt(result) || parseInt(result) == 0) {
  297. // this.tareVal = parseInt(result)
  298. // }
  299. // }
  300. // }
  301. // setTimeout(1000)
  302. // value 是一个 Uint8Array
  303. }
  304. await port.close()
  305. } else {
  306. console.log('the Web Serial API is not supported.', navigator)
  307. }
  308. }
  309. }
  310. }
  311. </script>
  312. <style lang="scss" scoped>
  313. .content {
  314. height: 350px;
  315. position: relative;
  316. background: black;
  317. color: #2aff7c;
  318. font-size: 32px;
  319. text-align: right;
  320. padding: 20px;
  321. border-radius: 10px;
  322. margin: 0 20px 20px 20px;
  323. .btn {
  324. position: absolute;
  325. bottom: 20px;
  326. right: 20px;
  327. border: 1px solid #2aff7c;
  328. width: 200px;
  329. border-radius: 10px;
  330. display: flex;
  331. align-items: center;
  332. justify-content: center;
  333. }
  334. .contentInfo {
  335. display: flex;
  336. .uncertain {
  337. width: 35%;
  338. text-align: left;
  339. }
  340. .number {
  341. width: 65%;
  342. text-align: right;
  343. }
  344. }
  345. }
  346. .title {
  347. text-align: right;
  348. margin-bottom: 20px;
  349. }
  350. </style>