I don’t know if it’s actually possible but I need to get the weight from a digital scale for trucks either with JavaScript or PHP.
Currently the scale is connected to a PC that is extremely slow and runs Windows XP along with a custom software developed by the company that made the scale to keep a registry of the different vehicles
that get weighed each day.
Unfortunately the PC can’t be upgraded since I would lose all the drivers and the software doesn’t work on a newer version of Windows.
I was thinking that if I can communicate with the serial device and get the data, either through Javascript or PHP, and get the weight same as the custom software does, then I could make a small webpage with PHP and a MYSQL backend
that does the same thing that the custom software does but with all the functionality that I’m actually missing and upgrade the PC.
I bought a serial to USB adapter and connected the device to my Windows 10 laptop and it appears that it’s transmitting data correctly as you can see in this picture
I’ve searched for ways to communicate with a serial device with Javascript and I think that with NodeJS you can do it but I don’t know if I can implement that to a webpage. Also I haven’t written anything in C or C++ or C# so I don’t know how I would do it from any of those languages (a lot of the answers I saw were to do it using one of those).
I also saw that there is a chrome app called chrome.serial but I haven’t found any working examples that would indicate me on how to proceed.
Any help would be greatly appreciated!!! 🙂
3
Answers
Well... I couldn't get it done with PHP after trying a script and trying to get it to work in Linux because in Windows the script can only write and not read the data from the device.
I went with Node and serial.io following the advice from Ifaruki (thanks for that!!) and with the following script I was able to correctly read the data from the scale
I'm not really familiarized with Node.
What I need to do now is to run this function inside a browser so I can spit out the data from the device in a webpage... Please if anyone could point me in the right direction....
One way is to set up an Node.js Environment and then try out serial.io
https://serialport.io/
I have fiund this, maybe this is the way to go.
For Real-Time applications, Node.js is the way to go along with socket.io, for real-time updates on your website
I have just done this using PHP and Powershell. Let powershell read serial port and dump the data to a file in your xampp. Then PHP can be used to read the file and serve as REST call handler. You can make Ajax call to localhost to get data.
Setup xampp on your machine
Open Poweshell in Administrator mode and execute the below command. This will set the powershell restriction free. Otherwise ps1 script cannot be run on the run.
Create a directory in xampp/htdocs/weight. I called it weight and you can call anything you want.
Create .ps1 script in the folder with following code
Add a PHP file. I called it weightUtil.php. Add the following code into it.
You are now set and ready. Run the tomcat server from Xampp control panel and you are ready to receive weight with POST request.
You can make post call to
http://localhost/weight/weightUtil.php
If you find any issues with rights to access file in C drive, simply add
everyone
with full access to xampp. Things will then run smooth.