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;
python怎么读取串口数据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);
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。