Exemplo n.º 1
0
bool RS485::receive(frame &frame_receive){

//读取的字节数
	DWORD wCount;
	//读取状态
	int bReadStat = 0;
	char byte_buf = 0;
	char *frame_length = new char[2];
	char frame_type = 0;

//	printf("开始读取\n");
	//读取起始位
	ReadFile(hCom,&byte_buf,1,&wCount,NULL);
	if (byte_buf != 0x10)
	{
//		printf("未读取到起始位0x10\n");
		return false;
	}
	ReadFile(hCom,&byte_buf,1,&wCount,NULL);
	if(byte_buf != 0x02){
//		printf("未读取到到起始位0x02\n");
		return false;
	}
	//读取帧长度
	ReadFile(hCom,frame_length,2,&wCount,NULL);

	//读取帧类型
	ReadFile(hCom, &frame_type, 1,&wCount,NULL);
	frame_receive.set_type(frame_type);

	//读取帧
	ReadFile(hCom, frame_receive.getFrame() + 5, frame_receive.getLength() -5,&wCount,NULL);

	return true;
}
Exemplo n.º 2
0
bool RS485::send(frame &frm){
	DWORD dwBytesWrite=frm.getLength();
	COMSTAT ComStat;
	DWORD dwErrorFlags;
	BOOL bWriteStat;
	ClearCommError(hCom,&dwErrorFlags,&ComStat);
	bWriteStat=WriteFile(hCom,frm.getFrame(),dwBytesWrite,& dwBytesWrite,NULL);
	if(!bWriteStat)
	{
		std::cout<<"写串口失败"<<std::endl;
		return false;
	}
	return true;
}