Completed
Push — master ( 5042f6...c348a8 )
by Roberto
11:18
created

Serial   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 5
c 4
b 0
f 0
lcom 1
cbo 1
dl 0
loc 80
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 18 1
A __destruct() 0 4 1
A close() 0 4 1
A write() 0 6 1
A read() 0 4 1
1
<?php
2
3
namespace Posprint\Connectors;
4
5
/**
6
 * Class Serial
7
 *
8
 * @category   NFePHP
9
 * @package    Posprint
10
 * @copyright  Copyright (c) 2016
11
 * @license    http://www.gnu.org/licenses/lesser.html LGPL v3
12
 * @author     Roberto L. Machado <linux.rlm at gmail dot com>
13
 * @link       http://github.com/nfephp-org/posprint for the canonical source repository
14
 */
15
16
use Posprint\Connectors\ConnectorInterface;
17
use Posprint\Extras\PhpSerial;
18
19
class Serial implements ConnectorInterface
20
{
21
    /**
22
     *
23
     * @var PhpSerial
24
     */
25
    protected $resource;
26
    /**
27
     *
28
     * @var bool
29
     */
30
    protected $deviceStatus = false;
31
    
32
    /**
33
     * Construct
34
     * Set and open serial device
35
     * @param string $device
36
     * @param int $baudRate
37
     * @param int $byteSize
38
     * @param string $parity
39
     * @param string $stopbits
40
     * @param string $flowctrl
41
     */
42
    public function __construct(
43
        $device = "/dev/ttyS0",
44
        $baudRate = 9600,
45
        $byteSize = 8,
46
        $parity = 'none',
47
        $stopbits = '1',
48
        $flowctrl = 'none'
49
    ) {
50
        $this->resource = new PhpSerial();
51
        $this->resource->setPort($device);
52
        $this->resource->setBaudRate($baudRate);
53
        $this->resource->setDataBits($byteSize);
54
        $this->resource->setParity($parity);
55
        $this->resource->setFlowControl($flowctrl);
56
        $this->resource->setStopBits($stopbits);
57
        $this->resource->setUp();
58
        $this->deviceStatus = $this->resource->open();
59
    }
60
    
61
    /**
62
     * End class
63
     */
64
    public function __destruct()
65
    {
66
        $this->close();
67
    }
68
    
69
    
70
    /**
71
     * Finalize printer connection
72
     */
73
    public function close()
74
    {
75
        $this->deviceStatus = ! $this->resource->close();
76
    }
77
78
    /**
79
     * send data to printer
80
     * @param string $data
81
     */
82
    public function write($data)
83
    {
84
        $this->resource->write($data);
85
        $this->resource->flush();
86
        
87
    }
88
    
89
    /**
90
     * Read serial port
91
     * @param int $len
92
     * @return string
93
     */
94
    public function read($len = 0)
95
    {
96
        return $this->resource->read($len);
97
    }
98
}
99