12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Mead
- * Date: 2019/9/4
- * Time: 11:58 AM
- */
- namespace App\Servers;
- use App\Handlers\Crc16Handler;
- class ResponseServer
- {
- private $start = 'AA AA';
- use Crc16Handler;
- public function send($data = [], $cmd = '81', $num_code = '01')
- {
- $num = 12;
- $num += count($data);
- $data_length = $this->byNumGetDataLength($num);
- $header = [
- $data_length,
- $this->getSoftwareVersion(),
- $cmd,
- $num_code,
- '00',
- '00',
- '00'
- ];
- $data = $this->arr2str($header, $data);
- $data .= self::crc16(explode(' ', $data));
- return $this->format($this->start . $data);
- }
- /**
- * 获取数据包的长度
- * @param $num
- * @return array
- * User: Mead
- */
- public function byNumGetDataLength($num)
- {
- $length = dechex($num);
- return str_pad($length, 4, '0', STR_PAD_LEFT);
- }
- /**
- * 软件版本号
- * @return string
- * User: Mead
- */
- public function getSoftwareVersion()
- {
- return '04';
- }
- /**
- * 获取流水号
- * @return int
- * User: Mead
- */
- public function getPipelineNumber()
- {
- return '00';
- }
- public function arr2str()
- {
- $args = func_get_args();
- $arr = '';
- foreach ($args as $arg) {
- if (is_array($arg)) {
- $arr .= implode('', $arg);
- } else {
- $arr .= str_replace(' ', '', $arg);
- }
- }
- return implode(' ', str_split($arr, 2));
- }
- public function format($str)
- {
- return strtoupper(implode(' ', str_split(str_replace(' ', '', $str), 2)));
- }
- }
|