PHP com port communication In Hindi | Write on com port in PHP

Blogs ❯❯ PHP

Image could not load

PHP com port communication

PHP development में कई बार हमें hardware devices से भी communicate करना पड़ जाता है। जैसे किसी LED को on / off करना या फिर Matrix LED पर content लिखना।

Matrix LED में content को आपने कई banks , buses या फिर restaurants में जरूर देखा होगा। जहाँ पर token wise customers को serve किया जाता है।

PHP com port communication

Well , PHP में इस तरह से hardware communication करने के लिए functionalities हैं जिनका use करके easily content show करा सकते हैं।

generally content को ports पर लिखा जाता है जिनसे ये LEDs connected होती है , तो हमें इन ports पर ही data write करना होता है , इसलिए उन्हे COM PORT Communication कहते हैं।

किसी PORT पर data write करने के लिए 2 methods हैं -

  • using fwrite() function

  • using dio extension

PHP com port example

जैसे आप normally किसी file को open करके content लिखते थे बैसे ही किसी port पर भी लिख सकते हैं।

function write_port($port, $data) { try { // open port. $comPort = fopen($port, $data); if (!$comPort) { die("Could not open PORT $portNumber"); } // write on port. $bytesWritten = fwrite($comPort, $data); if ($bytesWritten === false) { echo 'Error writing to'; } else { echo "Wrote $bytesWritten bytes to COM port"; } // finally close port. fclose($comPort); } catch(Exception $e) { echo $e->getMessage(); } } // now call the function. write_port("COM1", "Hello there !");

कुछ इस तरह से आप com port पर data write करते हैं।

port पर write करते समय ध्यान रहे वो port पहले से open नहीं होना चाहिए , और data write करने के बाद port को close करना important है। दूसरी चीज़ port को open करने के लिए necessary permissions भी होना चाहिए।

अगर किसी वजह से कोई error भी नहीं है और port पर data write भी नहीं हुआ है तो आप अपने system में device manager के अंदर जाकर उस particular port को reset करके try कर सकते हैं।

PHP serial port communication

port पर write करने का दूसरा तरीक है , dio extension का use करना। इसकी full form होती है direct input output .

Install dio extension

dio communication करने के लिए आपको इस extension को install करना पड़ेगा। Linux / Mac में तो आप direct command line से install कर सकते हैं जबकि windows के लिए आप PHP package की official website https://pecl.php.net से इसे download कर सकते हैं।

download कर के बाद इसे extract करें फिर php_dio.dll file को copy करके php/ext folder में paste कर दे। then php.ini file में जाकर extension=dio line add करके extension को enable करके server restart कर दे।

PHP com port example

finally dio extension install करने के बाद अब इसे कुछ इस तरह से use करेंगे।

function write_port($port, $data) { try { $baudRate = 9600; $bits = 8; $spotBit = 1; $portName = $port; if(!extension_loaded('dio')) { throw new Exception("PHP Direct IO does not appear to be installed"); } $bbSerialPort = null; // check OS. if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { $bbSerialPort = dio_open($portName, O_RDWR ); //we're on windows configure com port from command line. exec("mode {$portName} baud={$baudRate} data={$bits} stop={$spotBit} parity=n xon=on"); } else { $bbSerialPort = dio_open($portName, O_RDWR | O_NOCTTY | O_NONBLOCK ); dio_fcntl($bbSerialPort, F_SETFL, O_SYNC); //we're on 'nix configure com from php direct io function. dio_tcsetattr($bbSerialPort, array( 'baud' => $baudRate, 'bits' => $bits, 'stop' => $spotBit, 'parity' => 'odd' )); } if(!$bbSerialPort) { throw new Exception("Could not open Serial port {$portName}"); } dio_write($bbSerialPort, $data ); dio_close($bbSerialPort); echo "Data written successfully on $portName"; } catch(Exception $e) { echo $e->getMessage(); } } // now call the function. write_port("COM1", "Hello there !");

पहली बार में कोई error न आये और data write भी न हो तो ऊपर बताये गए steps को follow करके देखें।

Important note about com port

Actually PORT से connect की जाने वाले hardware like LED / Matrix LED किसी private company द्वारा ready की जाती है , जिसका मतलब है कि content को read और write करने का standard अलग अलग होगा।

कई companies के hardware encrypted या hexadecimal data read करते हैं। और write किया जाने वाले data में कई तरह की information जैसे उस hardware की ID , Bits , Parity , Color etc. और data location मतलब hardware के किस जगह वो content देखेगा भी include होता है।

तो अच्छा होगा पहले आप उस hardware के बारे में company की तरफ provide किया गया documentation पढ़ लें फिर उसके according PORT पर data write करने के लिए उसका proper format बनाकर send करें।

I hope , ये blog आपके लिए helpful रहा होगा और आपको com port communication के बारे में अच्छे से समझ भी आया होगा ।

Recent Blogs

Loading ...

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook