hi, i found some included APIs, i tried use it ,no compile errors and run time errors, but finally the console just crashed.
Even if i compile without those API, the writfile and readfile function is still unfunctional..
Code:
#include <stdio.h>
#include <windows.h>
void main()
{
DCB dcb = {0};
unsigned char str[32]={0};
DWORD wCount;
BOOL bReadStat;
DWORD dwNumBytesWritten;
BOOL bResult;
char *inbuf[10] = {"0x55","0x00","0x01","0x00","0x02","0x00","0x10","0x00","0x13","0xAA"};
HANDLE hOpenFile = (HANDLE)CreateFile("\\\\.\\COM1", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING,0, NULL);
if (hOpenFile == INVALID_HANDLE_VALUE)
{ printf("Can not open the port\n");
hOpenFile = NULL;
}
else if(hOpenFile != INVALID_HANDLE_VALUE)
{
printf("The port has been opened\n");
}
PurgeComm(hOpenFile,PURGE_RXCLEAR|PURGE_TXCLEAR|PURGE_TXABORT|PURGE_RXABORT);
if (!BuildCommDCB("9600,n,8,1", &dcb)) {
printf("Setting error\n");}
else
printf("Setting ok\n");
bResult = WriteFile(hOpenFile,inbuf,10, &dwNumBytesWritten,NULL);
if(!bResult || dwNumBytesWritten == 0)
printf("Writing fail\n");
else
printf("Writing sucessful\n");
bReadStat=ReadFile(hOpenFile,str,32,&wCount,NULL);
if(!bReadStat||wCount== 0)
{
printf("Reading fail!");
PurgeComm(hOpenFile,PURGE_RXCLEAR|PURGE_TXCLEAR|PURGE_TXABORT|PURGE_RXABORT);
}
else
{
printf("Reading sucessful");
}
CloseHandle(hOpenFile);
}