Read and write serial port data, theoretically very simple, PHP has not tried ...

Source: Internet
Author: User
Tags bool error handling php source code readfile linux
Data I have not read the PHP source code, but it has file operation function, I think it should be OK? Not really, then do a CGI to achieve also can, and independence is good, easy to maintain. In the past I have done in the BCB environment, the idea is as follows: (in fact, the equivalent of ordinary file reading and writing operations, hehe)

First, open the file

Open a serial port first by file (either of the COM1~~COM4)
HANDLE M_hcomm = CreateFile ("COM2", Generic_read | Generic_write, NULL, open_existing, file_flag_overlapped, 0);

Then you can use WriteFile () and ReadFile () to read and write to the serial port, and finally remember CloseHandle (M_HCOMM), the current open serial port off Oh.
Since the CreateFile () function takes a file_flag_overlapped constant, note that the last parameter of the WriteFile () and ReadFile () function is a overlapped structure that needs to be initialized before reading and writing:

Overlapped M_ov;

M_ov. Offset = 0;
M_ov. Offsethigh = 0;
M_ov.hevent = NULL;

Well, the COM2 is already open, so simply read and write:

Second, write:

BOOL bresult = true;
char* M_writebuffer;
DWORD bytessent = 0;

strcpy (M_writebuffer, "Try to write in");
Bresult = WriteFile (M_hcomm, M_writebuffer, strlen (char*) m_writebuffer), &bytessent, M_ov);


Third, read:

Comstat comstat;//The structure contains the state of the communication device.
BOOL bresult = true;
DWORD dwerror = 0;
DWORD bytesread = 0;
unsigned char m_readbuff;
Start looping Read
for (;;)
{
Bresult = Clearcommerror (M_hcomm, &dwerror, &comstat);//update COMSTAT structure and clear all errors
if (comstat.cbinque = 0)
{
break;//Exit For Loop if you finish reading
}

Bresult = ReadFile (M_hcomm, &readbuff, 1, &bytesread, M_ov);//read one at a time, if you like, you can read n bits at a time
......
Put your processing module here, anyway Readbuff is content (1 bits), can be merged ...
......
}

Four, turn off
CloseHandle (M_hcomm);


The above ideas can be returned through the function of error handling, as for the meaning of the error constants can be read books, complete treatment is a problem, know the method can be, as long as you are a simple application, then simply read and write is enough. This file read and write method also applies to print mouth LPT1, LPT2~~~LPTN. I haven't tried it under Unix, but since the Unix-class operating system has more emphasis on device files than Windows, I believe it's simpler and more versatile to read and write COM ports in UNIX and Linux. Still this: Know the method on the line. When it's time to sell. ^_^
However, if you use the UNIX, Linux as a CGI, you may want to set the permissions of the compiled CGI file, try Oh.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.