balanceAlert.vue 8.8 KB

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