|
|
@@ -57,23 +57,32 @@ class Feeding extends Backend
|
|
|
return $this->view->fetch();
|
|
|
}
|
|
|
$tid = Db::name('task')->where('bach',$_POST['bach'])->order('id desc')->find();
|
|
|
+
|
|
|
if(empty($_POST['weight'])){
|
|
|
$this->error('投料数据不能为空');
|
|
|
}
|
|
|
- $fno = Db::name('feeding')->where('bach',$_POST['bach'])->order('id desc')->find();
|
|
|
- $param['no'] = $fno['no'] +1;//操作顺序
|
|
|
- $param['tid'] = $tid['id'];//关联批次id
|
|
|
+ $feeding_no = Db::name('feeding')->where('bach',$_POST['bach'])->order('id desc')->find();
|
|
|
+ if (isset($feeding_no['no'])) {
|
|
|
+ // 确保 $feeding_no 是一个数组且 $feeding_no['no'] 已设置
|
|
|
+ $param['no'] = $feeding_no['no'] + 1;
|
|
|
+ } else {
|
|
|
+ // $feeding_no 不存在,或者 $feeding_no['no'] 未设置
|
|
|
+ $param['no'] = 1;
|
|
|
+ }
|
|
|
+// $param['no'] = $fno['no'] +1;//操作顺序
|
|
|
+ $param['tid'] = $tid['id'];//关联批次idx
|
|
|
$param['bach'] = $_POST['bach'];//批次号
|
|
|
$param['date'] = $_POST['date'];//日期
|
|
|
$param['operator'] = $_POST['operator'];//操作人员
|
|
|
$param['inspector'] = $_POST['inspector'];//检验人员
|
|
|
$param['material'] = $_POST['material'];//原材料
|
|
|
- $param['gy_num'] = $_POST['gy_num'];//工艺序号
|
|
|
$param['nweight'] = $_POST['nweight'];//应投重量
|
|
|
+ $param['gy_num'] = $_POST['gy_num'];//工艺序号
|
|
|
$param['weight'] = $_POST['weight'];//投料重量
|
|
|
$param['material_bach'] = $_POST['material_bach'];//原材料批次号
|
|
|
- $param['remark'] = $_POST['gy_name'];//操作记录(记录批次号)
|
|
|
+ $param['remark'] = $_POST['gy_name'];//操作记录
|
|
|
$param['create'] = date('Y-m-d H:i:s');//创建时间
|
|
|
+
|
|
|
$feeding = Db::name('feeding')->insert($param);
|
|
|
if($feeding){return "添加成功";}else{return "添加失败";}
|
|
|
// $params = $this->request->post('row/a');
|
|
|
@@ -287,4 +296,355 @@ class Feeding extends Backend
|
|
|
return json($res);
|
|
|
}*/
|
|
|
|
|
|
+
|
|
|
+
|
|
|
+// //选择时获取电子秤数据
|
|
|
+
|
|
|
+// public function cheng(){
|
|
|
+//// $data = rand(1,100);return $data;
|
|
|
+// $serialPort = input('dzc');
|
|
|
+// if ($serialPort != '未选择') {
|
|
|
+// if (!empty($serialPort)) {
|
|
|
+// exec("mode $serialPort: baud=9600 data=8 stop=1 parity=n xon=on");
|
|
|
+// $ck = dio_open($serialPort, O_RDWR);
|
|
|
+// if (!$ck) {
|
|
|
+// die('打开失败');
|
|
|
+// }
|
|
|
+// do {
|
|
|
+// $shuju = dio_read($ck, 7);
|
|
|
+// } while ($shuju == null);
|
|
|
+// $shuju = floatval(strrev($shuju));
|
|
|
+//
|
|
|
+// if ($shuju != null) {
|
|
|
+// dio_write($ck, $shuju);
|
|
|
+// }
|
|
|
+//
|
|
|
+// dio_close($ck);
|
|
|
+// return $shuju;
|
|
|
+// }
|
|
|
+// } else {
|
|
|
+// // 如果 $serialPort 是 '未选择',则直接返回空字符串
|
|
|
+// return '';
|
|
|
+// }
|
|
|
+// }
|
|
|
+// public function cheng() {
|
|
|
+// // 获取串口列表
|
|
|
+// $serialPorts = ['COM3', 'COM4'];
|
|
|
+// $maxAttempts = 10; // 设置最大尝试次数
|
|
|
+// $dataFromPorts = [];
|
|
|
+// foreach ($serialPorts as $serialPort) {
|
|
|
+// exec("mode $serialPort: baud=9600 data=8 stop=1 parity=n xon=on");
|
|
|
+// $ck = dio_open($serialPort, O_RDWR);
|
|
|
+// if (!$ck) {
|
|
|
+// // 如果无法打开串口,记录错误并继续下一个串口
|
|
|
+// error_log("无法打开串口 $serialPort");
|
|
|
+// continue;
|
|
|
+// }
|
|
|
+// $attempts = 0;
|
|
|
+// while ($attempts < $maxAttempts) {
|
|
|
+// $shuju = dio_read($ck, 7);
|
|
|
+// if ($shuju !== null) {
|
|
|
+// // 将读取到的数据添加到数组中
|
|
|
+// $dataFromPorts[$serialPort] = floatval(strrev($shuju));
|
|
|
+// break; // 从当前串口读取到数据后跳出循环
|
|
|
+// }
|
|
|
+// sleep(1); // 等待一段时间再尝试读取
|
|
|
+// $attempts++;
|
|
|
+// }
|
|
|
+// dio_close($ck);
|
|
|
+//
|
|
|
+// }
|
|
|
+// echo "<pre>";
|
|
|
+// print_r($dataFromPorts);
|
|
|
+// echo "</pre>";die;
|
|
|
+//
|
|
|
+// return $dataFromPorts;
|
|
|
+// }
|
|
|
+//
|
|
|
+// //获取三位电子秤数据
|
|
|
+// public function cheng1() {
|
|
|
+//// $fixedDataScalePort中不一定是COM多少
|
|
|
+// $fixedDataScalePort = 'COM4';
|
|
|
+// $fixedData = $this->readFixedData($fixedDataScalePort);
|
|
|
+//echo "<pre>";
|
|
|
+//print_r($fixedData);
|
|
|
+//echo "<pre>";
|
|
|
+// //er 两位小数 san 三位小数
|
|
|
+// return [
|
|
|
+// 'san' => $fixedData,
|
|
|
+// ];
|
|
|
+// }
|
|
|
+
|
|
|
+
|
|
|
+ //固定串口获取电子秤
|
|
|
+
|
|
|
+ public function cheng21() {
|
|
|
+ $maxAttempts = 10; // 设置最大尝试次数
|
|
|
+ $serialPort = 'COM4';
|
|
|
+ // 尝试打开串口
|
|
|
+ exec("mode $serialPort: baud=9600 data=8 stop=1 parity=n xon=on");
|
|
|
+ $ck = dio_open($serialPort, O_RDWR);
|
|
|
+ if (!$ck) {
|
|
|
+ // 如果无法打开串口,记录错误并返回默认值或者null
|
|
|
+ error_log("无法打开串口 $serialPort");
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ $attempts = 0;
|
|
|
+ while ($attempts < $maxAttempts) {
|
|
|
+ $shuju = dio_read($ck, 7); // 调整为适当的字节数
|
|
|
+// $data = floatval(strrev($shuju));
|
|
|
+// $weight = floatval(str_replace('kg', '', $data));
|
|
|
+ echo "<pre>";
|
|
|
+ print_r($shuju);
|
|
|
+ echo "<pre>";
|
|
|
+
|
|
|
+ if ($shuju !== null) {
|
|
|
+ // 数据获取成功
|
|
|
+
|
|
|
+ dio_close($ck);
|
|
|
+ return $shuju;
|
|
|
+ }
|
|
|
+ sleep(1); // 调整为更短的等待时间
|
|
|
+ $attempts++;
|
|
|
+ }
|
|
|
+ dio_close($ck);
|
|
|
+ // 数据获取失败,返回默认值或者null
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+// 多个串口获取有线电子秤三位数
|
|
|
+
|
|
|
+ //机箱左边
|
|
|
+ public function cheng4() {
|
|
|
+ $maxAttempts = 10; // 设置最大尝试次数
|
|
|
+ $serialPort = 'COM4';
|
|
|
+ // 尝试打开串口
|
|
|
+ exec("mode $serialPort: baud=9600 data=8 stop=1 parity=n xon=on");
|
|
|
+ $ck = dio_open($serialPort, O_RDWR);
|
|
|
+ if (!$ck) {
|
|
|
+ // 如果无法打开串口,记录错误并返回默认值或者null
|
|
|
+ error_log("无法打开串口 $serialPort");
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ $attempts = 0;
|
|
|
+ while ($attempts < $maxAttempts) {
|
|
|
+ $shuju = dio_read($ck, 7); // 调整为适当的字节数
|
|
|
+// $data = floatval(strrev($shuju));
|
|
|
+// $weight = floatval(str_replace('kg', '', $data));
|
|
|
+ echo "<pre>";
|
|
|
+ print_r($shuju);
|
|
|
+ echo "<pre>";
|
|
|
+
|
|
|
+ if ($shuju !== null) {
|
|
|
+ // 数据获取成功
|
|
|
+
|
|
|
+ dio_close($ck);
|
|
|
+ return $shuju;
|
|
|
+ }
|
|
|
+ sleep(1); // 调整为更短的等待时间
|
|
|
+ $attempts++;
|
|
|
+ }
|
|
|
+ dio_close($ck);
|
|
|
+ // 数据获取失败,返回默认值或者null
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ //机箱右边
|
|
|
+ public function cheng3() {
|
|
|
+ $maxAttempts = 10; // 设置最大尝试次数
|
|
|
+ $serialPort = 'COM3';
|
|
|
+ // 尝试打开串口
|
|
|
+ exec("mode $serialPort: baud=9600 data=8 stop=1 parity=n xon=on");
|
|
|
+ $ck = dio_open($serialPort, O_RDWR);
|
|
|
+ if (!$ck) {
|
|
|
+ // 如果无法打开串口,记录错误并返回默认值或者null
|
|
|
+ error_log("无法打开串口 $serialPort");
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ $attempts = 0;
|
|
|
+ while ($attempts < $maxAttempts) {
|
|
|
+ $shuju = dio_read($ck, 7); // 调整为适当的字节数
|
|
|
+// $data = floatval(strrev($shuju));
|
|
|
+// $weight = floatval(str_replace('kg', '', $data));
|
|
|
+ echo "<pre>";
|
|
|
+ print_r($shuju);
|
|
|
+ echo "<pre>";
|
|
|
+
|
|
|
+ if ($shuju !== null) {
|
|
|
+ // 数据获取成功
|
|
|
+
|
|
|
+ dio_close($ck);
|
|
|
+ return $shuju;
|
|
|
+ }
|
|
|
+ sleep(1); // 调整为更短的等待时间
|
|
|
+ $attempts++;
|
|
|
+ }
|
|
|
+ dio_close($ck);
|
|
|
+ // 数据获取失败,返回默认值或者null
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 只获取一个固定串口
|
|
|
+ */
|
|
|
+ public function cheng() {
|
|
|
+ $maxAttempts = 10; // 设置最大尝试次数
|
|
|
+// $serialPorts = $this->getSerialPorts(); // 获取所有可用的串口号
|
|
|
+ // 执行mode命令并获取输出结果
|
|
|
+ exec('mode', $output, $return);
|
|
|
+ // 初始化一个变量来存放找到的串口
|
|
|
+ $serialPort = null;
|
|
|
+ // 遍历命令输出的每一行
|
|
|
+ foreach ($output as $line) {
|
|
|
+ // 使用正则表达式匹配串口设备
|
|
|
+ if (preg_match('/COM(\d+)/', $line, $matches)) {
|
|
|
+
|
|
|
+ // 检查串口号是否为1或2
|
|
|
+ if ($matches[1] == '1' || $matches[1] == '2') {
|
|
|
+ // 如果是COM1或COM2,则跳过
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ // 找到匹配的串口号并返回
|
|
|
+ $serialPort = $matches[0];
|
|
|
+ break; // 停止搜索
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 尝试打开串口
|
|
|
+ exec("mode $serialPort: baud=9600 data=8 stop=1 parity=n xon=on");
|
|
|
+ $ck = dio_open($serialPort, O_RDWR);
|
|
|
+ if (!$ck) {
|
|
|
+ // 如果无法打开串口,记录错误并返回默认值或者null
|
|
|
+ error_log("无法打开串口 $serialPort");
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ $attempts = 0;
|
|
|
+ while ($attempts < $maxAttempts) {
|
|
|
+ $shuju = dio_read($ck, 7); // 调整为适当的字节数
|
|
|
+// $data = floatval(strrev($shuju));
|
|
|
+// $weight = floatval(str_replace('kg', '', $data));
|
|
|
+
|
|
|
+ if ($shuju !== null) {
|
|
|
+ // 数据获取成功
|
|
|
+
|
|
|
+ dio_close($ck);
|
|
|
+ return $shuju;
|
|
|
+ }
|
|
|
+ sleep(1); // 调整为更短的等待时间
|
|
|
+ $attempts++;
|
|
|
+ }
|
|
|
+ dio_close($ck);
|
|
|
+ // 数据获取失败,返回默认值或者null
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ public function getSerialPorts() {
|
|
|
+ // 执行mode命令并获取输出结果
|
|
|
+ exec('mode', $output, $return);
|
|
|
+ // 初始化一个变量来存放找到的串口
|
|
|
+ $serialPort = null;
|
|
|
+ // 遍历命令输出的每一行
|
|
|
+ foreach ($output as $line) {
|
|
|
+ // 使用正则表达式匹配串口设备
|
|
|
+ if (preg_match('/COM(\d+)/', $line, $matches)) {
|
|
|
+ // 检查串口号是否为1或2
|
|
|
+ if ($matches[1] == '1' || $matches[1] == '2') {
|
|
|
+ // 如果是COM1或COM2,则跳过
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ // 找到匹配的串口号并返回
|
|
|
+ $serialPort = $matches[0];
|
|
|
+ break; // 停止搜索
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 返回找到的串口,如果没有找到则为null
|
|
|
+ return $serialPort;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取多个串口
|
|
|
+ */
|
|
|
+ public function dzcduo() {
|
|
|
+ $maxAttempts = 10; // 设置最大尝试次数
|
|
|
+ $serialPorts = $this->getchuankou(); // 获取所有可用的串口号
|
|
|
+ foreach ($serialPorts as $COM) {
|
|
|
+ // 尝试打开串口
|
|
|
+ $ck = @dio_open($COM, O_RDWR | O_NONBLOCK);
|
|
|
+ if ($ck === false) {
|
|
|
+ // 如果无法打开串口,记录错误
|
|
|
+ error_log("无法打开串口 $COM");
|
|
|
+ continue; // 继续尝试下一个串口
|
|
|
+ }
|
|
|
+ // 成功打开串口,配置串口参数
|
|
|
+ exec("mode $COM baud=9600 data=8 stop=1 parity=n xon=on");
|
|
|
+ // ... 这里进行串口通信等操作 ...
|
|
|
+ $attempts = 0;
|
|
|
+ while ($attempts < $maxAttempts) {
|
|
|
+ $shuju = dio_read($ck, 7); // 调整为适当的字节数
|
|
|
+ if ($shuju !== false) { // 注意:dio_read()返回false时表示失败
|
|
|
+ // 数据获取成功
|
|
|
+ dio_close($ck);
|
|
|
+ return $shuju;
|
|
|
+ }
|
|
|
+ sleep(1); // 调整为更短的等待时间
|
|
|
+ $attempts++;
|
|
|
+ }
|
|
|
+ // 如果在尝试了最大次数后仍未能读取到数据,记录错误并跳过当前串口
|
|
|
+ error_log("无法从串口 $COM 读取数据");
|
|
|
+ dio_close($ck);
|
|
|
+ }
|
|
|
+ return null; // 如果所有串口都无法读取数据,则返回null
|
|
|
+ }
|
|
|
+ public function getchuankou() {
|
|
|
+ // 执行mode命令并获取输出结果
|
|
|
+ exec('mode', $output, $return);
|
|
|
+ // 初始化一个数组来存放找到的串口
|
|
|
+ $serialPorts = [];
|
|
|
+
|
|
|
+ // 遍历命令输出的每一行
|
|
|
+ foreach ($output as $line) {
|
|
|
+ // 使用正则表达式匹配串口设备
|
|
|
+ if (preg_match('/COM(\d+)/', $line, $matches)) {
|
|
|
+ // 检查串口号是否为1或2
|
|
|
+ if ($matches[1] == '1' || $matches[1] == '2') {
|
|
|
+ // 如果是COM1或COM2,则跳过
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ // 将匹配到的串口号添加到数组中
|
|
|
+ $serialPorts[] = $matches[0];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 返回找到的串口列表
|
|
|
+ return $serialPorts;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 固定串口两位小数电子秤获取
|
|
|
+ */
|
|
|
+ public function cheng1() {
|
|
|
+ $maxAttempts = 10; // 设置最大尝试次数
|
|
|
+ $dataFromPorts = [];
|
|
|
+ $serialPort = 'COM3';
|
|
|
+ exec("mode $serialPort: baud=9600 data=8 stop=1 parity=n xon=on");
|
|
|
+ $ck = dio_open($serialPort, O_RDWR);
|
|
|
+ if (!$ck) {
|
|
|
+ // 如果无法打开串口,记录错误并继续下一个串口
|
|
|
+ error_log("无法打开串口 $serialPort");
|
|
|
+ }
|
|
|
+ $attempts = 0;
|
|
|
+ while ($attempts < $maxAttempts) {
|
|
|
+ $shuju = dio_read($ck, 7);
|
|
|
+ if ($shuju !== null) {
|
|
|
+ // 将读取到的数据添加到数组中
|
|
|
+ $dataFromPorts[$serialPort] = floatval(strrev($shuju));
|
|
|
+ break; // 从当前串口读取到数据后跳出循环
|
|
|
+ }
|
|
|
+ sleep(1); // 等待一段时间再尝试读取
|
|
|
+ $attempts++;
|
|
|
+ }
|
|
|
+ dio_close($ck);
|
|
|
+ return $dataFromPorts;
|
|
|
+ }
|
|
|
}
|