trimitere date din c++ intr-o pagina php in browser

Intrebari despre limbajul C++, standardul C++, STL, OOP in C++ sau alte subiecte nelegate de VisualC++
Post Reply
violetab
Junior
Junior
Posts: 4
Joined: 12 Dec 2013, 16:01
Judet: Constanţa

trimitere date din c++ intr-o pagina php in browser

Post by violetab » 12 Dec 2013, 16:21

Am un cititor de date pus pe portul COM4. Am preluat datele in c++ ( in php nu am reusit). In cmd, cand rulez executabilul rezultat dupa compilare, imi afiseaza datele venite de la cititor.
Aplicatia mea e scrisa in php.
Este posibil sa trimit datele citite in c++ la browser-ul meu unde ruleaza aplicatia?
Eu am gasit pe net exemplul acesta:

Code: Select all

#include <stdio.h>
#include <curl/curl.h>

int main(void)
{
  CURL *curl;
  CURLcode res;

  /* In windows, this will init the winsock stuff */
  curl_global_init(CURL_GLOBAL_ALL);

  /* get a curl handle */
  curl = curl_easy_init();
  if(curl) {
    /* First set the URL that is about to receive our POST. This URL can
       just as well be a https:// URL if that is what should receive the
       data. */
    curl_easy_setopt(curl, CURLOPT_URL, "http://localhost/acces/persoana.php");
    /* Now specify the POST data */
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "nume=daniel&prenume=ana");

    /* Perform the request, res will get the return code */
    res = curl_easy_perform(curl);
    /* Check for errors */
    if(res != CURLE_OK)
      fprintf(stderr, "curl_easy_perform() failed: %s\n",
              curl_easy_strerror(res));

    /* always cleanup */
    curl_easy_cleanup(curl);
  }
  curl_global_cleanup();
  return 0;
}
care doar imi afiseaza in cmd fisierul meu php. Atat am reusit eu.
Este posibil sa trimit datele din c++ in browser in php?
Va multumesc!



User avatar
bu7ch3r
Membru++
Membru++
Posts: 326
Joined: 17 May 2011, 15:17
Judet: Iaşi
Location: Sofia
Contact:

Re: trimitere date din c++ intr-o pagina php in browser

Post by bu7ch3r » 13 Dec 2013, 14:15

Pai codul pus de tine face un POST la o o pagina de pe serverul tau local si ii trimite "nume" si "prenume".
Daca ai sa schimbi cu "http://adresa_paginii/calea unde e fisierul php/fisierultau.php" ai sa poti sa trimiti ce ai tu chef.
Scriptul tau tre sa parseze datele alea si sa le stocheze undeva, sau sa le trimita la toti clientii care au aplicatia deschisa.

Is tare curios cum ai ajuns tu la concluzia ca poti citi de pe COM cu php ?
Cu stima,
Lupu Claudiu

neagu_laurentiu
Membru++
Membru++
Posts: 919
Joined: 23 Jul 2007, 11:32

Re: trimitere date din c++ intr-o pagina php in browser

Post by neagu_laurentiu » 13 Dec 2013, 15:10

bu7ch3r wrote:Is tare curios cum ai ajuns tu la concluzia ca poti citi de pe COM cu php ?
PHP extension, w32api_invoke_function ori executia a unui program extern.

violetab
Junior
Junior
Posts: 4
Joined: 12 Dec 2013, 16:01
Judet: Constanţa

Re: trimitere date din c++ intr-o pagina php in browser

Post by violetab » 13 Dec 2013, 16:25

Am citit de pe COM4 cu c++ cu codul urmator:

Code: Select all

#include <stdio.h>
#include <conio.h>
#include <string.h>

#define STRICT
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

void system_error(char *name) {
// Retrieve, format, and print out a message from the last error.  The
// `name' that's passed should be in the form of a present tense noun
// (phrase) such as "opening file".
//
    char *ptr = NULL;
    FormatMessage(
        FORMAT_MESSAGE_ALLOCATE_BUFFER |
        FORMAT_MESSAGE_FROM_SYSTEM,
        0,
        GetLastError(),
        0,
        (char *)&ptr,
        1024,
        NULL);

    fprintf(stderr, "\nError %s: %s\n", name, ptr);
    LocalFree(ptr);
}

