3645|10

85

帖子

0

TA的资源

一粒金砂(初级)

楼主
 

关于串口的几个关键函数 [复制链接]

This function reads data from a file, starting at the position indicated by the file pointer. After the read operation has been completed, the file pointer is adjusted by the number of bytes actually read.

BOOL ReadFile(
  HANDLE hFile,
  LPVOID lpBuffer,
  DWORD nNumberOfBytesToRead,
  LPDWORD lpNumberOfBytesRead,
  LPOVERLAPPED lpOverlapped
);
Parameters
hFile
[in] Handle to the file to be read. The file handle must have been created with GENERIC_READ access to the file. This parameter cannot be a socket handle.
lpBuffer
[out] Pointer to the buffer that receives the data read from the file.
nNumberOfBytesToRead
[in] Number of bytes to be read from the file.
lpNumberOfBytesRead
[out] Pointer to the number of bytes read. ReadFile sets this value to zero before taking action or checking errors.
lpOverlapped
[in] Unsupported; set to NULL.
Return Values
Nonzero indicates success. Zero indicates failure. To get extended error information, call GetLastError.

If the return value is nonzero and the number of bytes read is zero, the file pointer was beyond the current end of the file at the time of the read operation.

最新回复

呵呵 楼主可以自己开个博客   详情 回复 发表于 2010-5-19 13:26
点赞 关注

回复
举报

90

帖子

0

TA的资源

一粒金砂(初级)

沙发
 
 
 

回复

73

帖子

0

TA的资源

一粒金砂(初级)

板凳
 
不知道提问什么,因为我自己也有很多模糊的地方,发帖只是为了留下自己学习的痕迹。
———————————————————————————————————————
This function waits for an event to occur for a specified communications device.

The set of events monitored by WaitCommEvent is contained in the event mask associated with the device handle.

BOOL WaitCommEvent(
  HANDLE hFile,
  LPDWORD lpEvtMask,
  LPOVERLAPPED lpOverlapped
);
Parameters
hFile
[in] Handle to the communications device.
The CreateFile function returns this handle.

lpEvtMask
[out] Long pointer to a 32-bit variable that receives a mask indicating the events that occurred.
If an error occurs, the value is zero; otherwise, it is one or more of the following values.

Value Description
EV_BREAK A break was detected on input.
EV_CTS The CTS (clear-to-send) signal changed state.
EV_DSR The DSR (data-set-ready) signal changed state.
EV_ERR A line-status error occurred. Line-status errors are CE_FRAME, CE_OVERRUN, and CE_RXPARITY.
EV_POWER Power event, which is generated whenever the device is powered on.
This value is specific to Windows CE.

EV_RING A ring indicator was detected.
EV_RLSD The RLSD (receive-line-signal-detect) signal changed state.
EV_RXCHAR A character was received and placed in the input buffer.
EV_RXFLAG The event character was received and placed in the input buffer.
The event character is specified in the device's DCB structure, which is applied to a serial port by using the SetCommState function.

EV_TXEMPTY The last character in the output buffer was sent.

lpOverlapped
[in] Ignored; set to NULL.
Return Values
Nonzero indicates success.

Zero indicates failure.

To obtain extended error information, call the GetLastError function.

Remarks
The WaitCommEvent function monitors a set of events for a specified communications resource. To set and query the current event mask of a communications resource, use the SetCommMask and GetCommMask functions. When a communications event that is set by SetCommMask occurs, WaitCommEvent returns.

