Feeding.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  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. $row = Db::name('feeding')->where('bach',$bach)->select();
  156. $res = Db::name('task')->where('bach',$bach)->order('create','desc')->select();
  157. //已有过工序
  158. if($row){
  159. $result['inspector'] = $row[0]['inspector'];
  160. $result['operator'] = $row[0]['operator'];
  161. }else{
  162. $result['inspector'] = '';
  163. $result['operator'] = '';
  164. }
  165. $result['data'] = $res;
  166. return json($result);
  167. }
  168. //获取配方信息
  169. public function get_formula(){
  170. $bach = $this->request->post('bach');//批次号
  171. $num = $this->request->post('num');//生产量
  172. $process = Db::name('feeding')->where('bach',$bach)->select();
  173. if($num){//如果有,批次号重复,需精确查找
  174. $res = Db::name('task')->alias('t')
  175. ->join('formula_detail f','f.pid=t.fid','left')
  176. ->field('f.material,f.percentage,f.gy_name,f.gy_num,t.number,t.id,t.kuodan')
  177. ->where('t.bach',$bach)->where('t.number',$num)->select();
  178. }else{//如果没有,,批次号未重复,直接差出数据
  179. $res = Db::name('task')->alias('t')
  180. ->join('formula_detail f','t.fid = f.pid','left')
  181. ->field('f.material,f.percentage,f.gy_name,f.gy_num,t.number,t.id,t.kuodan')
  182. ->where('t.bach',$bach)->select();
  183. }
  184. $pro = 0;
  185. //按照百分比计算出应投重量
  186. foreach($res as &$v){
  187. $v['weight']=0;
  188. $v['material_bach'] = '';
  189. if($process){ //已有工序,接上一次工序
  190. foreach ($process as $val){
  191. if(($val['material']==$v['material'] || in_array($val['material'],explode('/',$v['material']))) && $val['tid']==$v['id'] && $val['gy_num'] == $v['gy_num']){
  192. $v['weight']=$val['weight'];
  193. $v['material_bach']=$val['material_bach'];
  194. $pro = $val['gy_num'];
  195. }
  196. }
  197. }
  198. if($v['gy_name'] == null){
  199. $v['gy_name'] = '';
  200. }
  201. if($v['gy_num'] == null){
  202. $v['gy_num'] = '';
  203. }
  204. if($v['material'] == null){
  205. $v['material'] = '';
  206. }
  207. if($v['material_bach'] == null){
  208. $v['material_bach'] = '';
  209. }
  210. $total = array_column($res,'percentage');
  211. foreach ($total as $key=>$value){
  212. $total[$key] = decode($value);
  213. }
  214. $num = array_sum($total);
  215. if($v['kuodan']){
  216. $kd_sum = $v['number']+$v['kuodan'];
  217. if($v['percentage']){
  218. $number = ceil(decode($v['percentage']) / $num * $kd_sum *1000);
  219. $v['nweight'] = number_format($number/1000,3);
  220. }else{
  221. $v['nweight']='';
  222. }
  223. }else{
  224. if($v['percentage']){
  225. $number = ceil(decode($v['percentage']) / $num * $v['number'] *1000);
  226. $v['nweight'] = number_format($number/1000,3);
  227. }else{
  228. $v['nweight']='';
  229. }
  230. }
  231. }
  232. $row['total']=$num;
  233. $row['data'] = $res;
  234. $row['process'] = $pro+1;
  235. return json($row);
  236. }
  237. public function get_material(){
  238. $bach = $this->request->post('bach');//批次号
  239. $material = $this->request->post('material');//原材料
  240. if (!$bach || !$material){
  241. $this->error('扫码数据不能为空');
  242. }
  243. $task = Db::name('task')->where('bach',$bach)->find();
  244. $map = [];
  245. $map['f_name'] = $task['name'];
  246. $map['name'] = $material;
  247. $data = Db::name('formula_material')->where($map)->select();
  248. $result['data'] = '';
  249. if ($data){
  250. $result['data'] = $data;
  251. if (count($data)>1){
  252. $str = [];
  253. foreach ($data as $key=>$value){
  254. $str[$key] = $value['m_name'];
  255. }
  256. $result['string'] = implode('或 ',$str);
  257. }else{
  258. $result['string'] = $data[0]['m_name'];
  259. }
  260. $result['total'] = count($data);
  261. }else{
  262. $result['total'] = 0;
  263. }
  264. return json($result);
  265. }
  266. //查找替代料
  267. /*public function replace(){
  268. $bach = $this->request->post('bach');
  269. $wuliao = $this->request->post('wuliao');
  270. $res = Db::name('task')->alias('t')
  271. ->join('formula f','f.id = t.fid','left')
  272. ->join('formula_detail fd','fd.pid = f.id','left')
  273. ->join('formula_replace fr','fr.fid = fd.id','left')
  274. ->where('t.bach',$bach)->where('is_replace=1')->where('fr.material',$wuliao)
  275. ->field('fd.material,fd.id')->find();
  276. $res['yuan'] = explode('/',$res['material']);
  277. $key = array_search($wuliao, $res['yuan']);
  278. if ($key !== false) array_splice($res['yuan'], $key, 1);
  279. return json($res);
  280. }*/
  281. // //选择时获取电子秤数据
  282. // public function cheng(){
  283. //// $data = rand(1,100);return $data;
  284. // $serialPort = input('dzc');
  285. // if ($serialPort != '未选择') {
  286. // if (!empty($serialPort)) {
  287. // exec("mode $serialPort: baud=9600 data=8 stop=1 parity=n xon=on");
  288. // $ck = dio_open($serialPort, O_RDWR);
  289. // if (!$ck) {
  290. // die('打开失败');
  291. // }
  292. // do {
  293. // $shuju = dio_read($ck, 7);
  294. // } while ($shuju == null);
  295. // $shuju = floatval(strrev($shuju));
  296. //
  297. // if ($shuju != null) {
  298. // dio_write($ck, $shuju);
  299. // }
  300. //
  301. // dio_close($ck);
  302. // return $shuju;
  303. // }
  304. // } else {
  305. // // 如果 $serialPort 是 '未选择',则直接返回空字符串
  306. // return '';
  307. // }
  308. // }
  309. // public function cheng() {
  310. // // 获取串口列表
  311. // $serialPorts = ['COM3', 'COM4'];
  312. // $maxAttempts = 10; // 设置最大尝试次数
  313. // $dataFromPorts = [];
  314. // foreach ($serialPorts as $serialPort) {
  315. // exec("mode $serialPort: baud=9600 data=8 stop=1 parity=n xon=on");
  316. // $ck = dio_open($serialPort, O_RDWR);
  317. // if (!$ck) {
  318. // // 如果无法打开串口,记录错误并继续下一个串口
  319. // error_log("无法打开串口 $serialPort");
  320. // continue;
  321. // }
  322. // $attempts = 0;
  323. // while ($attempts < $maxAttempts) {
  324. // $shuju = dio_read($ck, 7);
  325. // if ($shuju !== null) {
  326. // // 将读取到的数据添加到数组中
  327. // $dataFromPorts[$serialPort] = floatval(strrev($shuju));
  328. // break; // 从当前串口读取到数据后跳出循环
  329. // }
  330. // sleep(1); // 等待一段时间再尝试读取
  331. // $attempts++;
  332. // }
  333. // dio_close($ck);
  334. //
  335. // }
  336. // echo "<pre>";
  337. // print_r($dataFromPorts);
  338. // echo "</pre>";die;
  339. //
  340. // return $dataFromPorts;
  341. // }
  342. //
  343. // //获取三位电子秤数据
  344. // public function cheng1() {
  345. //// $fixedDataScalePort中不一定是COM多少
  346. // $fixedDataScalePort = 'COM4';
  347. // $fixedData = $this->readFixedData($fixedDataScalePort);
  348. //echo "<pre>";
  349. //print_r($fixedData);
  350. //echo "<pre>";
  351. // //er 两位小数 san 三位小数
  352. // return [
  353. // 'san' => $fixedData,
  354. // ];
  355. // }
  356. //固定串口获取电子秤
  357. public function cheng21() {
  358. $maxAttempts = 10; // 设置最大尝试次数
  359. $serialPort = 'COM4';
  360. // 尝试打开串口
  361. exec("mode $serialPort: baud=9600 data=8 stop=1 parity=n xon=on");
  362. $ck = dio_open($serialPort, O_RDWR);
  363. if (!$ck) {
  364. // 如果无法打开串口,记录错误并返回默认值或者null
  365. error_log("无法打开串口 $serialPort");
  366. return null;
  367. }
  368. $attempts = 0;
  369. while ($attempts < $maxAttempts) {
  370. $shuju = dio_read($ck, 7); // 调整为适当的字节数
  371. // $data = floatval(strrev($shuju));
  372. // $weight = floatval(str_replace('kg', '', $data));
  373. echo "<pre>";
  374. print_r($shuju);
  375. echo "<pre>";
  376. if ($shuju !== null) {
  377. // 数据获取成功
  378. dio_close($ck);
  379. return $shuju;
  380. }
  381. sleep(1); // 调整为更短的等待时间
  382. $attempts++;
  383. }
  384. dio_close($ck);
  385. // 数据获取失败,返回默认值或者null
  386. return null;
  387. }
  388. // 多个串口获取有线电子秤三位数
  389. //机箱左边
  390. public function cheng4() {
  391. $maxAttempts = 10; // 设置最大尝试次数
  392. $serialPort = 'COM4';
  393. // 尝试打开串口
  394. exec("mode $serialPort: baud=9600 data=8 stop=1 parity=n xon=on");
  395. $ck = dio_open($serialPort, O_RDWR);
  396. if (!$ck) {
  397. // 如果无法打开串口,记录错误并返回默认值或者null
  398. error_log("无法打开串口 $serialPort");
  399. return null;
  400. }
  401. $attempts = 0;
  402. while ($attempts < $maxAttempts) {
  403. $shuju = dio_read($ck, 7); // 调整为适当的字节数
  404. // $data = floatval(strrev($shuju));
  405. // $weight = floatval(str_replace('kg', '', $data));
  406. echo "<pre>";
  407. print_r($shuju);
  408. echo "<pre>";
  409. if ($shuju !== null) {
  410. // 数据获取成功
  411. dio_close($ck);
  412. return $shuju;
  413. }
  414. sleep(1); // 调整为更短的等待时间
  415. $attempts++;
  416. }
  417. dio_close($ck);
  418. // 数据获取失败,返回默认值或者null
  419. return null;
  420. }
  421. //机箱右边
  422. public function cheng3() {
  423. $maxAttempts = 10; // 设置最大尝试次数
  424. $serialPort = 'COM3';
  425. // 尝试打开串口
  426. exec("mode $serialPort: baud=9600 data=8 stop=1 parity=n xon=on");
  427. $ck = dio_open($serialPort, O_RDWR);
  428. if (!$ck) {
  429. // 如果无法打开串口,记录错误并返回默认值或者null
  430. error_log("无法打开串口 $serialPort");
  431. return null;
  432. }
  433. $attempts = 0;
  434. while ($attempts < $maxAttempts) {
  435. $shuju = dio_read($ck, 7); // 调整为适当的字节数
  436. // $data = floatval(strrev($shuju));
  437. // $weight = floatval(str_replace('kg', '', $data));
  438. echo "<pre>";
  439. print_r($shuju);
  440. echo "<pre>";
  441. if ($shuju !== null) {
  442. // 数据获取成功
  443. dio_close($ck);
  444. return $shuju;
  445. }
  446. sleep(1); // 调整为更短的等待时间
  447. $attempts++;
  448. }
  449. dio_close($ck);
  450. // 数据获取失败,返回默认值或者null
  451. return null;
  452. }
  453. /**
  454. * 只获取一个固定串口
  455. */
  456. public function cheng() {
  457. $maxAttempts = 10; // 设置最大尝试次数
  458. // $serialPorts = $this->getSerialPorts(); // 获取所有可用的串口号
  459. // 执行mode命令并获取输出结果
  460. exec('mode', $output, $return);
  461. // 初始化一个变量来存放找到的串口
  462. $serialPort = null;
  463. // 遍历命令输出的每一行
  464. foreach ($output as $line) {
  465. // 使用正则表达式匹配串口设备
  466. if (preg_match('/COM(\d+)/', $line, $matches)) {
  467. // 检查串口号是否为1或2
  468. if ($matches[1] == '1' || $matches[1] == '2') {
  469. // 如果是COM1或COM2,则跳过
  470. continue;
  471. }
  472. // 找到匹配的串口号并返回
  473. $serialPort = $matches[0];
  474. break; // 停止搜索
  475. }
  476. }
  477. // 尝试打开串口
  478. exec("mode $serialPort: baud=9600 data=8 stop=1 parity=n xon=on");
  479. $ck = dio_open($serialPort, O_RDWR);
  480. if (!$ck) {
  481. // 如果无法打开串口,记录错误并返回默认值或者null
  482. error_log("无法打开串口 $serialPort");
  483. return null;
  484. }
  485. $attempts = 0;
  486. while ($attempts < $maxAttempts) {
  487. $shuju = dio_read($ck, 7); // 调整为适当的字节数
  488. // $data = floatval(strrev($shuju));
  489. // $weight = floatval(str_replace('kg', '', $data));
  490. if ($shuju !== null) {
  491. // 数据获取成功
  492. dio_close($ck);
  493. return $shuju;
  494. }
  495. sleep(1); // 调整为更短的等待时间
  496. $attempts++;
  497. }
  498. dio_close($ck);
  499. // 数据获取失败,返回默认值或者null
  500. return null;
  501. }
  502. public function getSerialPorts() {
  503. // 执行mode命令并获取输出结果
  504. exec('mode', $output, $return);
  505. // 初始化一个变量来存放找到的串口
  506. $serialPort = null;
  507. // 遍历命令输出的每一行
  508. foreach ($output as $line) {
  509. // 使用正则表达式匹配串口设备
  510. if (preg_match('/COM(\d+)/', $line, $matches)) {
  511. // 检查串口号是否为1或2
  512. if ($matches[1] == '1' || $matches[1] == '2') {
  513. // 如果是COM1或COM2,则跳过
  514. continue;
  515. }
  516. // 找到匹配的串口号并返回
  517. $serialPort = $matches[0];
  518. break; // 停止搜索
  519. }
  520. }
  521. // 返回找到的串口,如果没有找到则为null
  522. return $serialPort;
  523. }
  524. /**
  525. * 获取多个串口
  526. */
  527. public function dzcduo() {
  528. $maxAttempts = 10; // 设置最大尝试次数
  529. $serialPorts = $this->getchuankou(); // 获取所有可用的串口号
  530. foreach ($serialPorts as $COM) {
  531. // 尝试打开串口
  532. $ck = @dio_open($COM, O_RDWR | O_NONBLOCK);
  533. if ($ck === false) {
  534. // 如果无法打开串口,记录错误
  535. error_log("无法打开串口 $COM");
  536. continue; // 继续尝试下一个串口
  537. }
  538. // 成功打开串口,配置串口参数
  539. exec("mode $COM baud=9600 data=8 stop=1 parity=n xon=on");
  540. // ... 这里进行串口通信等操作 ...
  541. $attempts = 0;
  542. while ($attempts < $maxAttempts) {
  543. $shuju = dio_read($ck, 7); // 调整为适当的字节数
  544. if ($shuju !== false) { // 注意:dio_read()返回false时表示失败
  545. // 数据获取成功
  546. dio_close($ck);
  547. return $shuju;
  548. }
  549. sleep(1); // 调整为更短的等待时间
  550. $attempts++;
  551. }
  552. // 如果在尝试了最大次数后仍未能读取到数据,记录错误并跳过当前串口
  553. error_log("无法从串口 $COM 读取数据");
  554. dio_close($ck);
  555. }
  556. return null; // 如果所有串口都无法读取数据,则返回null
  557. }
  558. public function getchuankou() {
  559. // 执行mode命令并获取输出结果
  560. exec('mode', $output, $return);
  561. // 初始化一个数组来存放找到的串口
  562. $serialPorts = [];
  563. // 遍历命令输出的每一行
  564. foreach ($output as $line) {
  565. // 使用正则表达式匹配串口设备
  566. if (preg_match('/COM(\d+)/', $line, $matches)) {
  567. // 检查串口号是否为1或2
  568. if ($matches[1] == '1' || $matches[1] == '2') {
  569. // 如果是COM1或COM2,则跳过
  570. continue;
  571. }
  572. // 将匹配到的串口号添加到数组中
  573. $serialPorts[] = $matches[0];
  574. }
  575. }
  576. // 返回找到的串口列表
  577. return $serialPorts;
  578. }
  579. /**
  580. * 固定串口两位小数电子秤获取
  581. */
  582. public function cheng1() {
  583. $maxAttempts = 10; // 设置最大尝试次数
  584. $dataFromPorts = [];
  585. $serialPort = 'COM3';
  586. exec("mode $serialPort: baud=9600 data=8 stop=1 parity=n xon=on");
  587. $ck = dio_open($serialPort, O_RDWR);
  588. if (!$ck) {
  589. // 如果无法打开串口,记录错误并继续下一个串口
  590. error_log("无法打开串口 $serialPort");
  591. }
  592. $attempts = 0;
  593. while ($attempts < $maxAttempts) {
  594. $shuju = dio_read($ck, 7);
  595. if ($shuju !== null) {
  596. // 将读取到的数据添加到数组中
  597. $dataFromPorts[$serialPort] = floatval(strrev($shuju));
  598. break; // 从当前串口读取到数据后跳出循环
  599. }
  600. sleep(1); // 等待一段时间再尝试读取
  601. $attempts++;
  602. }
  603. dio_close($ck);
  604. return $dataFromPorts;
  605. }
  606. }