阅读:1787回复:0
串口发送数据出现停顿的问题
版主:
你好!我贴上打开串口和发送数据的函数,请你帮忙看一看。 //打开串口 /****************************************************************************\\ * * name Open_ComPort * * Opens the specified COM-Port with the specified Baudrate. * Communication parameter: 8 databits, 1 stopbit, no parity * * Inputs ComStr ... possible: \"COM1\", \"COM2\", \"COM3\", \"COM4\" * BaudStr ... possible: \"115200L\", \"57600L\", \"38400L\", \"28800L\", * \"19200L\", \"14400L\", \"9600L\" * * Return MI_INITERR, 0 ... 3 (Handle for COM1 ... COM4) * \\****************************************************************************/ int __stdcall Open_ComPort (unsigned __int16 ComStr, unsigned __int16 BaudStr) { // int base; DWORD dwError; DCB dcb; // char buf[80]; uchar sbuf; int e; LPCTSTR m_sPort; unsigned int ComInt=ComStr; COMMTIMEOUTS TimeOuts; e=0; sbuf=0; switch (ComInt) { case 0: m_sPort=\"COM1\"; break; case 1: m_sPort=\"COM2\"; break; case 2: m_sPort=\"COM3\"; break; case 3: m_sPort=\"COM4\"; break; default: // baudrate not supported return(MI_INITERR); } m_hCom=CreateFile(m_sPort, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,NULL); // 重叠方式 /* m_hCom=CreateFile(m_sPort, // 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 */ if(m_hCom==INVALID_HANDLE_VALUE)//检测打开串口操作是否成功 { dwError = GetLastError (); return(MI_INITERR); } SetupComm(m_hCom,INMAXBLOCK,OUTMAXBLOCK);//设置输入、输出缓冲区的大小 SetCommMask(m_hCom, EV_RXCHAR | EV_TXEMPTY);//设置事件驱动的类型 PurgeComm( m_hCom, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR ); //清干净输入、输出缓冲区 // Retrieve the time-out parameters for all read and write operations // on the port. GetCommTimeouts (m_hCom, &TimeOuts); // 把间隔超时设为最大,把总超时设为0将导致ReadFile立即返回并完成操作 TimeOuts.ReadIntervalTimeout=MAXDWORD; TimeOuts.ReadTotalTimeoutMultiplier=0; TimeOuts.ReadTotalTimeoutConstant=0; /* 设置写超时以指定WriteComm成员函数中的 GetOverlappedResult函数的等待时间*/ TimeOuts.WriteTotalTimeoutMultiplier=50; TimeOuts.WriteTotalTimeoutConstant=2000; SetCommTimeouts(m_hCom, &TimeOuts); dcb.DCBlength = sizeof (DCB); // Get the default port setting information. if(!GetCommState(m_hCom, &dcb)) { dwError = GetLastError (); CloseHandle(m_hCom); return(MI_INITERR); } // Change the DCB structure settings. dcb.fBinary=TRUE; // Binary mode; no EOF check // dcb.BaudRate=1200;//m_nBaud; // 波特率 // Current baud dcb.ByteSize=8;//m_nDataBits; // 每字节位数// Number of bits/bytes, 4-8 dcb.fParity=TRUE; // Enable parity checking. dcb.Parity=NOPARITY; // 0-4=no,odd,even,mark,space dcb.StopBits=ONESTOPBIT; // 0,1,2 = 1, 1.5, 2 dcb.fOutxCtsFlow = FALSE; // No CTS output flow control dcb.fOutxDsrFlow = FALSE; // No DSR output flow control dcb.fDtrControl = DTR_CONTROL_ENABLE; // DTR flow control type dcb.fDsrSensitivity = FALSE; // DSR sensitivity dcb.fTXContinueOnXoff = TRUE; // XOFF continues Tx dcb.fOutX = FALSE; // No XON/XOFF out flow control dcb.fInX = FALSE; // No XON/XOFF in flow control dcb.fErrorChar = FALSE; // Disable error replacement. dcb.fNull = FALSE; // Disable null stripping. dcb.fRtsControl = RTS_CONTROL_ENABLE; // RTS flow control dcb.fAbortOnError = FALSE; // Do not abort reads/writes on error. MIFS_Baud = BaudStr; switch (MIFS_Baud) { case 300L: dcb.BaudRate = CBR_300; // set baudrate via DCB-structure break; case 600L: dcb.BaudRate = CBR_600; // set baudrate via DCB-structure break; case 1200L: dcb.BaudRate = CBR_1200; // set baudrate via DCB-structure break; case 2400L: dcb.BaudRate = CBR_2400; // set baudrate via DCB-structure break; case 4800L: dcb.BaudRate = CBR_4800; // set baudrate via DCB-structure break; case 9600L: dcb.BaudRate = CBR_9600; // set baudrate via DCB-structure break; case 19200L: dcb.BaudRate = CBR_19200; // set baudrate via DCB-structure break; case 14400L: dcb.BaudRate = CBR_14400; // set baudrate via DCB-structure break; case 38400L: dcb.BaudRate = CBR_38400; // set baudrate via DCB-structure break; case 56000L: dcb.BaudRate = CBR_56000; // set baudrate via DCB-structure break; //case 28800L: case 57600L: dcb.BaudRate = CBR_57600; // set baudrate via DCB-structure break; case 115200L: dcb.BaudRate = CBR_115200; // set baudrate via DCB-structure break; default: // baudrate not supported CloseHandle(m_hCom); return(MI_INITERR); } if (SetCommState(m_hCom,&dcb) < 0) { dwError = GetLastError (); CloseHandle(m_hCom); return (MI_INITERR); } BCCMode = SER_BCC; // default ChkBytes = BCC_CHKBYTES; // default CHeckMode=CHecksum_1Byte; return (int(m_hCom));//(CMD_OK); } //发送数据 /****************************************************************************\\ * * name send_char * * transmits one character via API * * Inputs c ... character to transmit * * Return CMD_OK, MI_SERERR * \\****************************************************************************/ __int16 send_char (uchar *c,uchar slength) { BOOL fState; DWORD length=slength; COMSTAT ComStat; DWORD dwErrorFlags; ClearCommError(m_hCom,&dwErrorFlags,&ComStat); fState=WriteFile(m_hCom,c,length,&length,&m_osWrite); // fState=WriteFile(m_hCom,c,length,&length,NULL); // dwErrorFlags = GetLastError (); if(!fState) { if(GetLastError()==ERROR_IO_PENDING) GetOverlappedResult(m_hCom,&m_osWrite,&length,TRUE);// 等待 else return (MI_SERERR); } return (CMD_OK); } |
|