balanceAlert.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. this.deptBudgetList.warehouseName == '克东瑞信达')
  174. ) {
  175. for (var i = 0; i < value.length; i++) {
  176. var tmp = String.fromCharCode(value[i])
  177. if (tmp == '+') {
  178. flag = true
  179. }
  180. if (flag && result.length < 6 && tmp != '+') {
  181. result += tmp
  182. }
  183. }
  184. if (this.information.indexOf('毛重') > -1) {
  185. if (parseInt(result) || parseInt(result) == 0) {
  186. this.grossWeightVal = parseInt(result)
  187. }
  188. } else {
  189. if (parseInt(result) || parseInt(result) == 0) {
  190. this.tareVal = parseInt(result)
  191. }
  192. }
  193. } else
  194. if (
  195. this.deptBudgetList.warehouseName &&
  196. (this.deptBudgetList.warehouseName == '顺诚粮库' ||
  197. this.deptBudgetList.warehouseName == '鲅鱼圈金信库')
  198. ) {
  199. for (var i = value.length - 1; i >= 0; i--) {
  200. var tmp = String.fromCharCode(value[i])
  201. console.log(tmp)
  202. if (String.fromCharCode(value[0]) == '.') {
  203. flag = true
  204. }
  205. if (flag && result.length < 9 && tmp != '=' && tmp != '.') {
  206. result += tmp
  207. }
  208. }
  209. if (this.information.indexOf('毛重') > -1) {
  210. if (parseInt(result) || parseInt(result) == 0) {
  211. this.grossWeightVal = parseInt(result)
  212. }
  213. } else {
  214. if (parseInt(result) || parseInt(result) == 0) {
  215. this.tareVal = parseInt(result)
  216. }
  217. }
  218. } else{
  219. if (value.length > 10) {
  220. for (var i = 0; i < value.length; i++) {
  221. var tmp = String.fromCharCode(value[i])
  222. // if (value[0] != 49 && value[0] != 2) {
  223. // // if (
  224. // // value[value.length - 1] == 48 &&
  225. // // value[value.length - 2] == 48
  226. // // ) {
  227. // // flag1 = true
  228. // // } else {
  229. // // break
  230. // // }
  231. // // flag1 = true
  232. // if (i == 0) {
  233. // this.result1 = tmp + '0'
  234. // }
  235. // }
  236. // else{
  237. // if (tmp == String.fromCharCode(32)) {
  238. // flag = true
  239. // }
  240. // }
  241. if (tmp == String.fromCharCode(32)) {
  242. flag = true
  243. }
  244. if (
  245. flag &&
  246. result.length < 7 &&
  247. tmp != String.fromCharCode(32)
  248. // &&
  249. // !(
  250. // value[value.length - 1] == 48 && value[value.length - 2] == 48
  251. // )
  252. ) {
  253. if (i > 0 && value[i] == 48 && value[i - 1] == 32 && result) {
  254. break
  255. } else {
  256. result += tmp
  257. }
  258. }
  259. // if (flag1 && tmp != String.fromCharCode(32)) {
  260. // // if (
  261. // // value[value.length - 1] == 48 &&
  262. // // value[value.length - 2] == 48
  263. // // ) {
  264. // // if (i == 0) {
  265. // // this.result1 = tmp + '0'
  266. // // }
  267. // // }
  268. // if (i == 0) {
  269. // this.result1 = tmp + '0'
  270. // }
  271. // }
  272. }
  273. }
  274. if (this.information.indexOf('毛重') > -1) {
  275. if (parseInt(result) || parseInt(result) == 0) {
  276. this.grossWeightVal = parseInt(result + this.result1)
  277. }
  278. } else {
  279. if (parseInt(result) || parseInt(result) == 0) {
  280. this.tareVal = parseInt(result + this.result1)
  281. }
  282. }
  283. }
  284. // else {
  285. // for (var i = 0; i < value.length; i++) {
  286. // var tmp = String.fromCharCode(value[i])
  287. // if (tmp == '+') {
  288. // flag = true
  289. // }
  290. // if (flag && result.length < 6 && tmp != '+') {
  291. // result += tmp
  292. // }
  293. // }
  294. // if (this.information.indexOf('毛重') > -1) {
  295. // if (parseInt(result) || parseInt(result) == 0) {
  296. // this.grossWeightVal = parseInt(result)
  297. // }
  298. // } else {
  299. // if (parseInt(result) || parseInt(result) == 0) {
  300. // this.tareVal = parseInt(result)
  301. // }
  302. // }
  303. // }
  304. // setTimeout(1000)
  305. // value 是一个 Uint8Array
  306. }
  307. await port.close()
  308. } else {
  309. console.log('the Web Serial API is not supported.', navigator)
  310. }
  311. }
  312. }
  313. }
  314. </script>
  315. <style lang="scss" scoped>
  316. .content {
  317. height: 350px;
  318. position: relative;
  319. background: black;
  320. color: #2aff7c;
  321. font-size: 32px;
  322. text-align: right;
  323. padding: 20px;
  324. border-radius: 10px;
  325. margin: 0 20px 20px 20px;
  326. .btn {
  327. position: absolute;
  328. bottom: 20px;
  329. right: 20px;
  330. border: 1px solid #2aff7c;
  331. width: 200px;
  332. border-radius: 10px;
  333. display: flex;
  334. align-items: center;
  335. justify-content: center;
  336. }
  337. .contentInfo {
  338. display: flex;
  339. .uncertain {
  340. width: 35%;
  341. text-align: left;
  342. }
  343. .number {
  344. width: 65%;
  345. text-align: right;
  346. }
  347. }
  348. }
  349. .title {
  350. text-align: right;
  351. margin-bottom: 20px;
  352. }
  353. </style>