该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
如何通过终端读取并显示串口连接的某硬件的数据
通过一程序来代替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);
}