0) { if(dwbytesttransfered==1) { resp=rx; bReturn=TRUE; } else-bReturn=FALSE; } else-bReturn=FALSE; 回程; } void ClosePort() { 闭合手柄(hPort); 返回; } 句柄配置串行端口(LPCSTR lpszPortName) { 句柄hComm=NULL; 德沃德误差; DCB端口DCB; 通信超时通信超时; //打开串行端口。 hComm=CreateFile(lpszPortName,//指向端口名称的指针 一般的读,一般的写, //访问(读写)模式 0,//共享模式 NULL,//指向安全属性的指针 OPEN_EXISTING,//如何打开串口 0,//端口属性 NULL);//带属性的端口句柄 //抄袭 //初始化DCBlength成员。 PortDCB.DCBlength=sizeof(DCB); //获取默认端口设置信息。 GetCommState(hComm和PortDCB); //更改DCB结构设置。 PortDCB.BaudRate=9600;//当前波特率 PortDCB.fBinary=TRUE;//二进制模式;无EOF检查 PortDCB.fParity=TRUE;//启用奇偶校验 PortDCB.fOutxCtsFlow=FALSE;//无CTS输出流控制 PortDCB.fOutxDsrFlow=FALSE;//无DSR输出流控制 PortDCB.fDtrControl=DTR\u CONTROL\u ENABLE; //DTR流量控制类型 PortDCB.fDsrSensitivity=FALSE;//DSR灵敏度 PortDCB.ftxcontinuenoonxoff=TRUE;//XOFF继续发送 PortDCB.fOutX=FALSE;//没有XON/XOFF输出流控制 PortDCB.fInX=FALSE;//流控制中没有XON/XOFF PortDCB.fErrorChar=FALSE;//禁用错误替换 PortDCB.fNull=FALSE;//禁用空剥离 PortDCB.fRtsControl=RTS\u CONTROL\u ENABLE; //RTS流量控制 PortDCB.fAbortOnError=FALSE;//不要中止对的读取/写入 //错误 PortDCB.ByteSize=8;//位数/字节,4-8 PortDCB.Parity=NOPARITY;//0-4=no、奇数、偶数、标记、空格 PortDCB.StopBits=ONESTOPBIT;//0,1,2=1,1.5,2 //根据DCB的规范配置端口 //结构。 如果(!SetCommState(hComm和PortDCB)) { printf(“无法配置串行端口\n”); 返回NULL; } //检索所有读写操作的超时参数 //在港口。 GetCommTimeout(hComm和CommTimeout); //更改COMMTIMEOUTS结构设置。 CommTimeouts.ReadIntervalTimeout=MAXDWORD; CommTimeouts.ReadTotalTimeout乘数=0; CommTimeouts.ReadTotalTimeoutConstant=0; CommTimeouts.WriteTotalTimeout乘数=0; CommTimeouts.WriteTotalTimeoutConstant=0; 如果(!设置通信超时(hComm和通信超时)) { printf(“无法设置超时\n”); 返回NULL; } 返回hComm; } 内部主(空) { //也可以在此处使用COM2、COM3或COM4 hPort=ConfigureSerialPort(“\\.\\COM12”); if(hPort==NULL) { printf(“Com端口配置失败\n”); 返回-1; } //在此处调用ReadString和WriteString函数 字节b='m'; 写字节(b); ReadByte(b);//读取行我希望程序在此行上等待,直到至少出现一个字符(不是池)。 ClosePort(); 返回0; },c++,windows,serial-port,C++,Windows,Serial Port" /> 0) { if(dwbytesttransfered==1) { resp=rx; bReturn=TRUE; } else-bReturn=FALSE; } else-bReturn=FALSE; 回程; } void ClosePort() { 闭合手柄(hPort); 返回; } 句柄配置串行端口(LPCSTR lpszPortName) { 句柄hComm=NULL; 德沃德误差; DCB端口DCB; 通信超时通信超时; //打开串行端口。 hComm=CreateFile(lpszPortName,//指向端口名称的指针 一般的读,一般的写, //访问(读写)模式 0,//共享模式 NULL,//指向安全属性的指针 OPEN_EXISTING,//如何打开串口 0,//端口属性 NULL);//带属性的端口句柄 //抄袭 //初始化DCBlength成员。 PortDCB.DCBlength=sizeof(DCB); //获取默认端口设置信息。 GetCommState(hComm和PortDCB); //更改DCB结构设置。 PortDCB.BaudRate=9600;//当前波特率 PortDCB.fBinary=TRUE;//二进制模式;无EOF检查 PortDCB.fParity=TRUE;//启用奇偶校验 PortDCB.fOutxCtsFlow=FALSE;//无CTS输出流控制 PortDCB.fOutxDsrFlow=FALSE;//无DSR输出流控制 PortDCB.fDtrControl=DTR\u CONTROL\u ENABLE; //DTR流量控制类型 PortDCB.fDsrSensitivity=FALSE;//DSR灵敏度 PortDCB.ftxcontinuenoonxoff=TRUE;//XOFF继续发送 PortDCB.fOutX=FALSE;//没有XON/XOFF输出流控制 PortDCB.fInX=FALSE;//流控制中没有XON/XOFF PortDCB.fErrorChar=FALSE;//禁用错误替换 PortDCB.fNull=FALSE;//禁用空剥离 PortDCB.fRtsControl=RTS\u CONTROL\u ENABLE; //RTS流量控制 PortDCB.fAbortOnError=FALSE;//不要中止对的读取/写入 //错误 PortDCB.ByteSize=8;//位数/字节,4-8 PortDCB.Parity=NOPARITY;//0-4=no、奇数、偶数、标记、空格 PortDCB.StopBits=ONESTOPBIT;//0,1,2=1,1.5,2 //根据DCB的规范配置端口 //结构。 如果(!SetCommState(hComm和PortDCB)) { printf(“无法配置串行端口\n”); 返回NULL; } //检索所有读写操作的超时参数 //在港口。 GetCommTimeout(hComm和CommTimeout); //更改COMMTIMEOUTS结构设置。 CommTimeouts.ReadIntervalTimeout=MAXDWORD; CommTimeouts.ReadTotalTimeout乘数=0; CommTimeouts.ReadTotalTimeoutConstant=0; CommTimeouts.WriteTotalTimeout乘数=0; CommTimeouts.WriteTotalTimeoutConstant=0; 如果(!设置通信超时(hComm和通信超时)) { printf(“无法设置超时\n”); 返回NULL; } 返回hComm; } 内部主(空) { //也可以在此处使用COM2、COM3或COM4 hPort=ConfigureSerialPort(“\\.\\COM12”); if(hPort==NULL) { printf(“Com端口配置失败\n”); 返回-1; } //在此处调用ReadString和WriteString函数 字节b='m'; 写字节(b); ReadByte(b);//读取行我希望程序在此行上等待,直到至少出现一个字符(不是池)。 ClosePort(); 返回0; },c++,windows,serial-port,C++,Windows,Serial Port" />

从com端口rs232读取阻塞模式 我有一个C++代码,读写数据RS232,但我想它是阻塞模式,我希望它在读行上等待,直到接收到一个字符。这是我的代码: #include <windows.h> #include <stdio.h> #include <string.h> HANDLE hPort; BOOL WriteByte(BYTE bybyte) { DWORD iBytesWritten=0; DWORD iBytesToRead = 1; if(WriteFile(hPort,(LPCVOID) &bybyte,iBytesToRead,&iBytesWritten,NULL)==0) return FALSE; else return TRUE; } BOOL ReadByte(BYTE &resp) { BOOL bReturn = TRUE; BYTE rx; DWORD dwBytesTransferred=0; if (ReadFile (hPort, &rx, 1, &dwBytesTransferred, 0)> 0) { if (dwBytesTransferred == 1) { resp=rx; bReturn = TRUE; } else bReturn = FALSE; } else bReturn = FALSE; return bReturn; } void ClosePort() { CloseHandle(hPort); return; } HANDLE ConfigureSerialPort(LPCSTR lpszPortName) { HANDLE hComm = NULL; DWORD dwError; DCB PortDCB; COMMTIMEOUTS CommTimeouts; // Open the serial port. hComm = CreateFile (lpszPortName, // Pointer to the name of the port GENERIC_READ | GENERIC_WRITE, // Access (read-write) mode 0, // Share mode NULL, // Pointer to the security attribute OPEN_EXISTING, // How to open the serial port 0, // Port attributes NULL); // Handle to port with attribute // to copy // Initialize the DCBlength member. PortDCB.DCBlength = sizeof (DCB); // Get the default port setting information. GetCommState (hComm, &PortDCB); // Change the DCB structure settings. PortDCB.BaudRate = 9600; // Current baud PortDCB.fBinary = TRUE; // Binary mode; no EOF check PortDCB.fParity = TRUE; // Enable parity checking PortDCB.fOutxCtsFlow = FALSE; // No CTS output flow control PortDCB.fOutxDsrFlow = FALSE; // No DSR output flow control PortDCB.fDtrControl = DTR_CONTROL_ENABLE; // DTR flow control type PortDCB.fDsrSensitivity = FALSE; // DSR sensitivity PortDCB.fTXContinueOnXoff = TRUE; // XOFF continues Tx PortDCB.fOutX = FALSE; // No XON/XOFF out flow control PortDCB.fInX = FALSE; // No XON/XOFF in flow control PortDCB.fErrorChar = FALSE; // Disable error replacement PortDCB.fNull = FALSE; // Disable null stripping PortDCB.fRtsControl = RTS_CONTROL_ENABLE; // RTS flow control PortDCB.fAbortOnError = FALSE; // Do not abort reads/writes on // error PortDCB.ByteSize = 8; // Number of bits/byte, 4-8 PortDCB.Parity = NOPARITY; // 0-4=no,odd,even,mark,space PortDCB.StopBits = ONESTOPBIT; // 0,1,2 = 1, 1.5, 2 // Configure the port according to the specifications of the DCB // structure. if (!SetCommState (hComm, &PortDCB)) { printf("Could not configure serial port\n"); return NULL; } // Retrieve the time-out parameters for all read and write operations // on the port. GetCommTimeouts (hComm, &CommTimeouts); // Change the COMMTIMEOUTS structure settings. CommTimeouts.ReadIntervalTimeout = MAXDWORD; CommTimeouts.ReadTotalTimeoutMultiplier = 0; CommTimeouts.ReadTotalTimeoutConstant = 0; CommTimeouts.WriteTotalTimeoutMultiplier = 0; CommTimeouts.WriteTotalTimeoutConstant = 0; if (!SetCommTimeouts (hComm, &CommTimeouts)) { printf("Could not set timeouts\n"); return NULL; } return hComm; } int main(void) { // Can also use COM2, COM3 or COM4 here hPort = ConfigureSerialPort("\\\\.\\COM12"); if(hPort == NULL) { printf("Com port configuration failed\n"); return -1; } // Call your ReadString and WriteString functions here BYTE b = 'm'; WriteByte(b); ReadByte(b); //read line i want the program wait on this line until at least one character came(was not pooling). ClosePort(); return 0; } #包括 #包括 #包括 处理hPort; 布尔写字节(逐字节) { DWORD ibyteswrited=0; DWORD iBytesToRead=1; if(WriteFile(hPort,(LPCVOID) &bybyte、iBytesToRead和ibyteswrited,NULL)==0) 返回FALSE; 否则返回TRUE; } BOOL ReadByte(字节和响应) { BOOL-bReturn=TRUE; 字节rx; DWORD DWBYTESTTRANSFERRED=0; if(读取文件(hPort、&rx、1、&DWBYTESTTransfered、0)>0) { if(dwbytesttransfered==1) { resp=rx; bReturn=TRUE; } else-bReturn=FALSE; } else-bReturn=FALSE; 回程; } void ClosePort() { 闭合手柄(hPort); 返回; } 句柄配置串行端口(LPCSTR lpszPortName) { 句柄hComm=NULL; 德沃德误差; DCB端口DCB; 通信超时通信超时; //打开串行端口。 hComm=CreateFile(lpszPortName,//指向端口名称的指针 一般的读,一般的写, //访问(读写)模式 0,//共享模式 NULL,//指向安全属性的指针 OPEN_EXISTING,//如何打开串口 0,//端口属性 NULL);//带属性的端口句柄 //抄袭 //初始化DCBlength成员。 PortDCB.DCBlength=sizeof(DCB); //获取默认端口设置信息。 GetCommState(hComm和PortDCB); //更改DCB结构设置。 PortDCB.BaudRate=9600;//当前波特率 PortDCB.fBinary=TRUE;//二进制模式;无EOF检查 PortDCB.fParity=TRUE;//启用奇偶校验 PortDCB.fOutxCtsFlow=FALSE;//无CTS输出流控制 PortDCB.fOutxDsrFlow=FALSE;//无DSR输出流控制 PortDCB.fDtrControl=DTR\u CONTROL\u ENABLE; //DTR流量控制类型 PortDCB.fDsrSensitivity=FALSE;//DSR灵敏度 PortDCB.ftxcontinuenoonxoff=TRUE;//XOFF继续发送 PortDCB.fOutX=FALSE;//没有XON/XOFF输出流控制 PortDCB.fInX=FALSE;//流控制中没有XON/XOFF PortDCB.fErrorChar=FALSE;//禁用错误替换 PortDCB.fNull=FALSE;//禁用空剥离 PortDCB.fRtsControl=RTS\u CONTROL\u ENABLE; //RTS流量控制 PortDCB.fAbortOnError=FALSE;//不要中止对的读取/写入 //错误 PortDCB.ByteSize=8;//位数/字节,4-8 PortDCB.Parity=NOPARITY;//0-4=no、奇数、偶数、标记、空格 PortDCB.StopBits=ONESTOPBIT;//0,1,2=1,1.5,2 //根据DCB的规范配置端口 //结构。 如果(!SetCommState(hComm和PortDCB)) { printf(“无法配置串行端口\n”); 返回NULL; } //检索所有读写操作的超时参数 //在港口。 GetCommTimeout(hComm和CommTimeout); //更改COMMTIMEOUTS结构设置。 CommTimeouts.ReadIntervalTimeout=MAXDWORD; CommTimeouts.ReadTotalTimeout乘数=0; CommTimeouts.ReadTotalTimeoutConstant=0; CommTimeouts.WriteTotalTimeout乘数=0; CommTimeouts.WriteTotalTimeoutConstant=0; 如果(!设置通信超时(hComm和通信超时)) { printf(“无法设置超时\n”); 返回NULL; } 返回hComm; } 内部主(空) { //也可以在此处使用COM2、COM3或COM4 hPort=ConfigureSerialPort(“\\.\\COM12”); if(hPort==NULL) { printf(“Com端口配置失败\n”); 返回-1; } //在此处调用ReadString和WriteString函数 字节b='m'; 写字节(b); ReadByte(b);//读取行我希望程序在此行上等待,直到至少出现一个字符(不是池)。 ClosePort(); 返回0; }

从com端口rs232读取阻塞模式 我有一个C++代码,读写数据RS232,但我想它是阻塞模式,我希望它在读行上等待,直到接收到一个字符。这是我的代码: #include <windows.h> #include <stdio.h> #include <string.h> HANDLE hPort; BOOL WriteByte(BYTE bybyte) { DWORD iBytesWritten=0; DWORD iBytesToRead = 1; if(WriteFile(hPort,(LPCVOID) &bybyte,iBytesToRead,&iBytesWritten,NULL)==0) return FALSE; else return TRUE; } BOOL ReadByte(BYTE &resp) { BOOL bReturn = TRUE; BYTE rx; DWORD dwBytesTransferred=0; if (ReadFile (hPort, &rx, 1, &dwBytesTransferred, 0)> 0) { if (dwBytesTransferred == 1) { resp=rx; bReturn = TRUE; } else bReturn = FALSE; } else bReturn = FALSE; return bReturn; } void ClosePort() { CloseHandle(hPort); return; } HANDLE ConfigureSerialPort(LPCSTR lpszPortName) { HANDLE hComm = NULL; DWORD dwError; DCB PortDCB; COMMTIMEOUTS CommTimeouts; // Open the serial port. hComm = CreateFile (lpszPortName, // Pointer to the name of the port GENERIC_READ | GENERIC_WRITE, // Access (read-write) mode 0, // Share mode NULL, // Pointer to the security attribute OPEN_EXISTING, // How to open the serial port 0, // Port attributes NULL); // Handle to port with attribute // to copy // Initialize the DCBlength member. PortDCB.DCBlength = sizeof (DCB); // Get the default port setting information. GetCommState (hComm, &PortDCB); // Change the DCB structure settings. PortDCB.BaudRate = 9600; // Current baud PortDCB.fBinary = TRUE; // Binary mode; no EOF check PortDCB.fParity = TRUE; // Enable parity checking PortDCB.fOutxCtsFlow = FALSE; // No CTS output flow control PortDCB.fOutxDsrFlow = FALSE; // No DSR output flow control PortDCB.fDtrControl = DTR_CONTROL_ENABLE; // DTR flow control type PortDCB.fDsrSensitivity = FALSE; // DSR sensitivity PortDCB.fTXContinueOnXoff = TRUE; // XOFF continues Tx PortDCB.fOutX = FALSE; // No XON/XOFF out flow control PortDCB.fInX = FALSE; // No XON/XOFF in flow control PortDCB.fErrorChar = FALSE; // Disable error replacement PortDCB.fNull = FALSE; // Disable null stripping PortDCB.fRtsControl = RTS_CONTROL_ENABLE; // RTS flow control PortDCB.fAbortOnError = FALSE; // Do not abort reads/writes on // error PortDCB.ByteSize = 8; // Number of bits/byte, 4-8 PortDCB.Parity = NOPARITY; // 0-4=no,odd,even,mark,space PortDCB.StopBits = ONESTOPBIT; // 0,1,2 = 1, 1.5, 2 // Configure the port according to the specifications of the DCB // structure. if (!SetCommState (hComm, &PortDCB)) { printf("Could not configure serial port\n"); return NULL; } // Retrieve the time-out parameters for all read and write operations // on the port. GetCommTimeouts (hComm, &CommTimeouts); // Change the COMMTIMEOUTS structure settings. CommTimeouts.ReadIntervalTimeout = MAXDWORD; CommTimeouts.ReadTotalTimeoutMultiplier = 0; CommTimeouts.ReadTotalTimeoutConstant = 0; CommTimeouts.WriteTotalTimeoutMultiplier = 0; CommTimeouts.WriteTotalTimeoutConstant = 0; if (!SetCommTimeouts (hComm, &CommTimeouts)) { printf("Could not set timeouts\n"); return NULL; } return hComm; } int main(void) { // Can also use COM2, COM3 or COM4 here hPort = ConfigureSerialPort("\\\\.\\COM12"); if(hPort == NULL) { printf("Com port configuration failed\n"); return -1; } // Call your ReadString and WriteString functions here BYTE b = 'm'; WriteByte(b); ReadByte(b); //read line i want the program wait on this line until at least one character came(was not pooling). ClosePort(); return 0; } #包括 #包括 #包括 处理hPort; 布尔写字节(逐字节) { DWORD ibyteswrited=0; DWORD iBytesToRead=1; if(WriteFile(hPort,(LPCVOID) &bybyte、iBytesToRead和ibyteswrited,NULL)==0) 返回FALSE; 否则返回TRUE; } BOOL ReadByte(字节和响应) { BOOL-bReturn=TRUE; 字节rx; DWORD DWBYTESTTRANSFERRED=0; if(读取文件(hPort、&rx、1、&DWBYTESTTransfered、0)>0) { if(dwbytesttransfered==1) { resp=rx; bReturn=TRUE; } else-bReturn=FALSE; } else-bReturn=FALSE; 回程; } void ClosePort() { 闭合手柄(hPort); 返回; } 句柄配置串行端口(LPCSTR lpszPortName) { 句柄hComm=NULL; 德沃德误差; DCB端口DCB; 通信超时通信超时; //打开串行端口。 hComm=CreateFile(lpszPortName,//指向端口名称的指针 一般的读,一般的写, //访问(读写)模式 0,//共享模式 NULL,//指向安全属性的指针 OPEN_EXISTING,//如何打开串口 0,//端口属性 NULL);//带属性的端口句柄 //抄袭 //初始化DCBlength成员。 PortDCB.DCBlength=sizeof(DCB); //获取默认端口设置信息。 GetCommState(hComm和PortDCB); //更改DCB结构设置。 PortDCB.BaudRate=9600;//当前波特率 PortDCB.fBinary=TRUE;//二进制模式;无EOF检查 PortDCB.fParity=TRUE;//启用奇偶校验 PortDCB.fOutxCtsFlow=FALSE;//无CTS输出流控制 PortDCB.fOutxDsrFlow=FALSE;//无DSR输出流控制 PortDCB.fDtrControl=DTR\u CONTROL\u ENABLE; //DTR流量控制类型 PortDCB.fDsrSensitivity=FALSE;//DSR灵敏度 PortDCB.ftxcontinuenoonxoff=TRUE;//XOFF继续发送 PortDCB.fOutX=FALSE;//没有XON/XOFF输出流控制 PortDCB.fInX=FALSE;//流控制中没有XON/XOFF PortDCB.fErrorChar=FALSE;//禁用错误替换 PortDCB.fNull=FALSE;//禁用空剥离 PortDCB.fRtsControl=RTS\u CONTROL\u ENABLE; //RTS流量控制 PortDCB.fAbortOnError=FALSE;//不要中止对的读取/写入 //错误 PortDCB.ByteSize=8;//位数/字节,4-8 PortDCB.Parity=NOPARITY;//0-4=no、奇数、偶数、标记、空格 PortDCB.StopBits=ONESTOPBIT;//0,1,2=1,1.5,2 //根据DCB的规范配置端口 //结构。 如果(!SetCommState(hComm和PortDCB)) { printf(“无法配置串行端口\n”); 返回NULL; } //检索所有读写操作的超时参数 //在港口。 GetCommTimeout(hComm和CommTimeout); //更改COMMTIMEOUTS结构设置。 CommTimeouts.ReadIntervalTimeout=MAXDWORD; CommTimeouts.ReadTotalTimeout乘数=0; CommTimeouts.ReadTotalTimeoutConstant=0; CommTimeouts.WriteTotalTimeout乘数=0; CommTimeouts.WriteTotalTimeoutConstant=0; 如果(!设置通信超时(hComm和通信超时)) { printf(“无法设置超时\n”); 返回NULL; } 返回hComm; } 内部主(空) { //也可以在此处使用COM2、COM3或COM4 hPort=ConfigureSerialPort(“\\.\\COM12”); if(hPort==NULL) { printf(“Com端口配置失败\n”); 返回-1; } //在此处调用ReadString和WriteString函数 字节b='m'; 写字节(b); ReadByte(b);//读取行我希望程序在此行上等待,直到至少出现一个字符(不是池)。 ClosePort(); 返回0; },c++,windows,serial-port,C++,Windows,Serial Port,有什么想法吗?阻塞读取是ReadFile的正常行为,但您已通过将ReadTotalTimeoutConstant设置为MAXDWORD改变了这一点。研究MSDN页面上的通信超时。用零代替

有什么想法吗?

阻塞读取是ReadFile的正常行为,但您已通过将ReadTotalTimeoutConstant设置为MAXDWORD改变了这一点。研究MSDN页面上的通信超时。用零代替