gpt4 book ai didi

Linux 2.6.23 。接收错误。读取函数返回-1

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:59:06 25 4
gpt4 key购买 nike

请引用以下代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <termios.h>

#define BAUDRATE B115200
#define SER_DEVICE "/dev/ttyS0"
#define FALSE 0
#define TRUE 1


int main()
{
int fd,c,res,i,n;
struct termios oldtio,newtio;
unsigned char buf[255] = "WELCOME TO THE WORLD OF LINUX PROGRAMMING\n";
unsigned char buf2[255]= {"\0"};
//Opening a Device for Reading Writing.
//O_NOCTTY : - The Port Never Becomes the Controlling Terminal of the Process.
//O_NDELAY : - Use NON-Blocking IO. on some system this also means Deactivating the DCD line.

fd=open("/dev/ttyS0",O_RDWR | O_NOCTTY | O_NDELAY);

if(fd<0)
{
printf("\nError in opening the File\n");
exit(0);
}
else
{
printf("File Opened SuccessFull..HurraYYY !!!!1\n");
}

//printf("--------------Test Begin---------------\n");

//Save Current Serial Port Settings

tcgetattr(fd,&oldtio);
//clear the struct for New port settings
memset(&newtio,0,sizeof(newtio));

//Baud rate : Set bps rate .
//You could also use cfsetispeed and cfsetospeed.
//CRTSCTS : Output Hardware Flow control
//CS8 : 8n1(8bit No Parity 1 Stopbit)
//CLOCAL : local connection no modem control
//CREAD : Enable Receiving character
//printf("Setting Configuration for Port");
newtio.c_cflag |= (BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD);

//IGNPAR : Ignore bytes with parity error.
//ICRNL : map CR to NL
//printf("Setting Parity\n");
newtio.c_cflag |= (IGNPAR | ICRNL);

//RAW output
//printf("Raw Output\n");
newtio.c_oflag = 0;
//printf("Enabling Canonical format \n");
//ICANON : Enable canonical input.
newtio.c_lflag |= ICANON;
//printf("Initialising Char\n");
//Initialise all characters
newtio.c_cc[VMIN] = 1; /*Blocking read until one character arrives*/
newtio.c_cc[VTIME] = 0; /*Inter character timer unused*/

/*
Now clean the Modem Line and Activate the Settings for the Port.
*/
tcflush(fd,TCIFLUSH);
printf("Flushing Lines\n");
tcsetattr(fd,TCSANOW,&newtio);


n=write(fd,&buf,42);
printf("n=%d",n);


for(i=0;i<sizeof(unsigned int);i++);

for(i=0;i<sizeof(unsigned int);i++);
for(i=0;i<sizeof(unsigned int);i++);
for(i=0;i<200;i++)
printf("");
n=0;
n = read(fd,&buf2,42);
if(n==-1)
{
printf("\nError in Receiving");
}
else
printf("Received String = %s",buf2);

/*
Restore the Old Port Setting
*/

tcsetattr(fd,TCSANOW,&oldtio);

printf("==============TEST END==============\n");

close(fd);
}

我能够传输出现在 super 终端上的字符串。但是函数 Read 返回值为 -1。我发现的可能性是:1. 接收配置错误。2.是否需要环回。

我试过循环返回但它不起作用。我在 while(1) 中执行了代码

transmit ans Receive ... and if read returns something != -1 ..break from the Loop. But that to doesn't work. What is the minimum delay that one should add in read/write cycle.

我正在 MPC 8641d 处理器上执行这段代码。

请您的建议对我很重要。

希望得到您的指导!!!! :)

最佳答案

要了解 read() 失败的详细原因,您需要查看全局变量 errno 中存储的值是什么(这在 read 的手册页中有记载)。一种简单的方法是在打印失败消息时使用 perror() 而不是 printf() ——perror() 将附加一个人类可读的字符串来告诉你原因。

关于Linux 2.6.23 。接收错误。读取函数返回-1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10258593/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com