Serial::__destruct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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
     * @var PhpSerial
23
     */
24
    protected $resource;
25
    /**
26
     * @var bool
27
     */
28
    protected $deviceStatus = false;
29
    
30
    /**
31
     * Construct
32
     * Set and open serial device
33
     * @param string $device
34
     * @param int $baudRate
35
     * @param int $byteSize
36
     * @param string $parity
37
     * @param int $stopbits
38
     * @param string $flowctrl
39
     */
40
    public function __construct(
41
        $device = "/dev/ttyS0",
42
        $baudRate = 9600,
43
        $byteSize = 8,
44
        $parity = 'none',
45
        $stopbits = 1,
46
        $flowctrl = 'none'
47
    ) {
48
        $this->resource = new PhpSerial();
49
        $this->resource->setPort($device);
50
        $this->resource->setBaudRate($baudRate);
51
        $this->resource->setDataBits($byteSize);
52
        $this->resource->setParity($parity);
53
        $this->resource->setFlowControl($flowctrl);
54
        $this->resource->setStopBits($stopbits);
55
        $this->resource->setUp();
56
        $this->deviceStatus = $this->resource->open();
57
    }
58
    
59
    /**
60
     * End class
61
     */
62
    public function __destruct()
63
    {
64
        $this->close();
65
    }
66
    
67
    
68
    /**
69
     * Finalize printer connection
70
     */
71
    public function close()
72
    {
73
        $this->deviceStatus = ! $this->resource->close();
74
    }
75
76
    /**
77
     * send data to printer
78
     * @param string $data
79
     */
80
    public function write($data)
81
    {
82
        $this->resource->write($data);
83
        $this->resource->flush();
84
    }
85
    
86
    /**
87
     * Read serial port
88
     * @param int $len
89
     * @return string
90
     */
91
    public function read($len = 0)
92
    {
93
        return $this->resource->read($len);
94
    }
95
}
96