linux 串口之间传送字符串?

liushu01 2003-09-01 09:52:07
我的机器有两个串口,已经用线连接起来,我想向一个串口写字符串另一个串口能收到,可是不知怎么写,自己抄了一些代码,但接收口接到的都是乱码,而且字符数也不对,请教各位该怎么做。串口通信需要哪些设置?请多多指教!
...全文
145 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
liushu01 2003-09-02
  • 打赏
  • 举报
回复
木易:
我知道你是高手,你能告诉我发送和接受怎样对应吗?以及需要哪些对应,发送一个字符串要让另一个端口正确收到,两边的程序该怎样设置串口?
wwwunix 2003-09-02
  • 打赏
  • 举报
回复
要将串口设置成非标准模式。
另:可以在本版块里面找,前一段时间有这样的帖子。
haothinking 2003-09-02
  • 打赏
  • 举报
回复
网上查找,相关问题很多的。
wide288 2003-09-02
  • 打赏
  • 举报
回复
是运行在linux 下的吗?
liushu01 2003-09-02
  • 打赏
  • 举报
回复
谢谢木易
wwwunix 2003-09-02
  • 打赏
  • 举报
回复
这是我读串口的程序,调试通过的。

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/time.h>
#include <termios.h>
int main() {
int fd,size,stopb,check,i;
unsigned int sub;
char a;
struct termios term,save_term;
struct timeval t1,t2;
struct timezone tz1,tz2;
double per;
speed_t speed;
fd=open("/dev/ttyS0",O_RDWR);
if (fd<0) {
perror("Open tty error");
exit(-1);
}
tcgetattr(fd,&save_term);
term=save_term;
term.c_lflag &= ~(ECHO|ICANON|IEXTEN|ISIG);
term.c_iflag &= ~(BRKINT|ICRNL|INPCK|ISTRIP|IXON);
term.c_cflag &= ~(CSIZE|PARENB);
term.c_cflag |= CS8;
term.c_oflag &= ~(OPOST);
term.c_cc[VMIN]=1;
term.c_cc[VTIME]=0;
cfsetospeed(&term,B9600);
if (tcsetattr(fd,TCSANOW,&term)<0)
perror("setattr error");
else
printf("set OK\n");
gettimeofday(&t1,&tz1);
for (i=0;i<19200;i++) {
read(fd,&a,1);
}
gettimeofday(&t2,&tz2);
printf("Begin:%lds%ldus\n",t1.tv_sec,t1.tv_usec);
printf("Stop:%lds%ldus\n",t2.tv_sec,t2.tv_usec);
sub=(t2.tv_sec-t1.tv_sec)*1000000+(t2.tv_usec-t1.tv_usec);
printf("sub=%d\n",sub);
per=(double)sub/19200.00;
printf("per=%fus/byte",per);
}
rookieme 2003-09-01
  • 打赏
  • 举报
回复
找一个DOS下的源代码,不要用中断(太麻烦),用查询,直接对URT端口操作的就行
klbt 2003-09-01
  • 打赏
  • 举报
回复
学习。

23,125

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