balanceAlert.vue 8.5 KB

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