Feeding.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Cache;
  5. use think\Db;
  6. use think\exception\PDOException;
  7. use think\exception\ValidateException;
  8. use think\Session;
  9. /**
  10. * 生产投料
  11. *
  12. * @icon fa fa-circle-o
  13. */
  14. class Feeding extends Backend
  15. {
  16. protected $searchFields = 'material,bach';
  17. /**
  18. * Feeding模型对象
  19. * @var \app\admin\model\Feeding
  20. */
  21. protected $model = null;
  22. protected $noNeedRight = ['get_formula','get_task','get_material'];
  23. public function _initialize()
  24. {
  25. parent::_initialize();
  26. $this->model = new \app\admin\model\Feeding;
  27. }
  28. /**
  29. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  30. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  31. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  32. */
  33. /**
  34. * 添加
  35. *
  36. * @return string
  37. * @throws \think\Exception
  38. */
  39. public function add()
  40. {
  41. if (false === $this->request->isPost()) {
  42. //车间=>操作人员
  43. $cjcz = Db::name('personnel')->where('bid',"=",5)->where('position','=',"cjcz")->order('name desc')->select();
  44. //车间=>检验人员
  45. $cjjy = Db::name('personnel')->where('bid',"=",5)->where('position','=',"cjjy")->order('name desc')->select();
  46. $this->assign('cjcz',$cjcz);
  47. $this->assign('cjjy',$cjjy);
  48. return $this->view->fetch();
  49. }
  50. $tid = Db::name('task')->where('bach',$_POST['bach'])->order('id desc')->find();
  51. if(empty($_POST['weight'])){
  52. $this->error('投料数据不能为空');
  53. }
  54. $feeding_no = Db::name('feeding')->where('bach',$_POST['bach'])->order('id desc')->find();
  55. if (isset($feeding_no['no'])) {
  56. // 确保 $feeding_no 是一个数组且 $feeding_no['no'] 已设置
  57. $param['no'] = $feeding_no['no'] + 1;
  58. } else {
  59. // $feeding_no 不存在,或者 $feeding_no['no'] 未设置
  60. $param['no'] = 1;
  61. }
  62. // $param['no'] = $fno['no'] +1;//操作顺序
  63. $param['tid'] = $tid['id'];//关联批次idx
  64. $param['bach'] = $_POST['bach'];//批次号
  65. $param['date'] = $_POST['date'];//日期
  66. $param['operator'] = $_POST['operator'];//操作人员
  67. $param['inspector'] = $_POST['inspector'];//检验人员
  68. $param['material'] = $_POST['material'];//原材料
  69. $param['nweight'] = $_POST['nweight'];//应投重量
  70. $param['gy_num'] = $_POST['gy_num'];//工艺序号
  71. $param['weight'] = $_POST['weight'];//投料重量
  72. $param['material_bach'] = $_POST['material_bach'];//原材料批次号
  73. $param['remark'] = $_POST['gy_name'];//操作记录
  74. $param['create'] = date('Y-m-d H:i:s');//创建时间
  75. $feeding = Db::name('feeding')->insert($param);
  76. if($feeding){return "添加成功";}else{return "添加失败";}
  77. // $params = $this->request->post('row/a');
  78. // $params = $this->preExcludeFields($params);
  79. // $tid = Db::name('task')->where('bach',$params['bach'])->order('id desc')->find();
  80. // $arr = [];
  81. // if(empty($params['weight'])){
  82. // $this->error('投料数据不能为空');
  83. // }
  84. // foreach($params['weight'] as $k=>$v){
  85. // if($v){
  86. // $param['bach'] = $params['bach'];
  87. // $param['date'] = $params['date'];
  88. // $param['operator'] = $params['operator'];
  89. // $param['inspector'] = $params['inspector'];
  90. // $param['weight'] = $params['weight'][$k];
  91. // $param['nweight'] = $params['nweight'][$k];
  92. // $param['material'] = $params['material'][$k];
  93. // $param['gy_num'] = $params['gy_num'][$k];
  94. // $param['tid'] = $tid['id'];
  95. // $param['create'] = date('Y-m-d H:i:s');
  96. // $arr[] = $param;
  97. // }
  98. // }
  99. // //去掉已经存入数据库的工艺数据
  100. // $list = array();
  101. // foreach ($arr as $key=>$value){
  102. // $map=[];
  103. // $map['bach'] = $value['bach'];
  104. // $map['material'] = $value['material'];
  105. // $map['gy_num'] = $value['gy_num'];
  106. // $is_feeding_gy = Db::name('feeding')->where($map)->order('id desc')->find();
  107. // if (!$is_feeding_gy){
  108. // $list[$key] = $value;
  109. // }
  110. // }
  111. // $result = false;
  112. // Db::startTrans();
  113. // try {
  114. // //是否采用模型验证
  115. // if ($this->modelValidate) {
  116. // $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  117. // $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  118. // $this->model->validateFailException()->validate($validate);
  119. // }
  120. //
  121. // $result = $this->model->allowField(true)->saveAll($list);
  122. // Db::commit();
  123. // } catch (ValidateException|PDOException|Exception $e) {
  124. // Db::rollback();
  125. // $this->error($e->getMessage());
  126. // }
  127. // if ($result === false) {
  128. // $this->error(__('No rows were inserted'));
  129. // }
  130. // $this->success();
  131. }
  132. //投料添加页面=>修改应投重量
  133. // public function submits(){
  134. // $param = $this->request->post();
  135. // $where = [];
  136. // $where['bach'] = $param['bach'];
  137. // $id = Db::name('feeding')->where($where)->select();
  138. // echo "<pre>";
  139. // print_r($id);
  140. // echo "</pre>";die;
  141. // $arr['weight'] = $param['weight'];
  142. // $feeding = Db::name('feeding')->where('id',$id['id'])->select();
  143. // echo "<pre>";
  144. // print_r($feeding);
  145. // echo "</pre>";die;
  146. // if(!$feeding){
  147. // return '修改失败';
  148. // }else{
  149. // return '修改成功';
  150. // }
  151. // }
  152. //获取作业票信息
  153. public function get_task(){
  154. $bach = $this->request->post('bach');
  155. $year = date('Y'); // 获取当前年份
  156. $row = Db::name('feeding')->where('bach',$bach)->select();
  157. $res = Db::name('task')->where('bach',$bach)->order('create','desc')->select();
  158. //已有过工序
  159. if($row){
  160. $result['inspector'] = $row[0]['inspector'];
  161. $result['operator'] = $row[0]['operator'];
  162. }else{
  163. $result['inspector'] = '';
  164. $result['operator'] = '';
  165. }
  166. $result['data'] = $res;
  167. return json($result);
  168. }
  169. //获取配方信息
  170. public function get_formula(){
  171. $bach = $this->request->post('bach');//批次号
  172. $num = $this->request->post('num');//生产量
  173. $process = Db::name('feeding')->where('bach',$bach)->select();
  174. if($num){//如果有,批次号重复,需精确查找
  175. $res = Db::name('task')->alias('t')
  176. ->join('formula_detail f','f.pid=t.fid','left')
  177. ->field('f.material,f.percentage,f.gy_name,f.gy_num,t.number,t.id,t.kuodan')
  178. ->where('t.bach',$bach)->where('t.number',$num)->select();
  179. }else{//如果没有,,批次号未重复,直接差出数据
  180. $res = Db::name('task')->alias('t')
  181. ->join('formula_detail f','t.fid = f.pid','left')
  182. ->field('f.material,f.percentage,f.gy_name,f.gy_num,t.number,t.id,t.kuodan')
  183. ->where('t.bach',$bach)->select();
  184. }
  185. $pro = 0;
  186. //按照百分比计算出应投重量
  187. foreach($res as &$v){
  188. $v['weight']=0;
  189. $v['material_bach'] = '';
  190. if($process){ //已有工序,接上一次工序
  191. foreach ($process as $val){
  192. if(($val['material']==$v['material'] || in_array($val['material'],explode('/',$v['material']))) && $val['tid']==$v['id'] && $val['gy_num'] == $v['gy_num']){
  193. $v['weight']=$val['weight'];
  194. $v['material_bach']=$val['material_bach'];
  195. $pro = $val['gy_num'];
  196. }
  197. }
  198. }
  199. if($v['gy_name'] == null){
  200. $v['gy_name'] = '';
  201. }
  202. if($v['gy_num'] == null){
  203. $v['gy_num'] = '';
  204. }
  205. if($v['material'] == null){
  206. $v['material'] = '';
  207. }
  208. if($v['material_bach'] == null){
  209. $v['material_bach'] = '';
  210. }
  211. $total = array_column($res,'percentage');
  212. foreach ($total as $key=>$value){
  213. $total[$key] = decode($value);
  214. }
  215. $num = array_sum($total);
  216. if($v['kuodan']){
  217. $kd_sum = $v['number']+$v['kuodan'];
  218. if($v['percentage']){
  219. $number = ceil(decode($v['percentage']) / $num * $kd_sum *1000);
  220. $v['nweight'] = number_format($number/1000,3);
  221. }else{
  222. $v['nweight']='';
  223. }
  224. }else{
  225. if($v['percentage']){
  226. $number = ceil(decode($v['percentage']) / $num * $v['number'] *1000);
  227. $v['nweight'] = number_format($number/1000,3);
  228. }else{
  229. $v['nweight']='';
  230. }
  231. }
  232. }
  233. $row['total']=$num;
  234. $row['data'] = $res;
  235. $row['process'] = $pro+1;
  236. return json($row);
  237. }
  238. public function get_material(){
  239. $bach = $this->request->post('bach');//批次号
  240. $material = $this->request->post('material');//原材料
  241. if (!$bach || !$material){
  242. $this->error('扫码数据不能为空');
  243. }
  244. $task = Db::name('task')->where('bach',$bach)->find();
  245. $map = [];
  246. $map['f_name'] = $task['name'];
  247. $map['name'] = $material;
  248. $data = Db::name('formula_material')->where($map)->select();
  249. $result['data'] = '';
  250. if ($data){
  251. $result['data'] = $data;
  252. if (count($data)>1){
  253. $str = [];
  254. foreach ($data as $key=>$value){
  255. $str[$key] = $value['m_name'];
  256. }
  257. $result['string'] = implode('或 ',$str);
  258. }else{
  259. $result['string'] = $data[0]['m_name'];
  260. }
  261. $result['total'] = count($data);
  262. }else{
  263. $result['total'] = 0;
  264. }
  265. return json($result);
  266. }
  267. //查找替代料
  268. /*public function replace(){
  269. $bach = $this->request->post('bach');
  270. $wuliao = $this->request->post('wuliao');
  271. $res = Db::name('task')->alias('t')
  272. ->join('formula f','f.id = t.fid','left')
  273. ->join('formula_detail fd','fd.pid = f.id','left')
  274. ->join('formula_replace fr','fr.fid = fd.id','left')
  275. ->where('t.bach',$bach)->where('is_replace=1')->where('fr.material',$wuliao)
  276. ->field('fd.material,fd.id')->find();
  277. $res['yuan'] = explode('/',$res['material']);
  278. $key = array_search($wuliao, $res['yuan']);
  279. if ($key !== false) array_splice($res['yuan'], $key, 1);
  280. return json($res);
  281. }*/
  282. // //选择时获取电子秤数据
  283. // public function cheng(){
  284. //// $data = rand(1,100);return $data;
  285. // $serialPort = input('dzc');
  286. // if ($serialPort != '未选择') {
  287. // if (!empty($serialPort)) {
  288. // exec("mode $serialPort: baud=9600 data=8 stop=1 parity=n xon=on");
  289. // $ck = dio_open($serialPort, O_RDWR);
  290. // if (!$ck) {
  291. // die('打开失败');
  292. // }
  293. // do {
  294. // $shuju = dio_read($ck, 7);
  295. // } while ($shuju == null);
  296. // $shuju = floatval(strrev($shuju));
  297. //
  298. // if ($shuju != null) {
  299. // dio_write($ck, $shuju);
  300. // }
  301. //
  302. // dio_close($ck);
  303. // return $shuju;
  304. // }
  305. // } else {
  306. // // 如果 $serialPort 是 '未选择',则直接返回空字符串
  307. // return '';
  308. // }
  309. // }
  310. // public function cheng() {
  311. // // 获取串口列表
  312. // $serialPorts = ['COM3', 'COM4'];
  313. // $maxAttempts = 10; // 设置最大尝试次数
  314. // $dataFromPorts = [];
  315. // foreach ($serialPorts as $serialPort) {
  316. // exec("mode $serialPort: baud=9600 data=8 stop=1 parity=n xon=on");
  317. // $ck = dio_open($serialPort, O_RDWR);
  318. // if (!$ck) {
  319. // // 如果无法打开串口,记录错误并继续下一个串口
  320. // error_log("无法打开串口 $serialPort");
  321. // continue;
  322. // }
  323. // $attempts = 0;
  324. // while ($attempts < $maxAttempts) {
  325. // $shuju = dio_read($ck, 7);
  326. // if ($shuju !== null) {
  327. // // 将读取到的数据添加到数组中
  328. // $dataFromPorts[$serialPort] = floatval(strrev($shuju));
  329. // break; // 从当前串口读取到数据后跳出循环
  330. // }
  331. // sleep(1); // 等待一段时间再尝试读取
  332. // $attempts++;
  333. // }
  334. // dio_close($ck);
  335. //
  336. // }
  337. // echo "<pre>";
  338. // print_r($dataFromPorts);
  339. // echo "</pre>";die;
  340. //
  341. // return $dataFromPorts;
  342. // }
  343. //
  344. // //获取三位电子秤数据
  345. // public function cheng1() {
  346. //// $fixedDataScalePort中不一定是COM多少
  347. // $fixedDataScalePort = 'COM4';
  348. // $fixedData = $this->readFixedData($fixedDataScalePort);
  349. //echo "<pre>";
  350. //print_r($fixedData);
  351. //echo "<pre>";
  352. // //er 两位小数 san 三位小数
  353. // return [
  354. // 'san' => $fixedData,
  355. // ];
  356. // }
  357. //固定串口获取电子秤
  358. public function cheng21() {
  359. $maxAttempts = 10; // 设置最大尝试次数
  360. $serialPort = 'COM4';
  361. // 尝试打开串口
  362. exec("mode $serialPort: baud=9600 data=8 stop=1 parity=n xon=on");
  363. $ck = dio_open($serialPort, O_RDWR);
  364. if (!$ck) {
  365. // 如果无法打开串口,记录错误并返回默认值或者null
  366. error_log("无法打开串口 $serialPort");
  367. return null;
  368. }
  369. $attempts = 0;
  370. while ($attempts < $maxAttempts) {
  371. $shuju = dio_read($ck, 7); // 调整为适当的字节数
  372. // $data = floatval(strrev($shuju));
  373. // $weight = floatval(str_replace('kg', '', $data));
  374. echo "<pre>";
  375. print_r($shuju);
  376. echo "<pre>";
  377. if ($shuju !== null) {
  378. // 数据获取成功
  379. dio_close($ck);
  380. return $shuju;
  381. }
  382. sleep(1); // 调整为更短的等待时间
  383. $attempts++;
  384. }
  385. dio_close($ck);
  386. // 数据获取失败,返回默认值或者null
  387. return null;
  388. }
  389. // 多个串口获取有线电子秤三位数
  390. //机箱左边
  391. public function cheng4() {
  392. $maxAttempts = 10; // 设置最大尝试次数
  393. $serialPort = 'COM4';
  394. // 尝试打开串口
  395. exec("mode $serialPort: baud=9600 data=8 stop=1 parity=n xon=on");
  396. $ck = dio_open($serialPort, O_RDWR);
  397. if (!$ck) {
  398. // 如果无法打开串口,记录错误并返回默认值或者null
  399. error_log("无法打开串口 $serialPort");
  400. return null;
  401. }
  402. $attempts = 0;
  403. while ($attempts < $maxAttempts) {
  404. $shuju = dio_read($ck, 7); // 调整为适当的字节数
  405. // $data = floatval(strrev($shuju));
  406. // $weight = floatval(str_replace('kg', '', $data));
  407. echo "<pre>";
  408. print_r($shuju);
  409. echo "<pre>";
  410. if ($shuju !== null) {
  411. // 数据获取成功
  412. dio_close($ck);
  413. return $shuju;
  414. }
  415. sleep(1); // 调整为更短的等待时间
  416. $attempts++;
  417. }
  418. dio_close($ck);
  419. // 数据获取失败,返回默认值或者null
  420. return null;
  421. }
  422. //机箱右边
  423. public function cheng3() {
  424. $maxAttempts = 10; // 设置最大尝试次数
  425. $serialPort = 'COM3';
  426. // 尝试打开串口
  427. exec("mode $serialPort: baud=9600 data=8 stop=1 parity=n xon=on");
  428. $ck = dio_open($serialPort, O_RDWR);
  429. if (!$ck) {
  430. // 如果无法打开串口,记录错误并返回默认值或者null
  431. error_log("无法打开串口 $serialPort");
  432. return null;
  433. }
  434. $attempts = 0;
  435. while ($attempts < $maxAttempts) {
  436. $shuju = dio_read($ck, 7); // 调整为适当的字节数
  437. // $data = floatval(strrev($shuju));
  438. // $weight = floatval(str_replace('kg', '', $data));
  439. echo "<pre>";
  440. print_r($shuju);
  441. echo "<pre>";
  442. if ($shuju !== null) {
  443. // 数据获取成功
  444. dio_close($ck);
  445. return $shuju;
  446. }
  447. sleep(1); // 调整为更短的等待时间
  448. $attempts++;
  449. }
  450. dio_close($ck);
  451. // 数据获取失败,返回默认值或者null
  452. return null;
  453. }
  454. /**
  455. * 只获取一个固定串口
  456. */
  457. public function cheng() {
  458. $maxAttempts = 10; // 设置最大尝试次数
  459. // $serialPorts = $this->getSerialPorts(); // 获取所有可用的串口号
  460. // 执行mode命令并获取输出结果
  461. exec('mode', $output, $return);
  462. // 初始化一个变量来存放找到的串口
  463. $serialPort = null;
  464. // 遍历命令输出的每一行
  465. foreach ($output as $line) {
  466. // 使用正则表达式匹配串口设备
  467. if (preg_match('/COM(\d+)/', $line, $matches)) {
  468. // 检查串口号是否为1或2
  469. if ($matches[1] == '1' || $matches[1] == '2') {
  470. // 如果是COM1或COM2,则跳过
  471. continue;
  472. }
  473. // 找到匹配的串口号并返回
  474. $serialPort = $matches[0];
  475. break; // 停止搜索
  476. }
  477. }
  478. // 尝试打开串口
  479. exec("mode $serialPort: baud=9600 data=8 stop=1 parity=n xon=on");
  480. $ck = dio_open($serialPort, O_RDWR);
  481. if (!$ck) {
  482. // 如果无法打开串口,记录错误并返回默认值或者null
  483. error_log("无法打开串口 $serialPort");
  484. return null;
  485. }
  486. $attempts = 0;
  487. while ($attempts < $maxAttempts) {
  488. $shuju = dio_read($ck, 7); // 调整为适当的字节数
  489. // $data = floatval(strrev($shuju));
  490. // $weight = floatval(str_replace('kg', '', $data));
  491. if ($shuju !== null) {
  492. // 数据获取成功
  493. dio_close($ck);
  494. return $shuju;
  495. }
  496. sleep(1); // 调整为更短的等待时间
  497. $attempts++;
  498. }
  499. dio_close($ck);
  500. // 数据获取失败,返回默认值或者null
  501. return null;
  502. }
  503. public function getSerialPorts() {
  504. // 执行mode命令并获取输出结果
  505. exec('mode', $output, $return);
  506. // 初始化一个变量来存放找到的串口
  507. $serialPort = null;
  508. // 遍历命令输出的每一行
  509. foreach ($output as $line) {
  510. // 使用正则表达式匹配串口设备
  511. if (preg_match('/COM(\d+)/', $line, $matches)) {
  512. // 检查串口号是否为1或2
  513. if ($matches[1] == '1' || $matches[1] == '2') {
  514. // 如果是COM1或COM2,则跳过
  515. continue;
  516. }
  517. // 找到匹配的串口号并返回
  518. $serialPort = $matches[0];
  519. break; // 停止搜索
  520. }
  521. }
  522. // 返回找到的串口,如果没有找到则为null
  523. return $serialPort;
  524. }
  525. /**
  526. * 获取多个串口
  527. */
  528. public function dzcduo() {
  529. $maxAttempts = 10; // 设置最大尝试次数
  530. $serialPorts = $this->getchuankou(); // 获取所有可用的串口号
  531. foreach ($serialPorts as $COM) {
  532. // 尝试打开串口
  533. $ck = @dio_open($COM, O_RDWR | O_NONBLOCK);
  534. if ($ck === false) {
  535. // 如果无法打开串口,记录错误
  536. error_log("无法打开串口 $COM");
  537. continue; // 继续尝试下一个串口
  538. }
  539. // 成功打开串口,配置串口参数
  540. exec("mode $COM baud=9600 data=8 stop=1 parity=n xon=on");
  541. // ... 这里进行串口通信等操作 ...
  542. $attempts = 0;
  543. while ($attempts < $maxAttempts) {
  544. $shuju = dio_read($ck, 7); // 调整为适当的字节数
  545. if ($shuju !== false) { // 注意:dio_read()返回false时表示失败
  546. // 数据获取成功
  547. dio_close($ck);
  548. return $shuju;
  549. }
  550. sleep(1); // 调整为更短的等待时间
  551. $attempts++;
  552. }
  553. // 如果在尝试了最大次数后仍未能读取到数据,记录错误并跳过当前串口
  554. error_log("无法从串口 $COM 读取数据");
  555. dio_close($ck);
  556. }
  557. return null; // 如果所有串口都无法读取数据,则返回null
  558. }
  559. public function getchuankou() {
  560. // 执行mode命令并获取输出结果
  561. exec('mode', $output, $return);
  562. // 初始化一个数组来存放找到的串口
  563. $serialPorts = [];
  564. // 遍历命令输出的每一行
  565. foreach ($output as $line) {
  566. // 使用正则表达式匹配串口设备
  567. if (preg_match('/COM(\d+)/', $line, $matches)) {
  568. // 检查串口号是否为1或2
  569. if ($matches[1] == '1' || $matches[1] == '2') {
  570. // 如果是COM1或COM2,则跳过
  571. continue;
  572. }
  573. // 将匹配到的串口号添加到数组中
  574. $serialPorts[] = $matches[0];
  575. }
  576. }
  577. // 返回找到的串口列表
  578. return $serialPorts;
  579. }
  580. /**
  581. * 固定串口两位小数电子秤获取
  582. */
  583. public function cheng1() {
  584. $maxAttempts = 10; // 设置最大尝试次数
  585. $dataFromPorts = [];
  586. $serialPort = 'COM3';
  587. exec("mode $serialPort: baud=9600 data=8 stop=1 parity=n xon=on");
  588. $ck = dio_open($serialPort, O_RDWR);
  589. if (!$ck) {
  590. // 如果无法打开串口,记录错误并继续下一个串口
  591. error_log("无法打开串口 $serialPort");
  592. }
  593. $attempts = 0;
  594. while ($attempts < $maxAttempts) {
  595. $shuju = dio_read($ck, 7);
  596. if ($shuju !== null) {
  597. // 将读取到的数据添加到数组中
  598. $dataFromPorts[$serialPort] = floatval(strrev($shuju));
  599. break; // 从当前串口读取到数据后跳出循环
  600. }
  601. sleep(1); // 等待一段时间再尝试读取
  602. $attempts++;
  603. }
  604. dio_close($ck);
  605. return $dataFromPorts;
  606. }
  607. }