balanceAlert.vue 9.5 KB

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