balanceAlert.vue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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="number" v-if="information.indexOf('毛重') > -1">{{grossWeightVal}} kg</div>
  6. <div class="number" v-else>{{tareVal}} KG</div>
  7. <div class="btn" @click="sendVal">确定</div>
  8. </div>
  9. </template>
  10. <script>
  11. export default {
  12. name: 'balanceAlert',
  13. props: ['deptBudgetList', 'information'],
  14. data () {
  15. return {
  16. param: 9600,
  17. grossWeightVal:'',
  18. tareVal:''
  19. }
  20. },
  21. activated(){
  22. },
  23. mounted(){
  24. console.log('11111111111111111111111111111111111')
  25. console.log(this.deptBudgetList)
  26. this.openPort()
  27. },
  28. computed: {
  29. },
  30. created () {
  31. },
  32. methods: {
  33. sendVal(){
  34. console.log(this.grossWeightVal)
  35. console.log(this.tareVal)
  36. if(this.information.indexOf('毛重') > -1){
  37. this.$emit('balanceListen',this.grossWeightVal)
  38. }else{
  39. this.$emit('balanceListen',this.tareVal)
  40. }
  41. },
  42. async closePort() {
  43. console.log('closePort')
  44. this.reader.cancel()
  45. },
  46. async openPort() {
  47. console.log('openPort', navigator)
  48. if ('serial' in navigator) {
  49. if (!this.$store.state.app.reader) {
  50. // The Web Serial API is supported.
  51. console.log('the Web Serial API is supported.')
  52. console.log(this.param)
  53. const port = await navigator.serial.requestPort()
  54. await port.open({
  55. baudRate: this.param,
  56. }) // set baud rate
  57. this.reader = port.readable.getReader()
  58. console.log('beforeReader', port)
  59. console.log('beforeReader', this.reader)
  60. this.$store.dispatch('app/setReader', this.reader)
  61. } else {
  62. console.log('afterport', this.$store.state.app.reader)
  63. this.reader = this.$store.state.app.reader
  64. }
  65. // 监听来自串行设备的数据
  66. while (true) {
  67. const { value, done } = await this.reader.read()
  68. // console.log("value",value);
  69. if (done) {
  70. // 允许稍后关闭串口。
  71. this.reader.releaseLock()
  72. break
  73. }
  74. var result = ''
  75. //2。获取16进制字符串
  76. // var receData = HexConvert.ByteToString(value);
  77. // console.log("receData",receData);
  78. var flag = false
  79. var flag1 = false
  80. // for (var i = 0; i < value.length; i++) {
  81. // var tmp = String.fromCharCode(value[i])
  82. // if (tmp == '+') {
  83. // flag = true
  84. // }
  85. // if (flag && result.length < 6 && tmp != '+') {
  86. // result += tmp
  87. // }
  88. // }
  89. // if (this.information != '毛重检斤') {
  90. // if(parseInt(result)){
  91. // this.deptBudgetList.grossWeight = parseInt(result)
  92. // }
  93. // } else {
  94. // if(parseInt(result)){
  95. // this.deptBudgetList.tare = parseInt(result)
  96. // }
  97. // }
  98. console.log('value:', value)
  99. if (
  100. this.deptBudgetList.warehouseName &&
  101. (this.deptBudgetList.warehouseName == '山东诸城迈饶库' ||
  102. this.deptBudgetList.warehouseName == '克东千红库' ||
  103. this.deptBudgetList.warehouseName == '鲅鱼圈祥腾库' ||
  104. this.deptBudgetList.warehouseName == '哈尔滨依兰库'||
  105. this.deptBudgetList.warehouseName == '龙江金信库')
  106. ) {
  107. for (var i = 0; i < value.length; i++) {
  108. var tmp = String.fromCharCode(value[i])
  109. if (tmp == '+') {
  110. flag = true
  111. }
  112. if (flag && result.length < 6 && tmp != '+') {
  113. result += tmp
  114. }
  115. }
  116. if (this.information.indexOf('毛重') > -1) {
  117. if (parseInt(result) || parseInt(result) == 0) {
  118. this.grossWeightVal = parseInt(result)
  119. }
  120. } else {
  121. if (parseInt(result) || parseInt(result) == 0) {
  122. this.tareVal = parseInt(result)
  123. }
  124. }
  125. } else if (
  126. this.deptBudgetList.warehouseName &&
  127. (this.deptBudgetList.warehouseName == '顺诚粮库' ||
  128. this.deptBudgetList.warehouseName == '鲅鱼圈金信库')
  129. ) {
  130. for (var i = value.length - 1; i >= 0; i--) {
  131. var tmp = String.fromCharCode(value[i])
  132. console.log(tmp)
  133. if (String.fromCharCode(value[0]) == '.') {
  134. flag = true
  135. }
  136. if (flag && result.length < 9 && tmp != '=' && tmp != '.') {
  137. result += tmp
  138. }
  139. }
  140. if (this.information.indexOf('毛重') > -1) {
  141. if (parseInt(result) || parseInt(result) == 0) {
  142. this.grossWeightVal = parseInt(result)
  143. }
  144. } else {
  145. if (parseInt(result) || parseInt(result) == 0) {
  146. this.tareVal = parseInt(result)
  147. }
  148. }
  149. } else {
  150. if(value.length > 10){
  151. for (var i = 0; i < value.length; i++) {
  152. var tmp = String.fromCharCode(value[i])
  153. // if (value[0] != 49 && value[0] != 2) {
  154. // // if (
  155. // // value[value.length - 1] == 48 &&
  156. // // value[value.length - 2] == 48
  157. // // ) {
  158. // // flag1 = true
  159. // // } else {
  160. // // break
  161. // // }
  162. // // flag1 = true
  163. // if (i == 0) {
  164. // this.result1 = tmp + '0'
  165. // }
  166. // }
  167. // else{
  168. // if (tmp == String.fromCharCode(32)) {
  169. // flag = true
  170. // }
  171. // }
  172. if (tmp == String.fromCharCode(32)) {
  173. flag = true
  174. }
  175. if (
  176. flag &&
  177. result.length < 7 &&
  178. tmp != String.fromCharCode(32)
  179. // &&
  180. // !(
  181. // value[value.length - 1] == 48 && value[value.length - 2] == 48
  182. // )
  183. ) {
  184. if(i > 0&& value[i] == 48&& value[i-1] == 32 && result){
  185. break
  186. }
  187. else{
  188. result += tmp
  189. }
  190. }
  191. // if (flag1 && tmp != String.fromCharCode(32)) {
  192. // // if (
  193. // // value[value.length - 1] == 48 &&
  194. // // value[value.length - 2] == 48
  195. // // ) {
  196. // // if (i == 0) {
  197. // // this.result1 = tmp + '0'
  198. // // }
  199. // // }
  200. // if (i == 0) {
  201. // this.result1 = tmp + '0'
  202. // }
  203. // }
  204. }
  205. }
  206. if (this.information.indexOf('毛重') > -1) {
  207. if (parseInt(result) || parseInt(result) == 0) {
  208. this.grossWeightVal = parseInt(
  209. result + this.result1
  210. )
  211. }
  212. } else {
  213. if (parseInt(result) || parseInt(result) == 0) {
  214. this.tareVal = parseInt(result + this.result1)
  215. }
  216. }
  217. }
  218. // setTimeout(1000)
  219. // value 是一个 Uint8Array
  220. }
  221. await port.close()
  222. } else {
  223. console.log('the Web Serial API is not supported.', navigator)
  224. }
  225. },
  226. }
  227. }
  228. </script>
  229. <style lang="scss" scoped>
  230. .content{
  231. background: black;
  232. color: #2AFF7C;
  233. font-size: 32px;
  234. text-align: right;
  235. padding: 20px;
  236. border-radius: 10px;
  237. margin: 0 20px 20px 20px;
  238. }
  239. .title{
  240. text-align: center;
  241. }
  242. .number{
  243. // font-size: 60px;
  244. }
  245. .btn{
  246. border: 1px solid #2aff7c;
  247. width: 50%;
  248. // margin: 30px auto;
  249. border-radius: 10px;
  250. display: flex;
  251. align-items: center;
  252. justify-content: center;
  253. }
  254. </style>