int main(int argc, char **argv) {

    int ch;
    char buffer[1];
    HANDLE file;
    COMMTIMEOUTS timeouts;
    DWORD read, written;
    DCB port;
    HANDLE keyboard = GetStdHandle(STD_INPUT_HANDLE);
    HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);
    DWORD mode;
    char port_name[128] = "\\\\.\\COM4";
    char init[] = ""; // e.g., "ATZ" to completely reset a modem.

    if ( argc > 2 )
        sprintf(port_name, "\\\\.\\COM%c", argv[1][0]);

    // open the comm port.
    file = CreateFile(port_name,
        GENERIC_READ | GENERIC_WRITE,
        0,
        NULL,
        OPEN_EXISTING,
        0,
        NULL);

    if ( INVALID_HANDLE_VALUE == file) {
        system_error("opening file");
        return 1;
    }

    // get the current DCB, and adjust a few bits to our liking.
    memset(&port, 0, sizeof(port));
    port.DCBlength = sizeof(port);
    if ( !GetCommState(file, &port))
        system_error("getting comm state");
    if (!BuildCommDCB("baud=19200 parity=n data=8 stop=1", &port))
        system_error("building comm DCB");
    if (!SetCommState(file, &port))
        system_error("adjusting port settings");

    // set short timeouts on the comm port.
    timeouts.ReadIntervalTimeout = 1;
    timeouts.ReadTotalTimeoutMultiplier = 1;
    timeouts.ReadTotalTimeoutConstant = 1;
    timeouts.WriteTotalTimeoutMultiplier = 1;
    timeouts.WriteTotalTimeoutConstant = 1;
    if (!SetCommTimeouts(file, &timeouts))
        system_error("setting port time-outs.");

    // set keyboard to raw reading.
    if (!GetConsoleMode(keyboard, &mode))
        system_error("getting keyboard mode");
    mode &= ~ ENABLE_PROCESSED_INPUT;
    if (!SetConsoleMode(keyboard, mode))
        system_error("setting keyboard mode");

    if (!EscapeCommFunction(file, CLRDTR))
        system_error("clearing DTR");
    Sleep(200);
    if (!EscapeCommFunction(file, SETDTR))
        system_error("setting DTR");

    if ( !WriteFile(file, init, sizeof(init), &written, NULL))
        system_error("writing data to port");

    if (written != sizeof(init))
        system_error("not all data written to port");

    // basic terminal loop:
    do {
        // check for data on port and display it on screen.
        ReadFile(file, buffer, sizeof(buffer), &read, NULL);
        if ( read )
            WriteFile(screen, buffer, read, &written, NULL);

        // check for keypress, and write any out the port.
        if ( kbhit() ) {
            ch = getch();
            WriteFile(file, &ch, 1, &written, NULL);
        }
    // until user hits ctrl-backspace.
    } while ( ch != 127);

    // close up and go home.
    CloseHandle(keyboard);
    CloseHandle(file);
    return 0;
}
Si acum vreau sa le trimit in aplicatie, in browser.
Aplicatia mea este deocamdata in local la adresa http://localhost/acces/persoana.php, urmand ca apoi sa o pun pe server.
Nu stiu cum sa trimit in browser datele venite de la cititor.

neagu_laurentiu
Membru++
Membru++
Posts: 919
Joined: 23 Jul 2007, 11:32

Re: trimitere date din c++ intr-o pagina php in browser

Post by neagu_laurentiu » 13 Dec 2013, 18:55

violetab wrote:Nu stiu cum sa trimit in browser datele venite de la cititor.
Datele nu le trimiti direct de la portul serial la browser. Daca aplicatia ta C e instalata la client, ea apeleaza serverul iar el trimite datele in browser (la o cerere a acestuia). Cand portul cu pricina se afla pe acelasi calculator cu PHP-ul atunci alege una din metodele enumerate mai sus de mine, desi merge si asta cu apelul paginii ca in varianta client-port serial.

violetab
Junior
Junior
Posts: 4
Joined: 12 Dec 2013, 16:01
Judet: Constanţa

Re: trimitere date din c++ intr-o pagina php in browser

Post by violetab » 16 Dec 2013, 11:58

Aplicatia C++ e instalata la client. Ma conectez cu socket_connect la server-ul de apache si php? Si apoi vreau ca server-ul sa trimita date in browser de fiecare data cand vin date de la port pe client.
VA multumesc.

neagu_laurentiu
Membru++
Membru++
Posts: 919
Joined: 23 Jul 2007, 11:32

Re: trimitere date din c++ intr-o pagina php in browser

Post by neagu_laurentiu » 16 Dec 2013, 21:09

E o varianta si asta cu soket-ul in PHP. La fel cum era si prima, cu trimiterea datelor via http.
Problema de fond e ca nu intelegi cum functioneaza web-ul in general, PHP ori browserul in particular. Browserul face o cerere la server/PHP si nu PHP livreaza din propria initiativa browserului (chiar daca se poate si asa e alta mancare de peste acum).

violetab
Junior
Junior
Posts: 4
Joined: 12 Dec 2013, 16:01
Judet: Constanţa

Re: trimitere date din c++ intr-o pagina php in browser

Post by violetab » 11 Mar 2014, 10:18

Am rezolvat problema. Am trimis datele din c++ in baza de date, iar in pagina php am pus un buton care in momentul in care e apasat preia datele din baza de date.
Va multumesc pentru raspunsuri.

Post Reply