这个程序能够打开串口,能够显示'open the serial port successfully'信息,也能够显示“ok”,但是,问题就出在ShowMessage(pBuffer);这个地方,只能够显示0。
斑竹和各位能不能给我看一下,到底在读串口的时候,问题出在什么地方呢?
谢谢了,源代码如下:
//---------------------------------------------------------------------------
#include
#pragma hdrstop
#include "Unit1.h"
HANDLE hComm;
int (*pCardReady)(long*);
static long pBuffer[100]={0};
COMMTIMEOUTS ctmoNew={0},ctmoOld;
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{ DCB dcbCommPort;
// OPEN THE COMM PORT.
// REPLACE "COM1" WITH A STRING OR "COM1", "COM3", ETC. TO OPEN
// ANOTHER PORT.
hComm = CreateFile("COM1",
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
0,
0);
// IF THE PORT CANNOT BE OPENED, BAIL OUT.
if(hComm == INVALID_HANDLE_VALUE) Application->Terminate();
else
ShowMessage("open the serial port successfully!");
// SET THE COMM TIMEOUTS IN OUR EXAMPLE.
GetCommTimeouts(hComm,&ctmoOld);
ctmoNew.ReadTotalTimeoutConstant = 100;
ctmoNew.ReadTotalTimeoutMultiplier = 0;
ctmoNew.WriteTotalTimeoutMultiplier = 0;
ctmoNew.WriteTotalTimeoutConstant = 0;
SetCommTimeouts(hComm, &ctmoNew);
dcbCommPort.DCBlength = sizeof(DCB);
GetCommState(hComm, &dcbCommPort);
BuildCommDCB("9600,N,8,1", &dcbCommPort);
SetCommState(hComm, &dcbCommPort);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
DWORD dwBytesRead;
// TRY TO READ CHARACTERS FROM THE SERIAL PORT.
// IF THERE ARE NONE, IT WILL TIME OUT AND TRY AGAIN.
// IF THERE ARE, IT WILL DISPLAY THEM.
bool rValue=ReadFile(hComm, pBuffer, 50, &dwBytesRead, NULL);
if(!rValue==NULL)
{ShowMessage("ok");
ShowMessage(pBuffer[0]);}
else
ShowMessage("no");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
CloseHandle(hComm);
}
//---------------------------------------------------------------------------
--------------------next---------------------
阅读(981) | 评论(0) | 转发(0) |