本文介绍了无法为串行通信编译win API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我收到这些错误 hSerial未指定类型< br /> 预期的非限定标识符 #include < windows.h > HANDLE hSerial; // 这里的错误// hSerial = createFile(COM4, GENERIC_READ | GENERIC_WRITE, 0 , 0 , OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0 ); // 此处出错// if(hSerial == INVALID_HANDLE_VALUE) { if (GetLastError()= ERROR_FILE_NOT_FOUND){ printf( // SERIAL PORT不存在//); } printf( //发生其他一些错误.// ); } DCB dcbSerialParams = { 0 }; dcbSerial.DCBlength = Sizeof(dcbSerialParams); if (!GetCommState(hSerial,& dcbSerialParams)) { Printf( 获取状态时出错); } dcbSerialParams.BaudRate = CBR_9600; dcbSerialParams.ByteSize = 8 ; dcbSerialParams.StopBits = ONESTOPBIT; dcbSerialParams.Parity = NOPARITY; if (!SetCommState(hSerial,& dcbSerialParams)){ // 错误设置串口状态 printf( 错误); } COMMTIMEOUTS超时= { 0 }; timeouts.ReadIntervalTimeout = 50 ; timeouts.ReadTotalTimeoutConstant = 50 ; timeouts.ReadTotalTimeoutMultiplier = 10 ; timeouts.WriteTotalTimeoutConstant = 50 ; timeouts.WriteTotalTimeoutMultiplier = 10 ; if (!SetCommTimeouts(hSerial,& timeouts)){ // 发生错误。通知用户 printf( 超时发生错误); } char szBuff [n + 1 ] = { 0 }; DWORD dwBytesRead = 0 ; if (!ReadFile(hSerial,szBuff,n,& dwBytesRead,NULL)){ // 发生错误。向用户报告。 printf( 读/写字节出错); } CloseHandle(hSerial); 解决方案 I am getting these errorshSerial does not name a type <br />expected unqualified identifiers before if#include<windows.h>HANDLE hSerial; // error here// hSerial = createFile ("COM4", GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);// error here// if(hSerial==INVALID_HANDLE_VALUE){ if(GetLastError()=ERROR_FILE_NOT_FOUND){ printf("//SERIAL PORT DOES NOT EXIST// "); } printf("//SOME OTHER ERROR OCCURED.//");}DCB dcbSerialParams = {0};dcbSerial.DCBlength=Sizeof(dcbSerialParams);if (!GetCommState(hSerial, &dcbSerialParams)){ Printf("Error in getting state");}dcbSerialParams.BaudRate=CBR_9600;dcbSerialParams.ByteSize=8;dcbSerialParams.StopBits=ONESTOPBIT;dcbSerialParams.Parity=NOPARITY;if(!SetCommState(hSerial, &dcbSerialParams)){//error setting serial port stateprintf("Error");}COMMTIMEOUTS timeouts={0};timeouts.ReadIntervalTimeout=50;timeouts.ReadTotalTimeoutConstant=50;timeouts.ReadTotalTimeoutMultiplier=10;timeouts.WriteTotalTimeoutConstant=50;timeouts.WriteTotalTimeoutMultiplier=10;if(!SetCommTimeouts(hSerial, &timeouts)){//error occureed. Inform userprintf("Error occuring in timeouts");}char szBuff[n + 1] = {0};DWORD dwBytesRead = 0;if(!ReadFile(hSerial, szBuff, n, &dwBytesRead, NULL)){//error occurred. Report to user.printf("Error occured in read/write byte");}CloseHandle(hSerial); 解决方案 这篇关于无法为串行通信编译win API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-19 19:52