Click here to Skip to main content
15,882,152 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I tried to read serial port data(which is connected to weighment machine) using php dio exntension(https://pecl.php.net/package/dio). For some weighment machines it's working fine but on some  machines it's not reading the data. I have shown my function below which reads the data from the serial port. Can somebody give me the solution :


What I have tried:

//Function which read the data from serial port
    public static function getWeight($portName = '', $baudRate = '', $dataBit = '', $stopBit = '')
    {
        $bbSerialPort;
        $bbSerialPort = dio_open($portName, O_RDWR); //open port with read and write permission
        //we're on windows configure com port from command line
        shell_exec("mode {$portName} baud={$baudRate} data={$dataBit} stop={$stopBit} parity=n xon=on");
        $runForSeconds = new DateInterval("PT1S");
        $endTime = (new DateTime())->add($runForSeconds);
        $abc = '';
        //loop for 1 seconds
        while (new DateTime() < $endTime) {
            $data = dio_read($bbSerialPort, 512); //this is a blocking call
            if ($data) {
                preg_match('/(:\d+)/', $data, $abc);
                if (count($abc)) {
                    $abc = $abc[0];
                    if ($abc) break;
                }
            }
        }
        dio_close($bbSerialPort);
        return $abc;
    }
Posted
Updated 6-Feb-23 5:55am
Comments
Andre Oosthuizen 6-Feb-23 9:50am    
Are there any errors returned, will help to see where it throws an error? Is the machine compatible, i.e. does it return the same data as the ones that are working, read the documentation on the ones that does not return any data...
Yogesh Sahu 2023 7-Feb-23 0:07am    
This function is not return any error. Problem is the dio_read function is not return any data(return empty data).
Richard Deeming 6-Feb-23 10:07am    
NB: Your PHP code will be running on the server, so the external machine will need to be connected to the server. If you were expecting the code running on the server to read data from a machine connected to the client, then you're in for a big disappointment.
Yogesh Sahu 2023 7-Feb-23 0:08am    
My project is install on client side(locally) system which are connected to the machine.

1 solution

Start by taking two machines: one that works, and one that doesn't.
Connect them in turn to you app and check that that is the case. If they both work fine, it's probably a configuration problem with your app somewhere.

Then connect the good one, and use RealTerm, Serialwatcher or similar to look at exactly what is being received for a particular weight in hex. Take a copy of this so you can reference it later.

Then connect the "bad" one, and do the same thing with the same weight. Compare the data with the good one.

If they look identical: check that your code is using exactly the same communication settings as the terminal app for both machines. If you are absolutely positive they are identical in every possible way, modify you program to store incoming data to a log file in hex, and compare that with the terminal program output.

When you know where the problem is showing up, and what it actually looks like form a data POV, you can start fixing it - but not until then!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900