物联网 Win平台 串口读写程序

该程序实现了串口的读写功能

适用于windows平台

开发环境为:VS2019

 

运行效果 目前很简陋,以后有时间再做细化:

 

该次运行是通过虚拟串口软件虚拟出com3,com4端口

com3连接程序,com4连接的是一款串口软件AccessPort 配置与图片在本文后面

 

com4端的配置

 

 

 

 

代码如下


  1 #define _CRT_SECURE_NO_WARNINGS
  2 #include <stdio.h>
  3 #include <Windows.h>
  4 #include <atlstr.h>
  5 #include <strsafe.h>
  6 #include <crtdbg.h>
  7 
  8 int FILESIZE = 16;
  9 char szStr[5] = "com3";
 10 
 11 
 12 DWORD WriteFileContent(LPCWSTR szFilePath,DCB dcb)
 13 {
 14     HANDLE hFile;
 15     
 16     BYTE lpFileDataBuffer[1024];
 17     //lpFileDataBuffer[0] = 0xff;
 18     //lpFileDataBuffer[1] = 0xff;
 19     DWORD dwWritedSize = 0;
 20     DWORD dwWritedSizes;
 21     int i = 0;
 22     int ByteSize = 0;
 23 
 24     hFile = CreateFile(szFilePath,
 25         GENERIC_WRITE,
 26         FILE_SHARE_READ,
 27         NULL,
 28         OPEN_ALWAYS,
 29         FILE_ATTRIBUTE_NORMAL,
 30         NULL
 31     );
 32     if (hFile == INVALID_HANDLE_VALUE)
 33     {
 34         printf("打开%s失败!\n",szStr);
 35         return 0;
 36     }
 37 
 38     /*
 39         DCB配置
 40     */
 41     if (!SetCommState(hFile, &dcb))
 42     {
 43         printf("BCD配置设置失败!");
 44         CloseHandle(hFile);
 45         getchar();
 46         system("pause");
 47         return 0;
 48     }
 49 
 50     while (1)
 51     {
 52         printf("请输入数据字节数,0则退出写入:\n>> ");
 53         scanf("%d", &ByteSize);
 54 
 55         if (ByteSize == 0)
 56         {
 57             break;
 58         }
 59             
 60         printf("请输入数据,其间空格隔开:\n>> ");
 61         for (i = 0; i < ByteSize; i++)
 62         {
 63             scanf("%x", &lpFileDataBuffer[dwWritedSize++]);
 64         }
 65 
 66         dwWritedSize = ByteSize;
 67 
 68         if (!WriteFile(hFile,
 69             lpFileDataBuffer,
 70             dwWritedSize,
 71             &dwWritedSizes,
 72             NULL))
 73         {
 74             printf("写入文件错误: %d\n", GetLastError());
 75             break;
 76         }
 77         printf("写入%d字节\n>> ", dwWritedSize);
 78 
 79 
 80         for (i = 0; i < dwWritedSize; i++)
 81         {
 82             printf("%x ", lpFileDataBuffer[i]);
 83         }
 84         printf("\n");
 85         dwWritedSize = 0;
 86 
 87         Sleep(1000);
 88     }
 89     CloseHandle(hFile);
 90     return 0;
 91 }
 92 
 93 DWORD ReadFileContent(LPCWSTR szFilePath, DCB dcb)
 94 {
 95     HANDLE hFile;
 96 
 97     BYTE lpFileDataBuffer[1024];
 98     DWORD dwReadedSize;
 99     DWORD i;
100     DWORD Time = 0;
101 
102     
103 
104     hFile = CreateFile(szFilePath,
105         GENERIC_READ,
106         FILE_SHARE_WRITE,
107         NULL,
108         OPEN_EXISTING,
109         FILE_ATTRIBUTE_NORMAL,
110         NULL);
111     
112     if (hFile == INVALID_HANDLE_VALUE)
113     {
114         printf("打开%s失败!\n",szStr);
115         return 0;
116     }
117 
118     /*
119         DCB配置
120     */
121     if (!SetCommState(hFile, &dcb))
122     {
123         printf("BCD配置设置失败!");
124         CloseHandle(hFile);
125         return 0;
126     }
127 
128     /*
129         设置超时
130     */
131     COMMTIMEOUTS timeOver;
132     memset(&timeOver, 0, sizeof(timeOver));
133     DWORD timeMultiplier = 100, timeConstant = 5000;
134     timeOver.ReadTotalTimeoutMultiplier = timeMultiplier;
135     timeOver.ReadTotalTimeoutConstant = timeConstant;
136     SetCommTimeouts(hFile, &timeOver);
137     
138 
139     while (1)
140     {
141         if (!ReadFile(hFile,
142             lpFileDataBuffer,
143             FILESIZE,
144             & dwReadedSize,
145             NULL))
146         {
147             printf("读取文件错误: %d\n", GetLastError());
148             break;
149         }
150         printf("读取%d字节\n>> ",dwReadedSize);
151         if (dwReadedSize == 0)
152         {
153             ++Time;
154             if (Time == 1)
155             {
156                 printf("读取数据停止!\n");
157                 break;
158             }
159         }
160         else
161         {
162             printf("读取%d字节\n>> ", dwReadedSize);
163         }
164         
165         for (i = 0; i < dwReadedSize; i++)
166         {
167             printf(" %x ", lpFileDataBuffer[i]);
168         }
169         printf("\n");
170 
171         Sleep(1000);
172     }
173     CloseHandle(hFile);
174     return 0;
175 }
176 
177 void SetDcb(DCB* dcb)
178 {
179     /*
180         DCB结构配置
181         BauRate        (波特率)
182         fParity        (指定奇偶校验位)
183         Parity        (校验方式)
184         ByteSize    (数据位个数)
185         StopBits    (停止位个数)
186     */
187     int No = 0;
188 
189     dcb->DCBlength = sizeof(DCB);
190 
191     while (1)
192     {
193         printf("  DCB结构配置\n");
194         printf("0.设置:串口编号\n");
195         printf("1.设置:波特率\n");
196         printf("2.设置:检验方式\n");
197         printf("3.设置:停止位数\n");
198         printf("4.设置:数据位个数\n");
199         printf("5.设置:读取时一组的字节数\n");
200         printf("6.查看:当前结构\n");
201         printf("7.退出\n");
202         printf(">> ");
203 
204         scanf("%d",& No);
205         switch (No) 
206         {
207             case 0:
208             {
209                 printf("请输入串口编号 例如1:\n>> ");
210                 getchar();
211                 scanf("%c", &szStr[3]);
212                 printf("串口编号成功改为:%s\n", szStr);
213                 break;
214             }
215             case 1:
216             {
217                 printf("波特率:\n>> ");
218                 scanf("%d", &dcb->BaudRate);
219                 printf("波特率成功改为:%d\n", dcb->BaudRate);
220                 break;
221             }
222             case 2:
223             {
224                 printf("2 :偶校验    EVENPARITY\n");    //    EVENPARITY
225                 printf("3 :标志校验    MARKPARITY\n");    //    MARKPARITY    
226                 printf("0 :无校验    NOPARITY\n");    //    NOPARITY
227                 printf("1 :奇校验    ODDPARITY\n");    //    ODDPARITY
228                 printf(">> ");
229                 scanf("%d", &dcb->Parity);
230                 printf("校验法成功更改为:%d\n", dcb->Parity);
231                 break;
232             }
233             case 3:
234             {
235                 printf("0 :1停止位        ONESTOPBIT\n");    //    ONESTOPBIT
236                 printf("1 :1.5停止位    ONE5STOPBITS\n");    //    ONE5STOPBITS
237                 printf("2 :2停止位        TWOSTOPBITS\n");    //    TWOSTOPBITS   
238                 printf(">> ");
239                 scanf("%d", &dcb->StopBits);
240                 printf("停止位成功更改为:%d\n", dcb->StopBits);
241                 break;
242             }
243             case 4:
244             {
245                 printf("数据位个数:\n>> ");
246                 scanf("%d", &dcb->ByteSize);
247                 printf("数据位个数成功更改为:%d\n", dcb->ByteSize);
248                 break;
249             }
250             case 5:
251             {
252                 printf("接收时每组字节数设置:\n>> ");
253                 scanf("%d", &FILESIZE);
254                 printf("接收时每组字节数成功更改为:%d", FILESIZE);
255                 break;
256             }
257             case 6:
258             {
259                 printf("  DCB结构配置\n");
260                 printf("0.串口编号        %s\n",szStr);
261                 printf("1.波特率        %d\n", dcb->BaudRate);
262                 printf("2.检验方式        ");//%d\n", dcb->Parity);
263                 switch (dcb->Parity)
264                 {
265                     case 0:
266                     {
267                         printf("NOPARITY    无校验\n");
268                         break;
269                     }
270                     case 1:
271                     {
272                         printf("ODDPARITY    奇校验\n");
273                         break;
274                     }
275                     case 2:
276                     {
277                         printf("EVENPARITY    偶校验\n");
278                         break;
279                     }
280                     case 3:
281                     {
282                         printf("MARKPARITY    标志校验\n");
283                         break;
284                     }
285                 }
286                 
287                 printf("3.停止位数        ");// % d\n", dcb->StopBits);
288                 if (dcb->StopBits == 0)
289                     printf("ONESTOPBIT    1停止位\n");
290                 else if (dcb->StopBits == 1)
291                     printf("ONE5STOPBITS    1.5停止位\n");
292                 else if (dcb->StopBits == 2)
293                     printf("TWOSTOPBITS    2停止位\n");
294                 else
295                     printf("dcb->ByteSize = %d\n", dcb->StopBits);
296 
297                 printf("4.数据位个数        %d    位\n", dcb->ByteSize);
298                 printf("5.字节数        %d    字节\n", FILESIZE);
299                 break;
300             }
301             case 7:
302             {
303                 printf("退出配置\n");
304                 return;
305             }
306             default:
307             {
308                 printf("输入有误\n");
309                 break;
310             }
311         }
312         printf("完毕\n");
313     }
314 
315 }
316 
317 int main()
318 {
319     
320     int Change = 0;
321 
322     DCB dcb;
323     memset(&dcb, 0, sizeof(dcb));
324 
325     /* 
326         将char[]类型转化成LPCWSTR类型
327     */
328 
329     WCHAR wszClassName[8];
330     memset(wszClassName, 0, sizeof(wszClassName));
331     MultiByteToWideChar(CP_ACP, 0, szStr, strlen(szStr) + 1, wszClassName,
332         sizeof(wszClassName) / sizeof(wszClassName[0]));
333 
334     LPCWSTR lpFileName = _T("com3"); //= wszClassName;
335     
336     /*
337         DCB结构配置
338         BauRate        (波特率)
339         fParity        (指定奇偶校验位)
340         Parity        (校验方式)
341         ByteSize    (数据位个数)
342         StopBits    (停止位个数)
343     */
344     printf("DCB结构正在默认配置...");
345     dcb.DCBlength = sizeof(DCB);
346     dcb.BaudRate = 9600;
347     dcb.Parity = 2;
348     dcb.ByteSize = 8;
349     dcb.StopBits = ONESTOPBIT;
350     printf("完成\n");
351 
352     
353 
354     while (1)
355     {    
356         printf("\n  <Win串口读写程序>\n");
357         printf("1.读取数据\n");
358         printf("2.写入数据\n");
359         printf("3.配置DCB结构体\n");
360         printf("4.结束程序\n");
361         printf(">> ");
362         scanf_s("%d", &Change);
363         if (Change == 1)
364         {
365             ReadFileContent(lpFileName,dcb);
366         }
367         else if (Change == 2)
368         {
369             WriteFileContent(lpFileName,dcb);
370         }
371         else if (Change == 3)
372         {
373             SetDcb(&dcb);
374         }
375         else if (Change == 4)
376         {
377             printf("程序结束\n");
378             break;
379         }
380         else
381             printf("输入错误,请重新输入!\n");
382     }
383     
384     return 0;
385 }

 

 

目前时间紧迫,以后在做注释

参考文献

串口DCB结构体详解 :

https://blog.csdn.net/wangshubo1989/article/details/47746401

使用win32API实现windows下的一步串口通讯

https://www.cnblogs.com/icemencc/p/6098606.html

使用windowsAPI进行串口编程

https://www.cnblogs.com/milanleon/p/4244267.html

c语言中的串口读写功能实现

https://blog.csdn.net/mrsama/article/details/77140443

posted on 2019-12-15 21:33  刀客塔的哲学三问  阅读(682)  评论(0)    收藏  举报