BCB下API函数实现串口通讯显示接收内容问题
编写完成后,在输出显示com传回来的值的时候,发现只有在receive函数内printf可以显示,但是在main()里把值显示就不可以。由于本人基础不好,还请大家看看。
代码如下
//---------------------------------------
#include <vcl.h>
//---------------------------------------
#pragma hdrstop
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include"Unit1.h"
//---------------------------------------
//HANDLE hComm;
//OVERLAPPED m_ov;
//COMSTAT comstat;
//DWORD m_dwCommEvents;
bool openport(char *portname)//open a com port
{
}
bool setupdcb(int rate_arg)
{
}
bool ReceiveChar(char *resp)
{
BOOL bRead = TRUE;
BOOL bResult = TRUE;
DWORD dwError = 0;
DWORD BytesRead = 0;
char RXBuff[5000];
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
&m_ov); // pointer to the m_ov structure
sprintf(resp,"%s",RXBuff);
//printf(resp);此处可显示
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
&m_ov, // Overlapped structure
&BytesRead, // Stores number of bytes read
TRUE); // Wait flag
}
}
}
bool WriteChar(char *str)
{
//省略
}
void colse()
{
CloseHandle(hComm);
}
//---------------------------------------
void main()
{
char *rxdata;或者char rxdata[5000]
char *txdata="<./wixs177/mfgsc-1024.sh \n";
if(openport("com1"))
printf("open comport success\n");
if(setupdcb(115200))
printf("setupDCB success\n");
if(setuptimeout())
printf("setuptimeout success\n");
//PurgeComm(hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);
WriteChar(txdata);
//sleep(10);
printf("received data:\n");
ReceiveChar(rxdata);
printf(rxdata);//此处不显示
printf("test receive!!");//此处也不显示
system("pause");
}
//---------------------------------------
#pragma argsused
//---------------------------------------
[解决办法]
找个3方串口控件,好用简单实用,去妖哥网站下载
[解决办法]
"ReceiveChar(rxdata);
printf(rxdata);//此处不显示
printf("test receive!!");//此处也不显示"
调试看看 ,你的程序是不是一直在ReceiveChar里循环了,根本没有运行后面两行?
[解决办法]
感觉用控件方便