balanceAlert.vue 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. ) {
  60. this.param = 1200
  61. } else if (this.deptBudgetList.warehouseName == '鲅鱼圈祥腾库') {
  62. this.param = 2400
  63. } else {
  64. this.param = 9600
  65. }
  66. console.log(this.param)
  67. await port.open({
  68. baudRate: this.param
  69. }) // set baud rate
  70. this.reader = port.readable.getReader()
  71. console.log('port ', port)
  72. this.$store.dispatch('app/setReader', this.reader)
  73. console.log('reader ', this.reader)
  74. } else {
  75. console.log('afterport', this.$store.state.app.reader)
  76. this.reader = this.$store.state.app.reader
  77. }
  78. // 监听来自串行设备的数据
  79. while (true) {
  80. const { value, done } = await this.reader.read()
  81. // console.log('value',value);
  82. if (done) {
  83. // 允许稍后关闭串口。
  84. this.reader.releaseLock()
  85. break
  86. }
  87. var result = ''
  88. //2。获取16进制字符串
  89. // var receData = HexConvert.ByteToString(value);
  90. // console.log("receData",receData);
  91. var flag = false
  92. var flag1 = false
  93. // for (var i = 0; i < value.length; i++) {
  94. // var tmp = String.fromCharCode(value[i])
  95. // if (tmp == '+') {
  96. // flag = true
  97. // }
  98. // if (flag && result.length < 6 && tmp != '+') {
  99. // result += tmp
  100. // }
  101. // }
  102. // if (this.information != '毛重检斤') {
  103. // if(parseInt(result)){
  104. // this.deptBudgetList.grossWeight = parseInt(result)
  105. // }
  106. // } else {
  107. // if(parseInt(result)){
  108. // this.deptBudgetList.tare = parseInt(result)
  109. // }
  110. // }
  111. if (value.length > 3) {
  112. if (value.length <= 6) {
  113. continue
  114. }
  115. if (
  116. this.deptBudgetList.warehouseName == '肇东金信库' &&
  117. value.length < 10
  118. ) {
  119. var start = new Date().getTime()
  120. while (new Date().getTime() - start < 200) {}
  121. continue
  122. }
  123. console.log('value23:', value)
  124. } else if (
  125. this.deptBudgetList.warehouseName == '白城内陆港' ||
  126. this.deptBudgetList.warehouseName == '鲅鱼圈祥腾库'
  127. ) {
  128. console.log('value:', value)
  129. var start = new Date().getTime()
  130. while (new Date().getTime() - start < 400) {}
  131. continue
  132. }
  133. if (
  134. this.deptBudgetList.warehouseName &&
  135. (this.deptBudgetList.warehouseName == '山东诸城迈饶库' ||
  136. this.deptBudgetList.warehouseName == '山东园丰库' ||
  137. this.deptBudgetList.warehouseName == '克东千红库' ||
  138. this.deptBudgetList.warehouseName == '鲅鱼圈祥腾库' ||
  139. this.deptBudgetList.warehouseName == '哈尔滨依兰库' ||
  140. this.deptBudgetList.warehouseName == '龙江金信库' ||
  141. this.deptBudgetList.warehouseName == '白城内陆港' ||
  142. this.deptBudgetList.warehouseName == '肇东金信库' ||
  143. this.deptBudgetList.warehouseName == '呼兰大金仓')
  144. ) {
  145. for (var i = 0; i < value.length; i++) {
  146. var tmp = String.fromCharCode(value[i])
  147. if (tmp == '+') {
  148. flag = true
  149. }
  150. if (flag && result.length < 6 && tmp != '+') {
  151. result += tmp
  152. }
  153. }
  154. if (this.information.indexOf('毛重') > -1) {
  155. if (parseInt(result) || parseInt(result) == 0) {
  156. this.grossWeightVal = parseInt(result)
  157. }
  158. } else {
  159. if (parseInt(result) || parseInt(result) == 0) {
  160. this.tareVal = parseInt(result)
  161. }
  162. }
  163. } else if (
  164. this.deptBudgetList.warehouseName &&
  165. (this.deptBudgetList.warehouseName == '顺诚粮库' ||
  166. this.deptBudgetList.warehouseName == '鲅鱼圈金信库')
  167. ) {
  168. for (var i = value.length - 1; i >= 0; i--) {
  169. var tmp = String.fromCharCode(value[i])
  170. console.log(tmp)
  171. if (String.fromCharCode(value[0]) == '.') {
  172. flag = true
  173. }
  174. if (flag && result.length < 9 && tmp != '=' && tmp != '.') {
  175. result += tmp
  176. }
  177. }
  178. if (this.information.indexOf('毛重') > -1) {
  179. if (parseInt(result) || parseInt(result) == 0) {
  180. this.grossWeightVal = parseInt(result)
  181. }
  182. } else {
  183. if (parseInt(result) || parseInt(result) == 0) {
  184. this.tareVal = parseInt(result)
  185. }
  186. }
  187. } else {
  188. if (value.length > 10) {
  189. for (var i = 0; i < value.length; i++) {
  190. var tmp = String.fromCharCode(value[i])
  191. // if (value[0] != 49 && value[0] != 2) {
  192. // // if (
  193. // // value[value.length - 1] == 48 &&
  194. // // value[value.length - 2] == 48
  195. // // ) {
  196. // // flag1 = true
  197. // // } else {
  198. // // break
  199. // // }
  200. // // flag1 = true
  201. // if (i == 0) {
  202. // this.result1 = tmp + '0'
  203. // }
  204. // }
  205. // else{
  206. // if (tmp == String.fromCharCode(32)) {
  207. // flag = true
  208. // }
  209. // }
  210. if (tmp == String.fromCharCode(32)) {
  211. flag = true
  212. }
  213. if (
  214. flag &&
  215. result.length < 7 &&
  216. tmp != String.fromCharCode(32)
  217. // &&
  218. // !(
  219. // value[value.length - 1] == 48 && value[value.length - 2] == 48
  220. // )
  221. ) {
  222. if (i > 0 && value[i] == 48 && value[i - 1] == 32 && result) {
  223. break
  224. } else {
  225. result += tmp
  226. }
  227. }
  228. // if (flag1 && tmp != String.fromCharCode(32)) {
  229. // // if (
  230. // // value[value.length - 1] == 48 &&
  231. // // value[value.length - 2] == 48
  232. // // ) {
  233. // // if (i == 0) {
  234. // // this.result1 = tmp + '0'
  235. // // }
  236. // // }
  237. // if (i == 0) {
  238. // this.result1 = tmp + '0'
  239. // }
  240. // }
  241. }
  242. }
  243. if (this.information.indexOf('毛重') > -1) {
  244. if (parseInt(result) || parseInt(result) == 0) {
  245. this.grossWeightVal = parseInt(result + this.result1)
  246. }
  247. } else {
  248. if (parseInt(result) || parseInt(result) == 0) {
  249. this.tareVal = parseInt(result + this.result1)
  250. }
  251. }
  252. }
  253. // setTimeout(1000)
  254. // value 是一个 Uint8Array
  255. }
  256. await port.close()
  257. } else {
  258. console.log('the Web Serial API is not supported.', navigator)
  259. }
  260. }
  261. }
  262. }
  263. </script>
  264. <style lang="scss" scoped>
  265. .content {
  266. height: 350px;
  267. position: relative;
  268. background: black;
  269. color: #2aff7c;
  270. font-size: 32px;
  271. text-align: right;
  272. padding: 20px;
  273. border-radius: 10px;
  274. margin: 0 20px 20px 20px;
  275. .btn {
  276. position: absolute;
  277. bottom: 20px;
  278. right: 20px;
  279. border: 1px solid #2aff7c;
  280. width: 200px;
  281. border-radius: 10px;
  282. display: flex;
  283. align-items: center;
  284. justify-content: center;
  285. }
  286. .contentInfo {
  287. display: flex;
  288. .uncertain {
  289. width: 35%;
  290. text-align: left;
  291. }
  292. .number {
  293. width: 65%;
  294. text-align: right;
  295. }
  296. }
  297. }
  298. .title {
  299. text-align: right;
  300. margin-bottom: 20px;
  301. }
  302. </style>