使用fgets()在C / C ++中读取comport [英] using fgets() to read comport in C/C++

查看:105
本文介绍了使用fgets()在C / C ++中读取comport的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个程序来通过comport发送命令并接收响应。目前我可以打开/关闭并写入comport,但无法让fgets()从comport中读取一行。目前程序在将字符串发送到comport后崩溃。我正在使用windows(visual studio 2008)。



以下代码



谢谢





  #include     stdafx.h 
#include < string.h >
#include < conio.h >
#include < stdlib.h >

int m ain( void

{
char str [ 80 ];
char rstr [ 80 ];
char buff [ 256 ];

FILE * comport;
if ((comport = fopen( COM1 wt))== NULL)
{
printf( 无法打开通讯端口COM1 \ n);
printf( 端口可能被禁用或正在使用\ n);
printf( 输入\quit \退出\ n) ;
getch();
}
else
{
printf( COM1已成功打开\ n);

for (;;)
{
printf( 输入字符串\\\
);
fgets(str, 80 ,stdin);
printf( \ n%s \ n,str);
fprintf(comport, %s \ n,str);
fflush(comport);

fgets(buff, sizeof buff,comport);

strcpy(rstr,buff);
printf( s%\ n,rstr);

}
fclose(comport);


}
return 0 ;
}

解决方案

你应该检查的返回值fgets(),我猜它会为NULL,因为你打开com端口只是为了写。在不成功的fgets之后,接下来的2行很容易崩溃。而不是wt尝试r +作为文件模式打开com1进行读取和写入并检查fgets的返回值。


这是旧的,但以防万一其他人是看看这个......发布的代码中有很多错误。



1)缓冲区溢出风险(高):

 strcpy( rstr,buff); 



因为buff大于rstr



2)无穷无尽的(;; )循环。使用while(!feof(stdin)&&!feof(comport)),以便当其中一个流处于EOF时你可以突破并点击fclose。



3)当COM端口忙时,你不需要输入退出退出,getch()只需要等待按键。



4 )在getch()之后返回0,这样你就不需要一个else块。



5)正如已经指出的那样,检查来自的返回值与fgets。如果nullptr你需要退出循环(如果你遵循#2,则继续执行。)



6)我强烈建议使用std :: array< char ,>而不是char []缓冲区。与strcpy_s或sprintf_s一起使用数组的.size()成员,你将有更少的溢出风险。



并且,不要堆积,但是这是一种奇怪的编码风格。 ;)


I'm trying write a program to send commands over the comport and receive a response. At the moment I can open/close and write to the comport but can't get fgets() to work reading a line in from the comport. At the moment the program crashes after sending the string to the comport. I am using windows (visual studio 2008).

code below

Thanks


#include "stdafx.h"
#include <string.h>
#include <conio.h>
#include <stdlib.h>

int main (void)

{
	char str[80];
	char rstr[80];
	char buff[256];

	FILE *comport;
	if ((comport = fopen("COM1", "wt")) == NULL)
		{
			printf("Failed to open the communication port COM1\n");
			printf("The port may be disabled or in use\n");
			printf("enter \"quit\" to exit \n");
			getch();
		}
	else
		{
		printf("COM1 opended successfully\n");
		
			for(;;)
			{
				printf ("enter a string \n");
				fgets (str , 80 , stdin);						
				printf ("\n%s\n",str);
				fprintf (comport, "%s\n",str);
				fflush(comport);

				fgets(buff, sizeof buff, comport);  
				
				strcpy(rstr, buff );
				printf("s%\n",rstr);
											
			}
		fclose(comport);
		

		}
	return 0;
}

解决方案

You should check the return value of fgets(), I guess it would be NULL because you opened the com port only for writing. After the unsuccessful fgets the next 2 lines can easily crash. Instead of "wt" try "r+" as file mode to open com1 for both reading and writing and check the return value of fgets.


This is old, but just in case anyone else is looking at this... There are numerous bugs in the posted code.

1) Buffer Overflow risk (high):

strcpy(rstr, buff );


Because buff is bigger than rstr

2) An endless for(;;) loop. Use while(!feof(stdin) && !feof(comport)) so that you can break out and hit the fclose when one of the streams is at EOF.

3) When the COM port is busy you don't need to enter "quit" to exit, getch() just waits for a keypress.

4) Put a return 0 after the getch() so that you don't need an else block.

5) As has been pointed out, check the return values from fgets. If nullptr you need to drop out of the loop (continue will do it if you follow #2.)

6) I highly suggest using std::array<char,> instead of char [] buffers. Along with strcpy_s or sprintf_s, using the .size() member of the array, you will be at less risk of overflows.

And, not to pile on, but that is a weird coding style. ;)


这篇关于使用fgets()在C / C ++中读取comport的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