balanceAlert.vue 9.8 KB

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