balanceAlert.vue 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 == '白城内陆港'){
  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. ) {
  138. for (var i = 0; i < value.length; i++) {
  139. var tmp = String.fromCharCode(value[i])
  140. if (tmp == '+') {
  141. flag = true
  142. }
  143. if (flag && result.length < 6 && tmp != '+') {
  144. result += tmp
  145. }
  146. }
  147. if (this.information.indexOf('毛重') > -1) {
  148. if (parseInt(result) || parseInt(result) == 0) {
  149. this.grossWeightVal = parseInt(result)
  150. }
  151. } else {
  152. if (parseInt(result) || parseInt(result) == 0) {
  153. this.tareVal = parseInt(result)
  154. }
  155. }
  156. } else if (
  157. this.deptBudgetList.warehouseName &&
  158. (this.deptBudgetList.warehouseName == '顺诚粮库' ||
  159. this.deptBudgetList.warehouseName == '鲅鱼圈金信库')
  160. ) {
  161. for (var i = value.length - 1; i >= 0; i--) {
  162. var tmp = String.fromCharCode(value[i])
  163. console.log(tmp)
  164. if (String.fromCharCode(value[0]) == '.') {
  165. flag = true
  166. }
  167. if (flag && result.length < 9 && tmp != '=' && tmp != '.') {
  168. result += tmp
  169. }
  170. }
  171. if (this.information.indexOf('毛重') > -1) {
  172. if (parseInt(result) || parseInt(result) == 0) {
  173. this.grossWeightVal = parseInt(result)
  174. }
  175. } else {
  176. if (parseInt(result) || parseInt(result) == 0) {
  177. this.tareVal = parseInt(result)
  178. }
  179. }
  180. } else {
  181. if (value.length > 10) {
  182. for (var i = 0; i < value.length; i++) {
  183. var tmp = String.fromCharCode(value[i])
  184. // if (value[0] != 49 && value[0] != 2) {
  185. // // if (
  186. // // value[value.length - 1] == 48 &&
  187. // // value[value.length - 2] == 48
  188. // // ) {
  189. // // flag1 = true
  190. // // } else {
  191. // // break
  192. // // }
  193. // // flag1 = true
  194. // if (i == 0) {
  195. // this.result1 = tmp + '0'
  196. // }
  197. // }
  198. // else{
  199. // if (tmp == String.fromCharCode(32)) {
  200. // flag = true
  201. // }
  202. // }
  203. if (tmp == String.fromCharCode(32)) {
  204. flag = true
  205. }
  206. if (
  207. flag &&
  208. result.length < 7 &&
  209. tmp != String.fromCharCode(32)
  210. // &&
  211. // !(
  212. // value[value.length - 1] == 48 && value[value.length - 2] == 48
  213. // )
  214. ) {
  215. if (i > 0 && value[i] == 48 && value[i - 1] == 32 && result) {
  216. break
  217. } else {
  218. result += tmp
  219. }
  220. }
  221. // if (flag1 && tmp != String.fromCharCode(32)) {
  222. // // if (
  223. // // value[value.length - 1] == 48 &&
  224. // // value[value.length - 2] == 48
  225. // // ) {
  226. // // if (i == 0) {
  227. // // this.result1 = tmp + '0'
  228. // // }
  229. // // }
  230. // if (i == 0) {
  231. // this.result1 = tmp + '0'
  232. // }
  233. // }
  234. }
  235. }
  236. if (this.information.indexOf('毛重') > -1) {
  237. if (parseInt(result) || parseInt(result) == 0) {
  238. this.grossWeightVal = parseInt(result + this.result1)
  239. }
  240. } else {
  241. if (parseInt(result) || parseInt(result) == 0) {
  242. this.tareVal = parseInt(result + this.result1)
  243. }
  244. }
  245. }
  246. // setTimeout(1000)
  247. // value 是一个 Uint8Array
  248. }
  249. await port.close()
  250. } else {
  251. console.log('the Web Serial API is not supported.', navigator)
  252. }
  253. },
  254. },
  255. }
  256. </script>
  257. <style lang="scss" scoped>
  258. .content {
  259. height: 350px;
  260. position: relative;
  261. background: black;
  262. color: #2aff7c;
  263. font-size: 32px;
  264. text-align: right;
  265. padding: 20px;
  266. border-radius: 10px;
  267. margin: 0 20px 20px 20px;
  268. .btn {
  269. position: absolute;
  270. bottom: 20px;
  271. right: 20px;
  272. border: 1px solid #2aff7c;
  273. width: 200px;
  274. border-radius: 10px;
  275. display: flex;
  276. align-items: center;
  277. justify-content: center;
  278. }
  279. .contentInfo {
  280. display: flex;
  281. .uncertain {
  282. width: 35%;
  283. text-align: left;
  284. }
  285. .number {
  286. width: 65%;
  287. text-align: right;
  288. }
  289. }
  290. }
  291. .title {
  292. text-align: right;
  293. margin-bottom: 20px;
  294. }
  295. </style>