1 #if !defined(_COMM_ACCESS_FUNCTIONS_AND_DATA)
2 #define _COMM_ACCESS_FUNCTIONS_AND_DATA
3 #if _MSC_VER > 1000
4 #pragma once
5 #endif // _MSC_VER > 1000
6 #define EVENTCHAR 0x0d
7 #define MAXBLOCKLENGTH 59
8
9 extern BYTE XwCom;
10 extern BYTE sCom1[5],sCom2[MAXBLOCKLENGTH+12];
11 extern sCom3[MAXBLOCKLENGTH+12];
12 extern BYTE opation;
13 extern short ComNum;
14
15 #define FC_DTRDSR 0x01
16 #define FC_RTSCTS 0x02
17 #define FC_XONXOFF 0x04
18 #define ASCII_BEL 0x07
19 #define ASCII_BS 0x08
20 #define ASCII_LF 0x0A
21 #define ASCII_CR 0x0D
22 #define ASCII_XON 0x11
23 #define ASCII_XOFF 0x13
24
25 class CComStatus
26 {
27 public:
28 HANDLE m_hCom;
29 BYTE m_bComId;
30 BYTE m_bByteSize;
31 BYTE m_bStopBits;
32 BYTE m_bParity;
33 DWORD m_dwBaudRate;
34
35 //WORD m_fChEvt;
36
37 char m_bEvtChar;
38 DWORD m_fBinary;
39 BOOL m_bConnected;
40 BOOL m_fXonXoff;
41 BOOL m_bFlowCtrl;
42 OVERLAPPED m_rdos;
43 OVERLAPPED m_wtos;
44
45 //functions
46
47 CComStatus();
48 CComStatus(BYTE bComId,BYTE bByteSize,BYTE bStopBits,BYTE bParity,
49 DWORD dwBaudRate,/*WORD fChEvt,*/char bEvtChar,DWORD fBinary);
50 BOOL OpenConnection();
51 BOOL CloseConnection();
52 BOOL SetupConnection();
53 BOOL IsConnected();
54 };
55
56 UINT CommWatchProc( LPVOID lpData );
57 BOOL WriteCommBlock( CComStatus& comDev, LPSTR lpByte , DWORD dwBytesToWrite);
58 int ReadCommBlock(CComStatus& comDev,LPSTR lpszBlock, int nMaxLength );
59 int ReadCommBlockEx(CComStatus& comDev,LPSTR lpszBlock, int nMaxLength,DWORD dwTimeOut);
60 #endif
61
62 ///////////////////////////////////////////////////////////////////////
63
64 #include "stdafx.h"
65 #include "com232.h"
66
67 BYTE XwCom=0x40;
68 BYTE sCom1[5],sCom2[MAXBLOCKLENGTH+12],sCom3[MAXBLOCKLENGTH+12];
69 BYTE opation;
70 short ComNum;
71 CComStatus::CComStatus()
72 {
73 m_hCom = NULL;
74 m_bComId = (char)ComNum;//COM1
75 m_bByteSize=8;
76 m_bStopBits=ONESTOPBIT;
77 m_bParity=NOPARITY;
78 m_dwBaudRate=9600;
79 m_bEvtChar=EVENTCHAR;
80 m_fBinary=1;
81 m_bConnected = FALSE;
82 m_bFlowCtrl = FC_XONXOFF ;
83 m_fXonXoff = FALSE;
84 }
85
86 CComStatus::CComStatus(BYTE bComId,BYTE bByteSize,BYTE bStopBits,BYTE bParity,DWORD dwBaudRate,/*WORD fChEvt,*/char bEvtChar,DWORD fBinary)
87 {
88 m_hCom = NULL;
89 m_bComId = bComId;
90 m_bByteSize=bByteSize;
91 m_bStopBits=bStopBits;
92 m_bParity=bParity;
93 m_dwBaudRate=dwBaudRate;
94 m_bEvtChar=bEvtChar;
95 m_fBinary=fBinary;
96 m_bConnected = FALSE;
97 m_bFlowCtrl = FC_XONXOFF ;
98 m_fXonXoff = FALSE;
99 }
100
101 BOOL CComStatus::OpenConnection()
102 {
103 char csCom[10];
104 COMMTIMEOUTS CommTimeOuts ;
105 if((m_bComId < 0) || (m_bComId > 4))
106 return FALSE;//从COM1到COM4
107 if(m_hCom)//if already open
108 return FALSE;
109
110 //OVERLAPPED包含异步I/O信息
111
112 m_rdos.Offset = 0;
113 m_rdos.OffsetHigh = 0;
114 m_rdos.hEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
115 if(m_rdos.hEvent == NULL)
116 return FALSE;
117 m_wtos.Offset = 0;
118 m_wtos.OffsetHigh = 0;
119 m_wtos.hEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
120 if(m_wtos.hEvent == NULL)
121 {
122 CloseHandle(m_rdos.hEvent);
123 return FALSE;
124 }
125
126 wsprintf(csCom,"COM%d",m_bComId);
127
128 m_hCom = CreateFile(csCom,GENERIC_READ | GENERIC_WRITE, 0,NULL, OPEN_EXISTING,ILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,NULL);
129
130 if(m_hCom == INVALID_HANDLE_VALUE) {
131 //dwError = GetLastError();
132 // handle error
133 return FALSE;
134 }
135 else
136 {
137 SetCommMask( m_hCom, EV_RXCHAR ) ; // get any early notifications
138 SetupComm( m_hCom, 4096, 4096 ) ; // setup device buffers
139 // purge any information in the buffer
140
141 PurgeComm( m_hCom, PURGE_TXABORT | PURGE_RXABORT |PURGE_TXCLEAR | PURGE_RXCLEAR ) ;
142
143 // set up for overlapped I/O
144
145 DWORD dwTemp = 1000 / (this->m_dwBaudRate / 8);
146 CommTimeOuts.ReadIntervalTimeout = 0xFFFFFFFF ;
147 CommTimeOuts.ReadTotalTimeoutMultiplier = 0;//((dwTemp > 0) ? dwTemp : 1);
148 CommTimeOuts.ReadTotalTimeoutConstant = 1000 ;
149
150 // CBR_9600 is approximately 1byte/ms. For our purposes, allow
151 // double the expected time per character for a fudge factor.
152
153 CommTimeOuts.WriteTotalTimeoutMultiplier =2*CBR_9600/this->m_dwBaudRate;//( npTTYInfo ) ;
154 CommTimeOuts.WriteTotalTimeoutConstant = 0;//1000 ;
155
156 SetCommTimeouts( m_hCom, &CommTimeOuts ) ;
157 }
158 if(!SetupConnection())
159 {
160 CloseConnection();
161 return FALSE;
162 }
163 EscapeCommFunction( m_hCom, SETDTR );
164 m_bConnected = TRUE;
165 return TRUE;
166 }
167
168 BOOL CComStatus::CloseConnection()
169 {
170 if (NULL == m_hCom)
171 return ( TRUE ) ;
172 // set connected flag to FALSE
173 m_bConnected = FALSE;
174 // disable event notification and wait for thread
175 // to halt
176 SetCommMask( m_hCom, 0 ) ;
177 EscapeCommFunction( m_hCom, CLRDTR ) ;
178 // purge any outstanding reads/writes and close device handle
179 PurgeComm( m_hCom, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR ) ;
180 CloseHandle( m_hCom ) ;
181 m_hCom = NULL;
182
183 // change the selectable items in the menu
184
185 CloseHandle(m_rdos.hEvent);
186 CloseHandle(m_wtos.hEvent);
187 return ( TRUE ) ;
188 }
189
190 BOOL CComStatus::SetupConnection()
191 {
192 BOOL fRetVal ;
193 BYTE bSet ;
194 DCB dcb ;
195 if(m_hCom == NULL)
196 return FALSE;
197 dcb.DCBlength = sizeof( DCB ) ;
198 GetCommState( m_hCom, &dcb ) ;
199 dcb.BaudRate = this->m_dwBaudRate;
200 dcb.ByteSize = this->m_bByteSize;
201 dcb.Parity = this->m_bParity;
202 dcb.StopBits = this->m_bStopBits ;
203 dcb.EvtChar = this->m_bEvtChar ;
204 // setup hardware flow control
205 bSet = (BYTE) ((m_bFlowCtrl & FC_DTRDSR) != 0) ;
206 dcb.fOutxDsrFlow = bSet ;
207 if (bSet)
208 dcb.fDtrControl = DTR_CONTROL_HANDSHAKE ;
209 else
210 dcb.fDtrControl = DTR_CONTROL_ENABLE ;
211 bSet = (BYTE) ((m_bFlowCtrl & FC_RTSCTS) != 0) ;
212 dcb.fOutxCtsFlow = bSet ;
213 if (bSet)
214 dcb.fRtsControl = RTS_CONTROL_HANDSHAKE ;
215 else
216 dcb.fRtsControl = RTS_CONTROL_ENABLE ;
217 // setup software flow control
218 bSet = (BYTE) ((m_bFlowCtrl & FC_XONXOFF) != 0) ;
219 dcb.fInX = dcb.fOutX = bSet ;
220 dcb.XonChar = ASCII_XON ;
221 char xon = ASCII_XON ;
222 dcb.XoffChar = ASCII_XOFF ;
223 char xoff = ASCII_XOFF ;
224 dcb.XonLim = 100 ;
225 dcb.XoffLim = 100 ;
226 // other various settings
227 dcb.fBinary = TRUE ;
228 dcb.fParity = TRUE ;
229 fRetVal = SetCommState( m_hCom, &dcb ) ;
230 return ( fRetVal ) ;
231 } // end of SetupConnection()
232
233 BOOL CComStatus::IsConnected()
234 {
235 return m_bConnected;
236 }
237
238 UINT CommWatchProc( LPVOID lpData )
239 {
240 DWORD dwEvtMask ;
241 //NPTTYINFO npTTYInfo = (NPTTYINFO) lpData ;
242 OVERLAPPED os ;
243 int nLength ;
244 //BYTE abIn[ MAXBLOCK + 1] ;
245
246 CComStatus * pCom = (CComStatus *)lpData;
247 memset( &os, 0, sizeof( OVERLAPPED ) ) ;
248 // create I/O event used for overlapped read
249
250 os.hEvent = CreateEvent( NULL, // no security
251 TRUE, // explicit reset req
252 FALSE, // initial event reset
253 NULL ) ; // no name
254
255 if (os.hEvent == NULL)
256 {
257 MessageBox( NULL, "Failed to create event for thread!", "TTY Error!",MB_ICONEXCLAMATION | MB_OK ) ;
258 return ( FALSE ) ;
259
weixin_42651887
- 粉丝: 103
- 资源: 1万+
最新资源
- c语言种地要浇水游戏源码.zip
- 计及复杂约束的非凸经济调度问题的双模态模拟植物生长算法
- 软件项目开发总结范文ppt
- c语言自创军旗游戏源码.zip
- 三菱FX1N PLC 485与三菱变频器modbus通讯可直接拿来实用了,三菱FX PLC与三菱变频器通讯 采用器件:三菱FX1N PLC,FX1N485BD板,1台三菱E740变频器,三菱FX2N
- c语言自创推箱子游戏改版.zip
- c语言做的播放器源码.zip
- labview转子动平衡采集与分析源码,信号源支持ni采集卡,串口采集卡,仿真
- 食堂点餐微信小程序源码(高分项目).zip
- php+mysql期末大作业
- c语言做的绘图板系统.zip
- OC-Dialect线上多语言(多列表) 3.SDK每个方法解析
- 食堂点餐微信小程序源码(高分毕业设计项目)
- c语言做的一个任务管理器.zip
- 一款高效的Vue 3低代码表单、工作流表单,包含表单设计器和表单渲染器,可视化设计,一键生成源码,基于VForm3的全栈低代码平台
- 纯c语言迷宫源码.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
评论0