c语言中串口读数据程序,如何通过串口来读写数据,请教达人

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

如何通过终端读取并显示串口连接的某硬件的数据

通过一程序来代替hexdump  从而进行输入输出

用C语言实现

下面一程序为i/o输入输出,请教达人,怎样才能输入输出串口的硬件数据

#include

#include

#include

#include

#include

#include

#include

#include

voidstripcrlf(char *temp);

int write_buffer(int fd,const void *buf,int count);

intread_buffer(int fd,void *buf,int count);

int rednlsting(int socket,char *buf,int maxlen);

int readdelimstring(int socket,char *buf,int maxlen,char delim);

void exiterror(char *message,int errnum);

const char *MESSAGE_filename="Select output filename:";

const char *MESSAGE_numbers="Please enter some numbers. Use -1 when you want to exit.\n";

int main(void)

{

int outfile;

int input[80];

int buffer[80];

write_buffer(1,MESSAGE_filename,strlen(MESSAGE_filename));

readnlstring(0,input,sizeof(input));

outfile=open(input,O_WRONLY | O_CREAT | O_TRUNC,0640);

if(outfile == -1)

{

exiterror("Error opening output file:",errno);

}

write_buffer(1,MESSAGE_numbers,strlen(MESSAGE_numbers));

do

{

readnlstring(0,input,sizeof(input));

if(write_buffer(outfile,input,strlen(input)) 

{

exiterror("Error writing:",errno);

}

if(write_buffer(outfile,"\n",1)<0)

{

exiterror("Error writing:",errno);

}

sprint(buffer,"New:%d\n", atoi(input)*5+(20*100)-12);

if(write_buffer(outfile,buffer,strlen(buffer))<0)

{

exiterror("Error writing:",errno);

}

}

while(atoi(input)!=-1);

close(outfile);

return 0;

}

void stripcrlf(char *temp)

{

while(strlen(temp)&&temp[0]&&((temp[strlen(temp)-1] == 13)||(temp[strlen(temp)-1] ==10)))

{

temp[strlen(temp)-1] = 0;

}

}

int write_buffer(int fd,const void *buf,int count)

{

const void *pts = buf;

int status = 0,n;

if (count<0)  return (-1);

while(status!= count)

{

n=write(fd,pts+status,count-status);

if(n 

status += n;

}

return(status);

}

int read_buffer(int fd,void *buf,int count)

{

void *pts=buf;

int status=0,n;

if(count<0)return(-1);

while(status!=count)

{

n = read(fd,pts+status,count-status);

if(n<1) return n;

status += n;

}

return(status);

}

int readnlstring(int socket,char *buf,int maxlen)

{

return readdelimstring(socket,buf,maxlen,'\n');

}

int readdelimstring(int socket,char *buf,int maxlen,char delim)

{

int status;

int count=0;

while(count

{

if((status=read_buffer(socket,buf+count,1))<1)

{

printf("Error reading./n");

return -1;

}

if(buf[count]==delim)

{

buf[count]=0;

return0;

}

count++;

}

buf[count]=0;

return0;

}

void exiterror(char *message,int errnum)

{

write_buffer(1,message,strlen(message));

write_buffer(1,sys_errlist[errnum],strlen(sys_errlist[errnum]));

write_buffer(1,"\n",1);

exit(255);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值