资源预览内容
第1页 / 共25页
第2页 / 共25页
第3页 / 共25页
第4页 / 共25页
第5页 / 共25页
第6页 / 共25页
第7页 / 共25页
第8页 / 共25页
第9页 / 共25页
第10页 / 共25页
亲,该文档总共25页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
+串口 API 应用 嵌 入 式 发布时间:2008-06-14 12:50:15 /*initialSerial功能:串口初始化参数:无返回:无*/void initialSerial() /串口初始化char szComParams50;DCB dcb;char *m_com; char *m_baud; char *m_jiaoyan;m_com=Com1;m_baud=1200;m_jiaoyan=E;COMMTIMEOUTS CommTimeOuts;m_hIDComDev = NULL;m_hIDComDev = CreateFile(m_com, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); /打开串口if(m_hIDComDev=INVALID_HANDLE_VALUE)AfxMessageBox(打开串口错误 0,请检查!);goto endd;if(m_hIDComDev =(HANDLE) -1)AfxMessageBox(打开串口错误,请检查!); goto endd;SetCommTimeouts(m_hIDComDev, /串口超时配置CommTimeOuts. ReadIntervalTimeout=0xFFFFFFFF;CommTimeOuts. ReadTotalTimeoutMultiplier = 0;CommTimeOuts. ReadTotalTimeoutConstant =5000;CommTimeOuts. WriteTotalTimeoutMultiplier = 0;CommTimeOuts. WriteTotalTimeoutConstant = 5000; PurgeComm(m_hIDComDev, PURGE_TXABORT|PURGE_RXABORT|PURGE_TXCLEAR|PURGE_RXCLEAR ) ; m_com=Com1:38400,E,8,1;wsprintf(szComParams, m_com); /设置串口参数 dcb. DCBlength = sizeof(DCB);GetCommState(m_hIDComDev, /int baud;baud = atoi(m_baud);dcb. BaudRate = baud; /设置波特率 dcb. ByteSize= 8; /设置校验字节if (!SetCommState(m_hIDComDev, &dcb)|(!SetupComm(m_hIDComDev,10000,10000)/设置串口和收发缓冲器的大小DWORD dwError = GetLastError();CloseHandle(m_hIDComDev);PurgeComm(m_hIDComDev,PURGE_RXCLEAR|PURGE_TXCLEAR|PURGE_TXABORT|PURGE_RXABORT);/清收发缓冲器endd:;/*SendData功能:发送数据给串口参数: buff 发送的数据send_length 长度返回: 成功 1 失败 0*/DWORD SendData( unsigned char buff,int send_length) /发送数据 int t;DWORD dwBytesWritten;if(!WriteFile(m_hIDComDev,buff,send_length,&dwBytesWritten,NULL)return 0; for (t=0;t0) ClearCommError(m_hIDComDev, if(stat.cbInQue 【第二步】设置缓冲区大小if(!SetupComm(m_hCom,2048,2048)cout【第三步】设置超时COMMTIMEOUTS TimeOuts;memset(TimeOuts.ReadIntervalTimeout = MAXDWORD;TimeOuts.ReadTotalTimeoutConstant = 0;TimeOuts.ReadTotalTimeoutMultiplier = 0;TimeOuts.WriteTotalTimeoutConstant = 2000;TimeOuts.WriteTotalTimeoutMultiplier = 50;SetCommTimeouts(m_hCom,【第四步】设置串口参数DCB dcb;if (!GetCommState(m_hCom,&dcb)coutGetCommState fail! Comm closeENDL;CloseHandle(m_hCom);return -1;coutGetCommState OK!ENDL;dcb.DCBlength = sizeof(dcb);if (!BuildCommDCB(9600,n,8,1,&dcb)/填充的数据传输率、奇偶校验类型、数据位、停止位/参数修改错误,进行错误处理coutBuileCOmmDCB fail,Comm close!ENDL;CloseHandle(m_hCom);return -1;if(SetCommState(m_hCom,&dcb)coutSetCommState OK!ENDL;【第五步】建立并初始化重叠结构OVERLAPPED wrOverlapped;ZeroMemory(if (wrOverlapped.hEvent != NULL)ResetEvent(wrOverlapped.hEvent);wrOverlapped.hEvent = CreateEvent(NULL,TRUE,FALSE,NULL);【第六步】封装数据(按照自己的格式封装需要发送的数据,此步可以省略)typedef enumHEAT_BEAT, /心跳数据NET_STATE,/网络状态数据PACKET /正常数据包/支持可扩展性.ProtocolType;typedef enumTrain_No,/无线车次信息Attemper_Command,/调度命令信息Revert_Command,/调度命令回执信息Replay_Command,/重发的调度命令信息KGL_SING /开关量数据/支持可扩展性.PacketDataType; /串口数据结构typedef struct SerialNetProtounsigned long PacketSize; /包总长度,不包括本身字段ProtocolType NetState; /协议包类型PacketDataType DataType; /数据类型unsigned long SourcedAddr; /数据包源地址unsigned long DestinationAddr; /数据包目的地址unsigned long DataLength; /包的数据段长度unsigned long Offset; / 数据在整个包中的偏移地址PacketHead;int DataLen = 100;char *pBuf = new charDataLen;strcpy(pBuf,Hello World!);DataLen = strlen(pBuf);PacketHead Myhead;Myhead.DestinationAddr = 11;Myhead.SourcedAddr = 10;Myhead.DataType = Attemper_Command;Myhead.DataLength = DataLen;Myhead.NetState = PACKET;Myhead.PacketSize = sizeof(PacketHead) - sizeof(unsigned long);Myhead.Offset = sizeof(Myhead.DestinationAddr) +sizeof(Myhead.SourcedAddr) + sizeof(Myhead.DataType) +sizeof(Myhead.DataLength) + sizeof(Myhead.NetState) + sizeof(Myhead.PacketSize); char *pSendBuffer = new charsizeof(Myhead)+DataLen+ 4;/发送的数据memcpy(pSendBuffer,#,2);/包头标志memcpy(pSendBuffer+2,(char*)/包头memcpy(pSendBuffer+2+sizeof(Myhead),pBuf,DataLen);/数据memcpy(pSendBuffer+2+sizeof(Myhead)+DataLen,2);/包尾标志【第七步】发送数据DWORD dwError;/DWORD dwWantSend = 100;DWORD dwRealSend = 0;char* pReadBuf = NULL;if (ClearCommError(m_hCom,&dwError,NULL)PurgeComm(m_hCom,PURGE_TXABORT | PURGE_TXCLEAR);coutPurgeComm OK!ENDL;if (!WriteFile(m_hCom,pSendBuffer,sizeof(Myhead)+DataLen+ 4,&dwRealSend,&wrOverlapped)if (GetLastError() = ERROR_IO_PENDING)while (!GetOverlappedResult(m_hCom,&wrOverlapped,&dwRealSend,FALSE)if (GetLastError() = ERROR_IO_INCOMPLETE)/cout写未完成,继续!ENDL;continue;elsecout发生错误,尝试恢复!ENDL;ClearCommError(m_hCom,break;【第八步】数据接收DWORD dwError;DWORD dwWantRead = 100;DWORD dwRealRead = 0;char* pReadBuf = new char100;
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号