本文介绍了串口通讯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 限时删除!! 嗨所有 i有一个名为nonin 9560(脉搏血氧仪)的设备,它可以通信蓝牙 i我正在使用Windows 7 visual 2008 c ++ ı有一个代码试图从设备读取数据(数据是ex 98的氧饱和度) bur问题是我无法读取数据! ReadFile不行! 请帮帮我........... 这里是代码: int main(int argc,char * argv []) { char INBUFFER [5000] = {'\0'}; DWORD bytes_read = 0; //从端口读取的字节数HANDLE comport = NULL; //处理COM端口 int bStatus; DCB comSettings; //包含各种端口设置 COMMTIMEOUTS CommTimeouts; //打开COM端口 if((comport = CreateFile(COM3:,//打开com5: GENERIC_READ | GENERIC_WRITE,//用于阅读和写 0,//独占访问 NULL,//没有安全属性 OPEN_EXISTING, 0, 0))== INVALID_HANDLE_VALUE) { printf(无法打开\ n); } / *设置超时(以毫秒为单位)* / GetCommTimeouts(comport,& CommTimeouts); //更改COMMTIMEOUTS结构设置。 CommTimeouts.ReadIntervalTimeout = MAXDWORD; CommTimeouts.ReadTotalTimeoutMultiplier = 0; CommTimeouts.ReadTotalTimeoutConstant = 0; CommTimeouts.WriteTotalTimeoutMultiplier = 10; CommTimeouts.WriteTotalTimeoutConstant = 1000; //在端口上设置所有读写操作 //的超时参数。 if(!SetCommTimeouts(comport,& CommTimeouts)) { printf(SetCommTimeouts Error\\\); } //设置端口参数。 //首先调用GetCommState(),以便用所有必要的值填充 // comSettings结构。 //然后更改你想要的并调用SetCommState()。 if(GetCommState(comport,& comSettings)== 0){ printf(GetCommState Error\\\); } 其他 { comSettings.BaudRate = 9600; comSettings.StopBits = ONESTOPBIT; comSettings.ByteSize = 8; comSettings.Parity = NOPARITY; comSettings.fParity = FALSE; bStatus = SetCommState(comport,& comSettings); try { if(bStatus == 0) { printf(SetCommState Error ... \ n); } BYTE字节; DWORD dwBytesTransferred; //从串口读取数据。 ReadFile(comport,& Byte,1,& dwBytesTransferred,0); //显示读取的数据。 if(dwBytesTransferred == 1){ ProcessChar(Byte); } CloseHandle(comport); } catch(例外e) { CloseHandle(comport); } } 返回0; } 我现在还有一个问题! 我再也无法打开com3了。 smt发生了。 任何其他程序,如oem评估程序,putty,hercules也不能再打开了。 他们正在打开精美的ysterday !! 解决方案 hi alli have a device called nonin 9560 (pulse oximeter) which communicate bluetooth i am working on windows 7 visual 2008 c++ı have a code tries to read data from the device(the data is the oxygen saturation for ex 98)bur the problem is i can not read the data!ReadFile does not work!please help me...........here is the code:int main(int argc, char* argv[]){char INBUFFER[5000] = {'\0'}; DWORD bytes_read = 0; // Number of bytes read from port HANDLE comport = NULL; // Handle COM portint bStatus; DCB comSettings; // Contains various port settings COMMTIMEOUTS CommTimeouts; // Open COM port if ((comport = CreateFile("COM3:", // open com5: GENERIC_READ | GENERIC_WRITE, // for reading and writing 0, // exclusive access NULL, // no security attributes OPEN_EXISTING, 0, 0)) == INVALID_HANDLE_VALUE) { printf("cannot open\n"); }/*Set timeouts in milliseconds*/GetCommTimeouts (comport, &CommTimeouts);// Change the COMMTIMEOUTS structure settings.CommTimeouts.ReadIntervalTimeout = MAXDWORD;CommTimeouts.ReadTotalTimeoutMultiplier = 0;CommTimeouts.ReadTotalTimeoutConstant = 0;CommTimeouts.WriteTotalTimeoutMultiplier = 10;CommTimeouts.WriteTotalTimeoutConstant = 1000;// Set the timeout parameters for all read and write operations// on the port.if (!SetCommTimeouts (comport, &CommTimeouts)){ printf("SetCommTimeouts Error\n");} //Set Port parameters. //Make a call to GetCommState() first in order to fill //the comSettings structure with all the necessary values. //Then change the ones you want and call SetCommState().if (GetCommState(comport, &comSettings)==0){printf("GetCommState Error\n");}else{comSettings.BaudRate = 9600;comSettings.StopBits = ONESTOPBIT;comSettings.ByteSize = 8;comSettings.Parity = NOPARITY;comSettings.fParity = FALSE;bStatus = SetCommState(comport, &comSettings);try {if (bStatus == 0){printf("SetCommState Error...\n");}BYTE Byte;DWORD dwBytesTransferred;// Read the data from the serial port.ReadFile (comport, &Byte, 1, &dwBytesTransferred, 0);// Display the data read.if (dwBytesTransferred == 1){ProcessChar (Byte);}CloseHandle(comport);}catch( exception e){CloseHandle(comport);}} return 0;}i have an additional problem now!i cannot open com3 anymore. smt happened.any other program like oem evaluation prog, putty, hercules can not open also anymore.they were openning fine ysterday!! 解决方案 这篇关于串口通讯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 1403页,肝出来的..
09-08 16:21