c++语⾔读串⼝,如何把读取数据保存下来,读取串⼝部分有源
代码,求⼤⽜帮助...
怎么把读取数据保存下来,读取串⼝部分有源代码,求⼤⽜帮助!
本帖最后由 a496377703 于 2014-10-29 20:47:41 编辑
#include
#include
//#include
#include
HANDLE hComm;
COMSTAT comstat;
bool openport(char *portname)//打开⼀个串⼝
{
hComm = CreateFile(portname,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (hComm == INVALID_HANDLE_VALUE)
return FALSE;
else
return true;
}
bool setupdcb(int rate_arg)
{DCB myDCB;
int rate= rate_arg;
memset(&myDCB,0,sizeof(myDCB));
if(!GetCommState(hComm,&myDCB))//获取当前DCB配置
{
return FALSE;
}
SetupComm(hComm,1024,512);
GetCommState(hComm,&myDCB);
myDCB.BaudRate=CBR_115200;
myDCB.fBinary=TRUE;
myDCB.fParity=TRUE;
myDCB.ByteSize=8;
myDCB.Parity=NOPARITY;
python怎么读取串口数据myDCB.StopBits=ONESTOPBIT;
SetCommState(hComm,&myDCB);
if(!SetCommState(hComm,&myDCB))
{
return false;
}
else
return true;
}
ReceiveChar( )
{
BOOL bRead = TRUE;
BOOL bResult = TRUE;
DWORD dwError = 0;
DWORD BytesRead = 0;
char RXBuff;
for (;;)
{
bResult = ClearCommError(hComm, &dwError, &comstat); if (comstat.cbInQue == 0)
continue;
if (bRead)
{
bResult = ReadFile(hComm,      // Handle to COMM port
&RXBuff,        // RX Buffer Pointer
1,                  // Read one byte
&BytesRead,          // Stores number of bytes read NULL);          // pointer to the m_ov structure
printf("%c",RXBuff);
if (!bResult)
{
switch (dwError = GetLastError())
{
case ERROR_IO_PENDING:
{
bRead = FALSE;
break;
}
default:
{
break;
}
}
}
else
{
bRead = TRUE;
}
} // close if (bRead)
if (!bRead)
{
bRead = TRUE;
bResult = GetOverlappedResult(hComm,  // Handle to COMM port NULL,  // Overlapped structure
&BytesRead,    // Stores number of bytes read
TRUE);          // Wait flag
}
}
return 0;
}
void  main()
{
if(openport("com3"))
printf("open comport success\n");
if(setupdcb(115200))
printf("setupDCB success\n");
PurgeComm(hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT); printf("received data:\n");
ReceiveChar( );
system("pause");
}
这段程序可以读到数据,请问在那⾥加保存数据的代码,具体代码是什么?
------解决思路----------------------
if (bRead)
{
bResult = ReadFile(hComm,      // Handle to COMM port
&RXBuff,        // RX Buffer Pointer
1,                  // Read one byte
&BytesRead,          // Stores number of bytes read
NULL);          // pointer to the m_ov structure
ReadFIle之后,你把 RXBuff 按BytesRead拷贝出来
------解决思路----------------------
ReceiveChar( )
{
BOOL bRead = TRUE;
BOOL bResult = TRUE;
DWORD dwError = 0;
DWORD BytesRead = 0;
char RXBuff;
FILE *f;//★
for (;;) {
bResult = ClearCommError(hComm, &dwError, &comstat);
if (comstat.cbInQue == 0)
continue;
if (bRead) {
bResult = ReadFile(hComm,      // Handle to COMM port
&RXBuff,        // RX Buffer Pointer
1,                  // Read one byte
&BytesRead,          // Stores number of bytes read
NULL);          // pointer to the m_ov structure
printf("%c",RXBuff);
f=fopen("d:\\myfile.dat","ab"); //★
if (f) {                        //★
fputc(RXBuff,f);            //★
fclose(f);                  //★
}                              //★
if (!bResult) {
switch (dwError = GetLastError()) {
case ERROR_IO_PENDING: {
bRead = FALSE;
break;
}
default: {
break;
}
}
} else {
bRead = TRUE;
}
} // close if (bRead)
if (!bRead) {
bRead = TRUE;
bResult = GetOverlappedResult(hComm,  // Handle to COMM port NULL,  // Overlapped structure
&BytesRead,    // Stores number of bytes read
TRUE);          // Wait flag
}
}
return 0;
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。