Only one WaitCommEvent can be used for each open COM port handle. This means that if you have three threads in your application and each thread needs to wait on a specific comm event, each thread needs to open the COM port and then use the assigned port handle for their respective WaitCommEvent calls.
——
从上面看,WaitCommEvent  只有有一个字符接收到就会产生了。
在串口应用程序中,要怎么和ReadFile配合使用才能不导致读出的数据有误呢。
在一段串口程序有
  
  1. DWORD MonitorCommEventProc(LPVOID pParam)   
  2. {   
  3.         InterlockedExchange(reinterpret_cast(&g_bMonitorProcRunning),TRUE);   
  4.    
  5.         RETAILMSG(TRUE,(TEXT("[VSP]:MonitorCommEventProc Running!\r\n")));   
  6.   
  7.         BYTE vtBufRead[READ_BUFFER_LENGTH];
  8.         while(TRUE)   
  9.         {   
  10.                 DWORD dwEvtMask = 0;   
  11.                 BOOL bWaitRes = WaitCommEvent(g_hCom,&dwEvtMask,NULL);      
  12. //RETAILMSG(TRUE,(TEXT("[VSP0]: dwEvtMask=0x%x,g_dwWaitMask=0x%x,g_bExitMonitorProc=0x%x\r\n"),dwEvtMask,g_dwWaitMask,g_bExitMonitorProc));
  13.                 if(g_bExitMonitorProc != FALSE)   
  14.                 {   
  15.                         break;   
  16.                 }   
  17.         
  18.                 if(bWaitRes == FALSE)   
  19.                 {   
  20.                         Sleep(1);
  21.                         continue;   
  22.                 }     
  23.                 DWORD dwRead = 0;      
  24.                 if(dwEvtMask & EV_RXCHAR)   
  25.                 {   
  26.                         EnterCriticalSection(&g_csRead);        
  27.                         if(!ReadFile(g_hCom,vtBufRead,READ_BUFFER_LENGTH,&dwRead,NULL))    //vtBufRead.size()
  28.                         {
  29.                                 RETAILMSG(TRUE,(TEXT("[VSP]: GetLastError code=0x%x\r\n"),GetLastError()));
  30.                         }       
  31.                         //data len size is the same as buffer len or not have read
  32.                         if(dwRead == READ_BUFFER_LENGTH || g_bReaded != FALSE)   
  33.                         {            
  34.                                 memcpy(g_vtBufRead,vtBufRead,dwRead);//g_vtBufRead.swap(vtBufRead);
  35.                                 g_dwLenReadBuf = dwRead;  
  36.                         }
  37.                         //add new data follow the old data
  38.                         else if(dwRead != 0)   
  39.                         {   
  40.                                 if(g_dwLenReadBuf + dwRead <= READ_BUFFER_LENGTH)   
  41.                                 {  
  42.                                         memcpy(g_vtBufRead+g_dwLenReadBuf,vtBufRead,dwRead);
  43.                                         g_dwLenReadBuf += dwRead;  
  44.                                 }   
  45.                                 else  
  46.                                 {   
  47.                                         DWORD dwCover = g_dwLenReadBuf + dwRead - READ_BUFFER_LENGTH;
  48.                                         memcpy(g_vtBufRead,g_vtBufRead+dwCover,g_dwLenReadBuf-dwCover);
  49.                                         memcpy(g_vtBufRead+g_dwLenReadBuf-dwCover,vtBufRead,dwRead);   
  50.                                         g_dwLenReadBuf = READ_BUFFER_LENGTH;                                        
  51.                                 }   
  52.                         }   
  53.                         g_bReaded = FALSE;   
  54.                         RETAILMSG(TRUE,(TEXT("[VSP]:Read data : %d\r\n"),dwRead));  
  55.        
  56.                         LeaveCriticalSection(&g_csRead);   
  57.                 }   
  58.                 if(dwEvtMask == EV_RXCHAR && ((g_dwWaitMask & EV_RXCHAR) == 0 || dwRead == 0))   
  59.                 {   
  60.                         //The return event mask is only EV_RXCHAR and there is not EV_RXCHAR in the wait mask.   
  61.                         continue;   
  62.                 }   
  63.                 InterlockedExchange(reinterpret_cast(&g_dwEvtMask),dwEvtMask);   
  64.                 PulseEvent(g_hEventComm);     
  65.                 //Sleep for other thread to respond to the event   
  66.                 //Sleep(100);  // mask at 2010.05.17 by wogo
  67.      
  68.                 DEBUGMSG(TRUE,(TEXT("[VSP]:PulseEvent! The event-mask is 0x%x\r\n"),dwEvtMask));      
  69.         }   
  70.    
  71.         RETAILMSG(TRUE,(TEXT("[VSP]:Exit the MonitorCommEventProc\r\n")));   
  72.         InterlockedExchange(reinterpret_cast(&g_bMonitorProcRunning),FALSE);   
  73.    
  74.         return 0;   
  75. }   
复制代码




 
 
 

回复

68

帖子

0

TA的资源

一粒金砂(初级)

4
 
