balanceAlert.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. this.deptBudgetList.warehouseName == '洮南向阳乡金昊'
  62. ) {
  63. this.param = 1200
  64. } else if (this.deptBudgetList.warehouseName == '鲅鱼圈祥腾库') {
  65. this.param = 2400
  66. } else {
  67. this.param = 9600
  68. }
  69. console.log(this.param)
  70. await port.open({
  71. baudRate: this.param
  72. }) // set baud rate
  73. this.reader = port.readable.getReader()
  74. console.log('port ', port)
  75. this.$store.dispatch('app/setReader', this.reader)
  76. console.log('reader ', this.reader)
  77. } else {
  78. console.log('afterport', this.$store.state.app.reader)
  79. this.reader = this.$store.state.app.reader
  80. }
  81. // 监听来自串行设备的数据
  82. while (true) {
  83. const { value, done } = await this.reader.read()
  84. // console.log('value',value);
  85. if (done) {
  86. // 允许稍后关闭串口。
  87. this.reader.releaseLock()
  88. break
  89. }
  90. var result = ''
  91. //2。获取16进制字符串
  92. // var receData = HexConvert.ByteToString(value);
  93. // console.log("receData",receData);
  94. var flag = false
  95. if (
  96. this.deptBudgetList.warehouseName == '克东润津库'&&
  97. value.length < 9
  98. ) {
  99. continue
  100. }
  101. if (value.length > 2) {
  102. if (value.length <= 6) {
  103. // continue
  104. }
  105. if (
  106. (this.deptBudgetList.warehouseName == '肇东金信库' || this.deptBudgetList.warehouseName == '甘南宏旗库') &&
  107. value.length < 10
  108. ) {
  109. var start = new Date().getTime()
  110. while (new Date().getTime() - start < 100) {}
  111. continue
  112. }
  113. if (
  114. this.deptBudgetList.warehouseName == '克东瑞信达'&&
  115. value.length < 10
  116. ) {
  117. var start = new Date().getTime()
  118. while (new Date().getTime() - start < 30) {}
  119. continue
  120. }
  121. var zzz = ''
  122. for (var i = 0; i < value.length; i++) {
  123. zzz += String.fromCharCode(value[i])
  124. }
  125. console.log('value23:', value)
  126. console.log('valuezzz:', zzz)
  127. } else if (
  128. this.deptBudgetList.warehouseName == '白城内陆港' ||
  129. this.deptBudgetList.warehouseName == '洮南胡力吐粮食储备库' ||
  130. this.deptBudgetList.warehouseName == '鲅鱼圈祥腾库'||
  131. this.deptBudgetList.warehouseName == '洮南向阳乡金昊'
  132. ) {
  133. console.log('value:', value)
  134. var start = new Date().getTime()
  135. while (new Date().getTime() - start < 400) {}
  136. continue
  137. }
  138. else if (
  139. this.deptBudgetList.warehouseName == '克东瑞信达'&&
  140. value.length < 10
  141. ) {
  142. var start = new Date().getTime()
  143. while (new Date().getTime() - start < 30) {}
  144. continue
  145. }
  146. var zzz = ''
  147. for (var i = 0; i < value.length; i++) {
  148. zzz += String.fromCharCode(value[i])
  149. }
  150. console.log('value23:', value)
  151. console.log('valuezzz:', zzz)
  152. if (
  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. this.deptBudgetList.warehouseName == '洮南蛟流河烘干塔'||
  172. this.deptBudgetList.warehouseName == '克东瑞信达')
  173. ) {
  174. for (var i = 0; i < value.length; i++) {
  175. var tmp = String.fromCharCode(value[i])
  176. if (tmp == '+') {
  177. flag = true
  178. }
  179. if (flag && result.length < 6 && tmp != '+') {
  180. result += tmp
  181. }
  182. }
  183. if (this.information.indexOf('毛重') > -1) {
  184. if (parseInt(result) || parseInt(result) == 0) {
  185. this.grossWeightVal = parseInt(result)
  186. }
  187. } else {
  188. if (parseInt(result) || parseInt(result) == 0) {
  189. this.tareVal = parseInt(result)
  190. }
  191. }
  192. } else
  193. if (
  194. this.deptBudgetList.warehouseName &&
  195. (this.deptBudgetList.warehouseName == '顺诚粮库' ||
  196. this.deptBudgetList.warehouseName == '鲅鱼圈金信库')
  197. ) {
  198. for (var i = value.length - 1; i >= 0; i--) {
  199. var tmp = String.fromCharCode(value[i])
  200. console.log(tmp)
  201. if (String.fromCharCode(value[0]) == '.') {
  202. flag = true
  203. }
  204. if (flag && result.length < 9 && tmp != '=' && tmp != '.') {
  205. result += tmp
  206. }
  207. }
  208. if (this.information.indexOf('毛重') > -1) {
  209. if (parseInt(result) || parseInt(result) == 0) {
  210. this.grossWeightVal = parseInt(result)
  211. }
  212. } else {
  213. if (parseInt(result) || parseInt(result) == 0) {
  214. this.tareVal = parseInt(result)
  215. }
  216. }
  217. } else{
  218. if (value.length > 10) {
  219. for (var i = 0; i < value.length; i++) {
  220. var tmp = String.fromCharCode(value[i])
  221. // if (value[0] != 49 && value[0] != 2) {
  222. // // if (
  223. // // value[value.length - 1] == 48 &&
  224. // // value[value.length - 2] == 48
  225. // // ) {
  226. // // flag1 = true
  227. // // } else {
  228. // // break
  229. // // }
  230. // // flag1 = true
  231. // if (i == 0) {
  232. // this.result1 = tmp + '0'
  233. // }
  234. // }
  235. // else{
  236. // if (tmp == String.fromCharCode(32)) {
  237. // flag = true
  238. // }
  239. // }
  240. if (tmp == String.fromCharCode(32)) {
  241. flag = true
  242. }
  243. if (
  244. flag &&
  245. result.length < 7 &&
  246. tmp != String.fromCharCode(32)
  247. // &&
  248. // !(
  249. // value[value.length - 1] == 48 && value[value.length - 2] == 48
  250. // )
  251. ) {
  252. if (i > 0 && value[i] == 48 && value[i - 1] == 32 && result) {
  253. break
  254. } else {
  255. result += tmp
  256. }
  257. }
  258. // if (flag1 && tmp != String.fromCharCode(32)) {
  259. // // if (
  260. // // value[value.length - 1] == 48 &&
  261. // // value[value.length - 2] == 48
  262. // // ) {
  263. // // if (i == 0) {
  264. // // this.result1 = tmp + '0'
  265. // // }
  266. // // }
  267. // if (i == 0) {
  268. // this.result1 = tmp + '0'
  269. // }
  270. // }
  271. }
  272. }
  273. if (this.information.indexOf('毛重') > -1) {
  274. if (parseInt(result) || parseInt(result) == 0) {
  275. this.grossWeightVal = parseInt(result + this.result1)
  276. }
  277. } else {
  278. if (parseInt(result) || parseInt(result) == 0) {
  279. this.tareVal = parseInt(result + this.result1)
  280. }
  281. }
  282. }
  283. // else {
  284. // for (var i = 0; i < value.length; i++) {
  285. // var tmp = String.fromCharCode(value[i])
  286. // if (tmp == '+') {
  287. // flag = true
  288. // }
  289. // if (flag && result.length < 6 && tmp != '+') {
  290. // result += tmp
  291. // }
  292. // }
  293. // if (this.information.indexOf('毛重') > -1) {
  294. // if (parseInt(result) || parseInt(result) == 0) {
  295. // this.grossWeightVal = parseInt(result)
  296. // }
  297. // } else {
  298. // if (parseInt(result) || parseInt(result) == 0) {
  299. // this.tareVal = parseInt(result)
  300. // }
  301. // }
  302. // }
  303. // setTimeout(1000)
  304. // value 是一个 Uint8Array
  305. }
  306. await port.close()
  307. } else {
  308. console.log('the Web Serial API is not supported.', navigator)
  309. }
  310. }
  311. }
  312. }
  313. </script>
  314. <style lang="scss" scoped>
  315. .content {
  316. height: 350px;
  317. position: relative;
  318. background: black;
  319. color: #2aff7c;
  320. font-size: 32px;
  321. text-align: right;
  322. padding: 20px;
  323. border-radius: 10px;
  324. margin: 0 20px 20px 20px;
  325. .btn {
  326. position: absolute;
  327. bottom: 20px;
  328. right: 20px;
  329. border: 1px solid #2aff7c;
  330. width: 200px;
  331. border-radius: 10px;
  332. display: flex;
  333. align-items: center;
  334. justify-content: center;
  335. }
  336. .contentInfo {
  337. display: flex;
  338. .uncertain {
  339. width: 35%;
  340. text-align: left;
  341. }
  342. .number {
  343. width: 65%;
  344. text-align: right;
  345. }
  346. }
  347. }
  348. .title {
  349. text-align: right;
  350. margin-bottom: 20px;
  351. }
  352. </style>