简易串口读取代码
来源:互联网 发布:下线推广 源码 编辑:程序博客网 时间:2024/05/19 16:21
/********CopyRight@piaoxiang.zhang*******/#include <stdio.h>#include <stdlib.h>#include <windows.h>#define COM "COM9"/******must change here by your com_id*******/#undef DEBUG_ENABLE #ifdef DEBUG_ENABLE #define Debug(fmt,args...) printf("[zpx debug] "fmt"\n",##args) #else #define Debug(fmt,args...)#endif#define MAX_RANGE(x,y) ((x)>(y)?(y):(x))static int image_full_flag=0;unsigned char buff[256*256];int read_com(HANDLE hFile){ DWORD dwNumRead=0; DWORD dwErr=0; int b=2,i; static int firstFrame=1; static int pixSum=0; COMSTAT comStat; b=ClearCommError(hFile,&dwErr,&comStat); if(comStat.cbInQue==0)return 1; Debug("b=%d,dwErr=%d,comStat.cbInQue=%d",b,(int)dwErr,(int)comStat.cbInQue); OVERLAPPED readOverlapped; readOverlapped.hEvent=CreateEvent(NULL,TRUE,FALSE,NULL); //Sleep(10); b=ReadFile(hFile,buff,comStat.cbInQue,&dwNumRead,&readOverlapped); Debug("ReadFile=%d",b); if(!b) { if(GetLastError()==ERROR_IO_PENDING){ Debug("GetLastError==ERR_IO_PENDING==%ld",ERROR_IO_PENDING); GetOverlappedResult(hFile,&readOverlapped,&dwNumRead,TRUE); } else { Debug("GetLastError()==%d",(int)GetLastError()); free(buff); return 0; } } Debug("dwNumRead=%d",(int)dwNumRead); Debug("\n==================show msg==========================\n"); for(i=0;i<comStat.cbInQue;i++) printf("%X ",buff[i]); Debug("\n==================show msg==========================\n");/******you should set MY_DATA_PROCESS undef**/#define MY_DATA_PROCESS#ifdef MY_DATA_PROCESS pixSum+=comStat.cbInQue; if(firstFrame!=0) { if(comStat.cbInQue==1) { if(firstFrame==2) { firstFrame=0; if(pixSum==96*96*2+2) { image_full_flag=1; printf("\ndon't skip first frame\n"); pixSum=0 ; return 1; } pixSum=0; return 0; } if(buff[comStat.cbInQue-1]>=0xfd)firstFrame=2 ; } else if(buff[comStat.cbInQue-1]==0xfd&&buff[comStat.cbInQue-2]==0xfe) { firstFrame=0; if(pixSum==96*96*2+2) { image_full_flag=1; printf("\ndon't skip first frame\n"); } else printf("\nskip first frame\n"); pixSum=0; } } else{ if(pixSum==96*96*2+2){ //must remove 2Byte data in the end ; pixSum=0; image_full_flag=1; printf("\nget full image\n"); } }#endif return 1;} void SetDcbInfo(HANDLE hFile,OVERLAPPED waitOverlapped){ int b; DWORD dwMask=EV_RXCHAR; while(1) { SetEvent(waitOverlapped.hEvent) ; WaitCommEvent(hFile,&dwMask,&waitOverlapped); WaitForSingleObject(waitOverlapped.hEvent,INFINITE); ResetEvent(waitOverlapped.hEvent); if(dwMask==EV_RXCHAR) { b=read_com(hFile); if(b==1) break; } } } int main(int argc,char **argv){ static HANDLE hFile; int getFileHandle=-1,cnt=0; while(1) { hFile=CreateFile(COM,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED,NULL); getFileHandle=(int)hFile; if(getFileHandle!=-1)break; Debug("Can't get %s %d times,Please check the Device!",COM,cnt++); Sleep(1000); } Debug("hFile=%d",(int)hFile); DCB *dcb=(DCB*)malloc(1*sizeof(DCB)); GetCommState(hFile,(LPDCB)dcb); dcb->BaudRate=CBR_57600; dcb->ByteSize=8;//数据位8bit dcb->StopBits=ONESTOPBIT;//停止位 1bit dcb->Parity=NOPARITY;// 校验位 SetCommState(hFile,(LPDCB)dcb); GetCommState(hFile,(LPDCB)dcb); PurgeComm(hFile,PURGE_RXCLEAR); SetCommMask(hFile,EV_RXCHAR); printf("wait for rs232 event\n"); OVERLAPPED waitOverlapped; waitOverlapped.hEvent=CreateEvent(NULL,TRUE,FALSE,NULL); for(;;) { SetDcbInfo(hFile,waitOverlapped); if(image_full_flag) { image_full_flag=0; break; } } Debug("get rs232 event"); system("pause"); free(dcb); return 0;}
验证结果:
0 0
- 简易串口读取代码
- 简易串口通信协议代码sep4020
- qt5串口简易操作
- 简易串口助手
- HC-SR04 超声波测距模块 51串口读取代码
- HC-SR04 超声波测距模块 51串口读取代码
- STC15系列读取MPU6050陀螺仪角度加速度串口显示代码
- STC15系列读取DS18B20温度传感器串口显示代码
- 简易读取xml文件
- 简易bmp图片读取
- MFC读取串口数据
- c#读取串口
- 关于读取串口
- C#读取计算机串口
- 读取串口数据画图
- 线程读取串口数据
- MFC读取串口数据
- 读取串口数据画图
- 常见数据库操作经验-数据库函数-删除索引
- ATSHA204a加密芯片在TG401上的移植配置与使用
- 简单的jsp页面
- Node.js 使用async模块
- 必须记住这几种庄家洗盘的形态
- 简易串口读取代码
- 几个零散的数学点
- MySQL 主从复制与读写分离概念及架构分析
- asp.net请求处理流程
- spring-创建 Bean 后置处理器
- Spring高级事务管理
- Android语音开发-听筒扬声器自动切换
- canvas旋转,缩放,位移动画
- 做了一个工科生涨姿势的号