balanceAlert.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. if (value.length > 2) {
  93. if (value.length <= 6) {
  94. // continue
  95. }
  96. if (
  97. (this.deptBudgetList.warehouseName == '肇东金信库' || this.deptBudgetList.warehouseName == '甘南宏旗库') &&
  98. value.length < 10
  99. ) {
  100. var start = new Date().getTime()
  101. while (new Date().getTime() - start < 100) {}
  102. continue
  103. }
  104. if (
  105. this.deptBudgetList.warehouseName == '克东瑞信达'&&
  106. value.length < 10
  107. ) {
  108. var start = new Date().getTime()
  109. while (new Date().getTime() - start < 30) {}
  110. continue
  111. }
  112. var zzz = ''
  113. for (var i = 0; i < value.length; i++) {
  114. zzz += String.fromCharCode(value[i])
  115. }
  116. console.log('value23:', value)
  117. console.log('valuezzz:', zzz)
  118. } else if (
  119. this.deptBudgetList.warehouseName == '白城内陆港' ||
  120. this.deptBudgetList.warehouseName == '鲅鱼圈祥腾库'
  121. ) {
  122. console.log('value:', value)
  123. var start = new Date().getTime()
  124. while (new Date().getTime() - start < 400) {}
  125. continue
  126. }
  127. else if (
  128. this.deptBudgetList.warehouseName == '克东瑞信达'&&
  129. value.length < 10
  130. ) {
  131. var start = new Date().getTime()
  132. while (new Date().getTime() - start < 30) {}
  133. continue
  134. }
  135. var zzz = ''
  136. for (var i = 0; i < value.length; i++) {
  137. zzz += String.fromCharCode(value[i])
  138. }
  139. console.log('value23:', value)
  140. console.log('valuezzz:', zzz)
  141. if (
  142. this.deptBudgetList.warehouseName &&
  143. (this.deptBudgetList.warehouseName == '山东诸城迈饶库' ||
  144. this.deptBudgetList.warehouseName == '山东园丰库' ||
  145. this.deptBudgetList.warehouseName == '克东千红库' ||
  146. this.deptBudgetList.warehouseName == '鲅鱼圈祥腾库' ||
  147. this.deptBudgetList.warehouseName == '哈尔滨依兰库' ||
  148. this.deptBudgetList.warehouseName == '龙江金信库' ||
  149. this.deptBudgetList.warehouseName == '白城内陆港' ||
  150. this.deptBudgetList.warehouseName == '甘南林峰库' ||
  151. this.deptBudgetList.warehouseName == '肇东金信库' ||
  152. this.deptBudgetList.warehouseName == '呼兰大金仓'||
  153. this.deptBudgetList.warehouseName == '克东瑞信达')
  154. ) {
  155. for (var i = 0; i < value.length; i++) {
  156. var tmp = String.fromCharCode(value[i])
  157. if (tmp == '+') {
  158. flag = true
  159. }
  160. if (flag && result.length < 6 && tmp != '+') {
  161. result += tmp
  162. }
  163. }
  164. if (this.information.indexOf('毛重') > -1) {
  165. if (parseInt(result) || parseInt(result) == 0) {
  166. this.grossWeightVal = parseInt(result)
  167. }
  168. } else {
  169. if (parseInt(result) || parseInt(result) == 0) {
  170. this.tareVal = parseInt(result)
  171. }
  172. }
  173. } else
  174. if (
  175. this.deptBudgetList.warehouseName &&
  176. (this.deptBudgetList.warehouseName == '顺诚粮库' ||
  177. this.deptBudgetList.warehouseName == '鲅鱼圈金信库')
  178. ) {
  179. for (var i = value.length - 1; i >= 0; i--) {
  180. var tmp = String.fromCharCode(value[i])
  181. console.log(tmp)
  182. if (String.fromCharCode(value[0]) == '.') {
  183. flag = true
  184. }
  185. if (flag && result.length < 9 && tmp != '=' && tmp != '.') {
  186. result += tmp
  187. }
  188. }
  189. if (this.information.indexOf('毛重') > -1) {
  190. if (parseInt(result) || parseInt(result) == 0) {
  191. this.grossWeightVal = parseInt(result)
  192. }
  193. } else {
  194. if (parseInt(result) || parseInt(result) == 0) {
  195. this.tareVal = parseInt(result)
  196. }
  197. }
  198. } else{
  199. if (value.length > 10) {
  200. for (var i = 0; i < value.length; i++) {
  201. var tmp = String.fromCharCode(value[i])
  202. // if (value[0] != 49 && value[0] != 2) {
  203. // // if (
  204. // // value[value.length - 1] == 48 &&
  205. // // value[value.length - 2] == 48
  206. // // ) {
  207. // // flag1 = true
  208. // // } else {
  209. // // break
  210. // // }
  211. // // flag1 = true
  212. // if (i == 0) {
  213. // this.result1 = tmp + '0'
  214. // }
  215. // }
  216. // else{
  217. // if (tmp == String.fromCharCode(32)) {
  218. // flag = true
  219. // }
  220. // }
  221. if (tmp == String.fromCharCode(32)) {
  222. flag = true
  223. }
  224. if (
  225. flag &&
  226. result.length < 7 &&
  227. tmp != String.fromCharCode(32)
  228. // &&
  229. // !(
  230. // value[value.length - 1] == 48 && value[value.length - 2] == 48
  231. // )
  232. ) {
  233. if (i > 0 && value[i] == 48 && value[i - 1] == 32 && result) {
  234. break
  235. } else {
  236. result += tmp
  237. }
  238. }
  239. // if (flag1 && tmp != String.fromCharCode(32)) {
  240. // // if (
  241. // // value[value.length - 1] == 48 &&
  242. // // value[value.length - 2] == 48
  243. // // ) {
  244. // // if (i == 0) {
  245. // // this.result1 = tmp + '0'
  246. // // }
  247. // // }
  248. // if (i == 0) {
  249. // this.result1 = tmp + '0'
  250. // }
  251. // }
  252. }
  253. }
  254. if (this.information.indexOf('毛重') > -1) {
  255. if (parseInt(result) || parseInt(result) == 0) {
  256. this.grossWeightVal = parseInt(result + this.result1)
  257. }
  258. } else {
  259. if (parseInt(result) || parseInt(result) == 0) {
  260. this.tareVal = parseInt(result + this.result1)
  261. }
  262. }
  263. }
  264. // else {
  265. // for (var i = 0; i < value.length; i++) {
  266. // var tmp = String.fromCharCode(value[i])
  267. // if (tmp == '+') {
  268. // flag = true
  269. // }
  270. // if (flag && result.length < 6 && tmp != '+') {
  271. // result += tmp
  272. // }
  273. // }
  274. // if (this.information.indexOf('毛重') > -1) {
  275. // if (parseInt(result) || parseInt(result) == 0) {
  276. // this.grossWeightVal = parseInt(result)
  277. // }
  278. // } else {
  279. // if (parseInt(result) || parseInt(result) == 0) {
  280. // this.tareVal = parseInt(result)
  281. // }
  282. // }
  283. // }
  284. // setTimeout(1000)
  285. // value 是一个 Uint8Array
  286. }
  287. await port.close()
  288. } else {
  289. console.log('the Web Serial API is not supported.', navigator)
  290. }
  291. }
  292. }
  293. }
  294. </script>
  295. <style lang="scss" scoped>
  296. .content {
  297. height: 350px;
  298. position: relative;
  299. background: black;
  300. color: #2aff7c;
  301. font-size: 32px;
  302. text-align: right;
  303. padding: 20px;
  304. border-radius: 10px;
  305. margin: 0 20px 20px 20px;
  306. .btn {
  307. position: absolute;
  308. bottom: 20px;
  309. right: 20px;
  310. border: 1px solid #2aff7c;
  311. width: 200px;
  312. border-radius: 10px;
  313. display: flex;
  314. align-items: center;
  315. justify-content: center;
  316. }
  317. .contentInfo {
  318. display: flex;
  319. .uncertain {
  320. width: 35%;
  321. text-align: left;
  322. }
  323. .number {
  324. width: 65%;
  325. text-align: right;
  326. }
  327. }
  328. }
  329. .title {
  330. text-align: right;
  331. margin-bottom: 20px;
  332. }
  333. </style>