HANDLE hcom;
hcom=CreateFile("COM2",GENERIC_READ | GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);//COM根据实际情况改下
if(hcom !=INVALID_HANDLE_VALUE)
{
SetupComm(hcom,1024,512);
DCB mydcb;
GetCommState(hcom,&mydcb);
mydcb.BaudRate=CBR_9600;//波特率
mydcb.fBinary=TRUE;
mydcb.fParity=TRUE;//无校验位
mydcb.ByteSize=8;//数据位8位
mydcb.Parity=NOPARITY;
mydcb.StopBits=ONESTOPBIT;
SetCommState(hcom,&mydcb);
}
else
{
MessageBox("创建串口失败,您输入的串口可能不存在","提示");
}
unsigned long h;
unsigned char song[10];
CString ss;
ReadFile(hcom,song,9,&h,NULL);//读取串口内容