0

I need to connect a scale (rs232 port) to see the weight in my laravel application.

the way I did it was through a C # console application which I call from php.

what I need is to see in real time the weight changes that of the scale has, but I don't know how to run the c # console continuously within PHP

this is the php code in my controller

public function puerto(){
        $ultima_linea = system('C:\Users\richa\source\repos\ports\ports\bin\Debug\ports.exe' ,$retval);
        return $ultima_linea;
        
    }

and this is the C # console code

class Program
    {
        static void Main (string [] args)
        {
            // Create a new SerialPort object with the default settings.
            SerialPort Serial_port = new SerialPort ("COM7");

            Serial_port.BaudRate = 9600;
            Serial_port.Parity = Parity.None;
            Serial_port.StopBits = StopBits.One;
            Serial_port.DataBits = 8;
            Port_serie.Handshake = Handshake.None;
            Serial_port.RtsEnable = true;

            // Set the read / write timeouts.
            Serial_port.ReadTimeout = 2; // Milliseconds.
            Serial_port.WriteTimeout = 2;

            // Detect any data received.
            Serial_port.DataReceived + = new SerialDataReceivedEventHandler (DataReceivedHandler);


            Serial_port.Open ();

          // ASCII encoding and saves in byte type array variable.
              byte [] myBuffer1 = Encoding.ASCII.GetBytes ("$"); 

      // Send the buffer data all its content.
              Serial_port.Write (myBuffer1, 0, myBuffer1.Length); 

            Console.WriteLine ();
            Console.ReadKey (); // Wait to press any key.
            Serial_port.Close (); // Close the serial port.
        }

       // Detect any incoming data.
       public static void DataReceivedHandler (object sender, SerialDataReceivedEventArgs e)
        {
            SerialPort sp = (SerialPort) sender;
            string dataInput = sp.ReadExisting (); // Stores the received data in the string type variable.
            Console.WriteLine (inputData); // Displays the received data on the screen.
            
        }    
  }

I have ajax requirement it's close to what I want but I know it can be improved

 $ (document) .ready (function () {
    list();

     $ (weight1) .change (function () {
    });

 function list () 
 {
     $ .ajax ({
        url: '{{route (' weighing._port ')}}',
        method: 'POST',
        data:{
            _token: $ ('input [name = "_ token"]'). val (),
                 }
     }). done (function (res) {
          $ ('# weight1'). html (res);
        })
}

})

  • 1
    Perhaps you could use PHP directly to read the serial port - https://stackoverflow.com/questions/13114275/php-serial-port-data-return-from-arduino – Nigel Ren Mar 02 '21 at 18:23
  • thank @NigelRen , this class run on windows ?. after use this class in my project, return "Specified serial port is not valid". im havent this problem in C# console – Hainhed Mar 02 '21 at 22:30
  • I'm not sure, but you would need to ensure that you use the same device (`COM7`) as in your example. https://stackoverflow.com/questions/29619929/read-data-form-serial-port-on-windows-with-php has something similar. – Nigel Ren Mar 03 '21 at 07:40

0 Answers0