PHP stdClass In Hindi | stdClass In PHP
PHP Program to check whether number is Palindrome or not | PHP Find Palindrome Number
PHP Null Coalescing Operator In Hindi , Null Coalescing Operator kya hai ?
PHP null In Hindi | PHP is null Function
Time Travel In Snowflake In Hindi | Snowflake Time Travel Example
Generator Functions In JavaScript In Hindi | JavaScript Generator Functions
Hardware Boot Process क्या है | Computer Boot कैसे होता है ?
If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
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 किया जाता है।
●●●
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
●●●
जैसे आप 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 कर सकते हैं।
●●●
port पर write करने का दूसरा तरीक है , dio
extension का use करना। इसकी full form होती है direct input output .
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 कर दे।
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 करके देखें।
●●●
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 के बारे में अच्छे से समझ भी आया होगा ।
Loading ...
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