日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

c语言如何获取串口列表,如何通过串口来读写数据,请教达人

發布時間:2024/10/6 编程问答 50 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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);

}

總結

以上是生活随笔為你收集整理的c语言如何获取串口列表,如何通过串口来读写数据,请教达人的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。