if(dwRead == READ_BUFFER_LENGTH || g_bReaded != FALSE)   
            {        
                memcpy(g_vtBufRead,vtBufRead,dwRead);//g_vtBufRead.swap(vtBufRead);
                g_dwLenReadBuf = dwRead;  
            }
            //add new data follow the old data
            else if(dwRead != 0)   
            {   
                if(g_dwLenReadBuf + dwRead <= READ_BUFFER_LENGTH)   
                {  
                    memcpy(g_vtBufRead+g_dwLenReadBuf,vtBufRead,dwRead);
                    g_dwLenReadBuf += dwRead;  
                }   
                else  
                {   
                    DWORD dwCover = g_dwLenReadBuf + dwRead - READ_BUFFER_LENGTH;
                    memcpy(g_vtBufRead,g_vtBufRead+dwCover,g_dwLenReadBuf-dwCover);
                    memcpy(g_vtBufRead+g_dwLenReadBuf-dwCover,vtBufRead,dwRead);   
                    g_dwLenReadBuf = READ_BUFFER_LENGTH;                     
                }   
感觉这里有多此一举的感觉。先修改试试。
 
 
 

回复

62

帖子

0

TA的资源

一粒金砂(初级)

5
 
路过接分
 
 
 

回复

63

帖子

0

TA的资源

一粒金砂(初级)

6
 
串口的确是Wince设备最常用到的接口了。
要实现大数据量,长时间运行仍然稳定可靠
还是要费点脑筋的。
 
 
 

回复

70

帖子

0

TA的资源

一粒金砂(初级)

7
 
还以为LZ是来问问题的?
 
 
 

回复

57

帖子

0

TA的资源

一粒金砂(初级)

8
 
增增人气……
 
 
 

回复

81

帖子

0

TA的资源

一粒金砂(初级)

9
 
WaitCommEvent的返回值的意义。

//
// Events
//

#define EV_RXCHAR           0x0001  // Any Character received
#define EV_RXFLAG           0x0002  // Received certain character
#define EV_TXEMPTY          0x0004  // Transmitt Queue Empty
#define EV_CTS              0x0008  // CTS changed state
#define EV_DSR              0x0010  // DSR changed state
#define EV_RLSD             0x0020  // RLSD changed state
#define EV_BREAK            0x0040  // BREAK received
#define EV_ERR              0x0080  // Line status error occurred
#define EV_RING             0x0100  // Ring signal detected
#define EV_PERR             0x0200  // Printer error occured
#define EV_RX80FULL         0x0400  // Receive buffer is 80 percent full
#define EV_EVENT1           0x0800  // Provider specific event 1
#define EV_EVENT2           0x1000  // Provider specific event 2
#define EV_POWER                        0x2000  // WINCE Power event.
 
 
 

回复

81

帖子

0

TA的资源

一粒金砂(初级)

10
 
MonitorCommEventProc-dwEvtMask =0x11
-COM_READ: returning 9 (total 9, dropped 0,0)
[VSP]:MonitorCommEventProc--dwRead=9
[VSP]:MonitorCommEventProc--g_dwLenReadBuf=9
MonitorCommEventProc-dwEvtMask =0x1
-COM_READ: returning 64 (total 73, dropped 0,0)
[VSP]:MonitorCommEventProc--dwRead=64
[VSP]:MonitorCommEventProc--g_dwLenReadBuf=73
MonitorCommEventProc-dwEvtMask =0x1
-COM_READ: returning 64 (total 137, dropped 0,0)
[VSP]:MonitorCommEventProc--dwRead=64
[VSP]:MonitorCommEventProc--g_dwLenReadBuf=137
MonitorCommEventProc-dwEvtMask =0x1
-COM_READ: returning 64 (total 201, dropped 0,0)
[VSP]:MonitorCommEventProc--dwRead=64
[VSP]:MonitorCommEventProc--g_dwLenReadBuf=201
MonitorCommEventProc-dwEvtMask =0x1
-COM_READ: returning 25 (total 226, dropped 0,0)
[VSP]:MonitorCommEventProc--dwRead=25
[VSP]:MonitorCommEventProc--g_dwLenReadBuf=226
[VSP]:COM_Read--Copy cout:dwCopy=226
 
 
 

回复

78

帖子

0

TA的资源

一粒金砂(初级)

11
 
呵呵
楼主可以自己开个博客
 
 
 

回复
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/7 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表