balanceAlert.vue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. for (var i = 0; i < value.length; i++) {
  151. var tmp = String.fromCharCode(value[i])
  152. if (value[0] != 49 && value[0] != 2) {
  153. if (
  154. value[value.length - 1] == 48 &&
  155. value[value.length - 2] == 48
  156. ) {
  157. flag1 = true
  158. } else {
  159. break
  160. }
  161. }
  162. if (tmp == String.fromCharCode(32)) {
  163. flag = true
  164. }
  165. if (
  166. flag &&
  167. result.length < 7 &&
  168. tmp != String.fromCharCode(32) &&
  169. !(
  170. value[value.length - 1] == 48 && value[value.length - 2] == 48
  171. )
  172. ) {
  173. result += tmp
  174. }
  175. if (flag1 && tmp != String.fromCharCode(32)) {
  176. if (
  177. value[value.length - 1] == 48 &&
  178. value[value.length - 2] == 48
  179. ) {
  180. if (i == 0) {
  181. this.result1 = tmp + '0'
  182. }
  183. }
  184. }
  185. }
  186. if (this.information.indexOf('毛重') > -1) {
  187. if (parseInt(result) || parseInt(result) == 0) {
  188. this.grossWeightVal = parseInt(
  189. result + this.result1
  190. )
  191. }
  192. } else {
  193. if (parseInt(result) || parseInt(result) == 0) {
  194. this.tareVal = parseInt(result + this.result1)
  195. }
  196. }
  197. }
  198. // setTimeout(1000)
  199. // value 是一个 Uint8Array
  200. }
  201. await port.close()
  202. } else {
  203. console.log('the Web Serial API is not supported.', navigator)
  204. }
  205. },
  206. }
  207. }
  208. </script>
  209. <style lang="scss" scoped>
  210. .content{
  211. background: black;
  212. color: #2AFF7C;
  213. font-size: 32px;
  214. text-align: right;
  215. padding: 20px;
  216. border-radius: 10px;
  217. margin: 0 20px 20px 20px;
  218. }
  219. .title{
  220. text-align: center;
  221. }
  222. .number{
  223. // font-size: 60px;
  224. }
  225. .btn{
  226. border: 1px solid #2aff7c;
  227. width: 50%;
  228. // margin: 30px auto;
  229. border-radius: 10px;
  230. display: flex;
  231. align-items: center;
  232. justify-content: center;
  233. }
  234. </style>