You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

450 lines
16 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. <?php
  2. define ("UART_DEVICE_NOTSET", 0);
  3. define ("UART_DEVICE_SET", 1);
  4. define ("UART_DEVICE_OPENED", 2);
  5. class uartPort{
  6. var $_device = null;
  7. var $_windevice = null;
  8. var $_dHandle = null;
  9. var $_dState = UART_DEVICE_NOTSET;
  10. var $_buffer = "";
  11. var $_os = "";
  12. /**
  13. * This var says if buffer should be flushed by sendMessage (true) or manualy (false)
  14. *
  15. * @var bool
  16. */
  17. var $autoflush = true;
  18. function __construct(){
  19. setlocale( LC_ALL, "en_US" );
  20. $sysname = php_uname();
  21. if (substr($sysname, 0, 5) === "Linux"){
  22. $this->_os = "linux";
  23. if($this->_exec("stty --version") === 0) {
  24. register_shutdown_function(array($this, "deviceClose"));
  25. } else {
  26. trigger_error("No stty availible, unable to run.", E_USER_ERROR);
  27. };
  28. } elseif (substr($sysname, 0, 6) === "Darwin") {
  29. $this->_os = "osx";
  30. if($this->_exec("stty") === 0) {
  31. register_shutdown_function(array($this, "deviceClose"));
  32. } else {
  33. trigger_error("No stty availible, unable to run.", E_USER_ERROR);
  34. };
  35. } elseif(substr($sysname, 0, 7) === "Windows") {
  36. $this->_os = "windows";
  37. register_shutdown_function(array($this, "deviceClose"));
  38. } else {
  39. trigger_error("Host OS is neither osx, linux nor windows, unable to run.", E_USER_ERROR);
  40. exit();
  41. };
  42. echo "Port Construct Done\n";
  43. }
  44. function __destruct(){
  45. if( $this->_dState == UART_DEVICE_OPENED ) $this->deviceClose();
  46. }
  47. /**
  48. * Device set function : used to set the device name/address.
  49. * -> linux : use the device address, like /dev/ttyS0
  50. * -> osx : use the device address, like /dev/tty.serial
  51. * -> windows : use the COMxx device name, like COM1 (can also be used
  52. * with linux)
  53. *
  54. * @param string $device the name of the device to be used
  55. * @return bool
  56. */
  57. function deviceSet ($device){
  58. if ($this->_dState !== UART_DEVICE_OPENED){
  59. if ($this->_os === "linux"){
  60. if (preg_match("@^COM(\d+):?$@i", $device, $matches)){
  61. $device = "/dev/ttyS" . ($matches[1] - 1);
  62. };
  63. if ($this->_exec("stty -F " . $device) === 0){
  64. $this->_device = $device;
  65. $this->_dState = UART_DEVICE_SET;
  66. return true;
  67. };
  68. } elseif ($this->_os === "osx"){
  69. if (preg_match("@^COM(\d+):?$@i", $device, $matches)){
  70. $device = "/dev/ttyS" . ($matches[1] - 1);
  71. };
  72. if ($this->_exec("stty -f " . $device) === 0){
  73. $this->_device = $device;
  74. $this->_dState = UART_DEVICE_SET;
  75. return true;
  76. };
  77. } elseif ($this->_os === "windows"){
  78. if (preg_match("@^COM(\d+):?$@i", $device, $matches) and $this->_exec(exec("mode " . $device)) === 0){
  79. $this->_windevice = "COM" . $matches[1];
  80. $this->_device = "\\.\com" . $matches[1];
  81. $this->_dState = UART_DEVICE_SET;
  82. return true;
  83. };
  84. };
  85. trigger_error("Specified serial port is not valid", E_USER_WARNING);
  86. return false;
  87. } else {
  88. trigger_error("You must close your device before to set an other one", E_USER_WARNING);
  89. return false;
  90. };
  91. echo "Device is Set = [{device}]\n";
  92. }
  93. /**
  94. * Opens the device for reading and/or writing.
  95. *
  96. * @param string $mode Opening mode : same parameter as fopen()
  97. * @return bool
  98. */
  99. function deviceOpen ($mode = "r+b"){
  100. if ($this->_dState === UART_DEVICE_OPENED){
  101. trigger_error("The device is already opened", E_USER_NOTICE);
  102. return true;
  103. };
  104. if ($this->_dState === UART_DEVICE_NOTSET){
  105. trigger_error("The device must be set before to be open", E_USER_WARNING);
  106. return false;
  107. };
  108. if (!preg_match("@^[raw]\+?b?$@", $mode)){
  109. trigger_error("Invalid opening mode : ".$mode.". Use fopen() modes.", E_USER_WARNING);
  110. return false;
  111. };
  112. $this->_dHandle = @fopen($this->_device, $mode );
  113. if ($this->_dHandle !== false){
  114. stream_set_blocking($this->_dHandle, 0);
  115. $this->_dState = UART_DEVICE_OPENED;
  116. return true;
  117. };
  118. $this->_dHandle = null;
  119. trigger_error("Unable to open the device", E_USER_WARNING);
  120. return false;
  121. }
  122. /**
  123. * Closes the device
  124. *
  125. * @return bool
  126. */
  127. function deviceClose (){
  128. if ($this->_dState !== UART_DEVICE_OPENED){
  129. return true;
  130. };
  131. if (fclose($this->_dHandle)){
  132. $this->_dHandle = null;
  133. $this->_dState = UART_DEVICE_SET;
  134. return true;
  135. };
  136. trigger_error("Unable to close the device", E_USER_ERROR);
  137. return false;
  138. }
  139. //
  140. // OPEN/CLOSE DEVICE SECTION -- {STOP}
  141. //
  142. //
  143. // CONFIGURE SECTION -- {START}
  144. //
  145. /**
  146. * Configure the Baud Rate
  147. * Possible rates : 110, 150, 300, 600, 1200, 2400, 4800, 9600, 38400,
  148. * 57600 and 115200.
  149. *
  150. * @param int $rate the rate to set the port in
  151. * @return bool
  152. */
  153. function confBaudRate ($rate){
  154. if ($this->_dState !== UART_DEVICE_SET){
  155. trigger_error("Unable to set the baud rate : the device is either not set or opened", E_USER_WARNING);
  156. return false;
  157. };
  158. $validBauds = array (
  159. 110 => 11,
  160. 150 => 15,
  161. 300 => 30,
  162. 600 => 60,
  163. 1200 => 12,
  164. 2400 => 24,
  165. 4800 => 48,
  166. 9600 => 96,
  167. 19200 => 19,
  168. 38400 => 38400,
  169. 57600 => 57600,
  170. 115200 => 115200
  171. );
  172. if (isset($validBauds[$rate])){
  173. if ($this->_os === "linux"){
  174. $ret = $this->_exec("stty -F " . $this->_device . " " . (int) $rate, $out);
  175. } elseif ($this->_os === "osx"){
  176. $ret = $this->_exec("stty -f " . $this->_device . " " . (int) $rate, $out);
  177. } elseif ($this->_os === "windows"){
  178. $ret = $this->_exec("mode " . $this->_windevice . " BAUD=" . $validBauds[$rate], $out);
  179. } else return false;
  180. if ($ret !== 0){
  181. trigger_error ("Unable to set baud rate: " . $out[1], E_USER_WARNING);
  182. return false;
  183. };
  184. };
  185. }
  186. /**
  187. * Configure parity.
  188. * Modes : odd, even, none
  189. *
  190. * @param string $parity one of the modes
  191. * @return bool
  192. */
  193. function confParity ($parity){
  194. if ($this->_dState !== UART_DEVICE_SET){
  195. trigger_error("Unable to set parity : the device is either not set or opened", E_USER_WARNING);
  196. return false;
  197. };
  198. $args = array(
  199. "none" => "-parenb",
  200. "odd" => "parenb parodd",
  201. "even" => "parenb -parodd",
  202. );
  203. if (!isset($args[$parity])){
  204. trigger_error("Parity mode not supported", E_USER_WARNING);
  205. return false;
  206. };
  207. if ($this->_os === "linux"){
  208. $ret = $this->_exec("stty -F " . $this->_device . " " . $args[$parity], $out);
  209. } elseif ($this->_os === "osx"){
  210. $ret = $this->_exec("stty -f " . $this->_device . " " . $args[$parity], $out);
  211. } else{
  212. $ret = $this->_exec("mode " . $this->_windevice . " PARITY=" . $parity{0}, $out);
  213. };
  214. if ($ret === 0){
  215. return true;
  216. };
  217. trigger_error("Unable to set parity : " . $out[1], E_USER_WARNING);
  218. return false;
  219. }
  220. /**
  221. * Sets the length of a character.
  222. *
  223. * @param int $int length of a character (5 <= length <= 8)
  224. * @return bool
  225. */
  226. function confCharacterLength ($int){
  227. if ($this->_dState !== UART_DEVICE_SET){
  228. trigger_error("Unable to set length of a character : the device is either not set or opened", E_USER_WARNING);
  229. return false;
  230. };
  231. $int = (int) $int;
  232. if ($int < 5) $int = 5;
  233. elseif ($int > 8) $int = 8;
  234. if ($this->_os === "linux"){
  235. $ret = $this->_exec("stty -F " . $this->_device . " cs" . $int, $out);
  236. } elseif ($this->_os === "osx"){
  237. $ret = $this->_exec("stty -f " . $this->_device . " cs" . $int, $out);
  238. } else {
  239. $ret = $this->_exec("mode " . $this->_windevice . " DATA=" . $int, $out);
  240. };
  241. if ($ret === 0){
  242. return true;
  243. };
  244. trigger_error("Unable to set character length : " .$out[1], E_USER_WARNING);
  245. return false;
  246. }
  247. /**
  248. * Sets the length of stop bits.
  249. *
  250. * @param float $length the length of a stop bit. It must be either 1,
  251. * 1.5 or 2. 1.5 is not supported under linux and on some computers.
  252. * @return bool
  253. */
  254. function confStopBits ($length){
  255. if ($this->_dState !== UART_DEVICE_SET){
  256. trigger_error("Unable to set the length of a stop bit : the device is either not set or opened", E_USER_WARNING);
  257. return false;
  258. };
  259. if ($length != 1 and $length != 2 and $length != 1.5 and !($length == 1.5 and $this->_os === "linux")){
  260. trigger_error("Specified stop bit length is invalid", E_USER_WARNING);
  261. return false;
  262. };
  263. if ($this->_os === "linux"){
  264. $ret = $this->_exec("stty -F " . $this->_device . " " . (($length == 1) ? "-" : "") . "cstopb", $out);
  265. } elseif ($this->_os === "osx"){
  266. $ret = $this->_exec("stty -f " . $this->_device . " " . (($length == 1) ? "-" : "") . "cstopb", $out);
  267. } else {
  268. $ret = $this->_exec("mode " . $this->_windevice . " STOP=" . $length, $out);
  269. };
  270. if ($ret === 0){
  271. return true;
  272. };
  273. trigger_error("Unable to set stop bit length : " . $out[1], E_USER_WARNING);
  274. return false;
  275. }
  276. /**
  277. * Configures the flow control
  278. *
  279. * @param string $mode Set the flow control mode. Availible modes :
  280. * -> "none" : no flow control
  281. * -> "rts/cts" : use RTS/CTS handshaking
  282. * -> "xon/xoff" : use XON/XOFF protocol
  283. * @return bool
  284. */
  285. function confFlowControl ($mode){
  286. if ($this->_dState !== UART_DEVICE_SET){
  287. trigger_error("Unable to set flow control mode : the device is either not set or opened", E_USER_WARNING);
  288. return false;
  289. };
  290. $linuxModes = array(
  291. "none" => "clocal -crtscts -ixon -ixoff",
  292. "rts/cts" => "-clocal crtscts -ixon -ixoff",
  293. "xon/xoff" => "-clocal -crtscts ixon ixoff"
  294. );
  295. $windowsModes = array(
  296. "none" => "xon=off octs=off rts=on",
  297. "rts/cts" => "xon=off octs=on rts=hs",
  298. "xon/xoff" => "xon=on octs=off rts=on",
  299. );
  300. if ($mode !== "none" and $mode !== "rts/cts" and $mode !== "xon/xoff") {
  301. trigger_error("Invalid flow control mode specified", E_USER_ERROR);
  302. return false;
  303. };
  304. if ($this->_os === "linux")
  305. $ret = $this->_exec("stty -F " . $this->_device . " " . $linuxModes[$mode], $out);
  306. elseif ($this->_os === "osx")
  307. $ret = $this->_exec("stty -f " . $this->_device . " " . $linuxModes[$mode], $out);
  308. else
  309. $ret = $this->_exec("mode " . $this->_windevice . " " . $windowsModes[$mode], $out);
  310. if ($ret === 0) return true;
  311. else {
  312. trigger_error("Unable to set flow control : " . $out[1], E_USER_ERROR);
  313. return false;
  314. };
  315. }
  316. /**
  317. * Sets a setserial parameter (cf man setserial)
  318. * NO MORE USEFUL !
  319. * -> No longer supported
  320. * -> Only use it if you need it
  321. *
  322. * @param string $param parameter name
  323. * @param string $arg parameter value
  324. * @return bool
  325. */
  326. function setSetserialFlag ($param, $arg = ""){
  327. if (!$this->_ckOpened()) return false;
  328. $return = exec ("setserial " . $this->_device . " " . $param . " " . $arg . " 2>&1");
  329. if ($return{0} === "I"){
  330. trigger_error("setserial: Invalid flag", E_USER_WARNING);
  331. return false;
  332. } elseif ($return{0} === "/"){
  333. trigger_error("setserial: Error with device file", E_USER_WARNING);
  334. return false;
  335. } else{
  336. return true;
  337. };
  338. }
  339. //
  340. // CONFIGURE SECTION -- {STOP}
  341. //
  342. //
  343. // I/O SECTION -- {START}
  344. //
  345. /**
  346. * Sends a string to the device
  347. *
  348. * @param string $str string to be sent to the device
  349. * @param float $waitForReply time to wait for the reply (in seconds)
  350. */
  351. function sendMessage ($str, $waitForReply = 0.1){
  352. $this->_buffer .= $str;
  353. if ($this->autoflush === true) $this->serialflush();
  354. usleep((int) ($waitForReply * 1000000));
  355. }
  356. /**
  357. * Reads the port until no new datas are availible, then return the content.
  358. *
  359. * @pararm int $count number of characters to be read (will stop before
  360. * if less characters are in the buffer)
  361. * @return string
  362. */
  363. function readPort ($count = 0){
  364. if ($this->_dState !== UART_DEVICE_OPENED){
  365. trigger_error("Device must be opened to read it", E_USER_WARNING);
  366. return false;
  367. };
  368. if ($this->_os === "linux" || $this->_os === "osx"){
  369. // Behavior in OSX isn't to wait for new data to recover, but just grabs what's there!
  370. // Doesn't always work perfectly for me in OSX
  371. $content = ""; $i = 0;
  372. if ($count !== 0){
  373. do {
  374. if ($i > $count) $content .= fread($this->_dHandle, ($count - $i));
  375. else $content .= fread($this->_dHandle, 128);
  376. } while (($i += 128) === strlen($content));
  377. } else {
  378. do {
  379. $content .= fread($this->_dHandle, 128);
  380. } while (($i += 128) === strlen($content));
  381. };
  382. return $content;
  383. } elseif ($this->_os === "windows"){
  384. /* Do nothing : not implented yet */
  385. };
  386. trigger_error("Reading serial port is not implemented for Windows", E_USER_WARNING);
  387. return false;
  388. }
  389. /**
  390. * Flushes the output buffer
  391. * Renamed from flush for osx compat. issues
  392. *
  393. * @return bool
  394. */
  395. function serialflush (){
  396. if (!$this->_ckOpened()) return false;
  397. if (fwrite($this->_dHandle, $this->_buffer) !== false){
  398. $this->_buffer = "";
  399. return true;
  400. } else {
  401. $this->_buffer = "";
  402. trigger_error("Error while sending message", E_USER_WARNING);
  403. return false;
  404. };
  405. }
  406. //
  407. // I/O SECTION -- {STOP}
  408. //
  409. //
  410. // INTERNAL TOOLKIT -- {START}
  411. //
  412. function _ckOpened(){
  413. if ($this->_dState !== UART_DEVICE_OPENED){
  414. trigger_error("Device must be opened", E_USER_WARNING);
  415. return false;
  416. };
  417. return true;
  418. }
  419. function _ckClosed(){
  420. if ($this->_dState !== UART_DEVICE_CLOSED){
  421. trigger_error("Device must be closed", E_USER_WARNING);
  422. return false;
  423. };
  424. return true;
  425. }
  426. function _exec($cmd, &$out = null){
  427. $desc = array(
  428. 1 => array("pipe", "w"),
  429. 2 => array("pipe", "w")
  430. );
  431. $proc = proc_open($cmd, $desc, $pipes);
  432. $ret = stream_get_contents($pipes[1]);
  433. $err = stream_get_contents($pipes[2]);
  434. fclose($pipes[1]);
  435. fclose($pipes[2]);
  436. $retVal = proc_close($proc);
  437. if (func_num_args() == 2) $out = array($ret, $err);
  438. return $retVal;
  439. }
  440. //
  441. // INTERNAL TOOLKIT -- {STOP}
  442. //
  443. }
  444. ?>