linux下串口编程(读写)

xin360 2007-06-03 04:11:43
以下是我在linux终端下写的C代码,目的是想让其能接受到串口的数据,可是不知道为什么总是不能接受到,也没有报错信息,请大侠们看看,帮帮忙指点以下,谢谢!

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <stdio.h>


#define BAUDRATE B38400
#define MODEMDEVICE "/dev/ttyS0"
//#define _POSIX_SOURCE 1 /* POSIX compliant source */

#define FALSE 0
#define TRUE 1

volatile int STOP=FALSE;

main()
{
int fd,c, res;
struct termios oldtio,newtio;
char buf[255];

fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY );
if (fd <0) {perror(MODEMDEVICE); exit(-1); }

tcgetattr(fd,&oldtio);
bzero(&newtio, sizeof(newtio));


newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;

newtio.c_iflag = IGNPAR | ICRNL;


newtio.c_oflag = 0;

newtio.c_lflag = ICANON;

newtio.c_cc[VINTR] = 0; /* Ctrl-c */
newtio.c_cc[VQUIT] = 0; /* Ctrl-\ */
newtio.c_cc[VERASE] = 0; /* del */
newtio.c_cc[VKILL] = 0; /* @ */
newtio.c_cc[VEOF] = 4; /* Ctrl-d */
newtio.c_cc[VTIME] = 0; /* inter-character timer unused */
newtio.c_cc[VMIN] = 1; /* blocking read until 1 character arrives */
newtio.c_cc[VSWTC] = 0; /* '\0' */
newtio.c_cc[VSTART] = 0; /* Ctrl-q */
newtio.c_cc[VSTOP] = 0; /* Ctrl-s */
newtio.c_cc[VSUSP] = 0; /* Ctrl-z */
newtio.c_cc[VEOL] = 0; /* '\0' */
newtio.c_cc[VREPRINT] = 0; /* Ctrl-r */
newtio.c_cc[VDISCARD] = 0; /* Ctrl-u */
newtio.c_cc[VWERASE] = 0; /* Ctrl-w */
newtio.c_cc[VLNEXT] = 0; /* Ctrl-v */
newtio.c_cc[VEOL2] = 0; /* '\0' */


tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&newtio);


while (STOP==FALSE) {
res = read(fd,buf,255);
buf[res]=0;
printf(":%s:%d\n", buf, res);
if (buf[0]=='z') STOP=TRUE;
}

tcsetattr(fd,TCSANOW,&oldtio);
}
...全文
1523 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
haoge101 2012-10-12
  • 打赏
  • 举报
回复
地地道道的的
suixin307 2011-10-10
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 kaqiusa 的回复:]

你是不是在虚拟机下调试的??
[/Quote]

在虚拟机下有什么不一样嘛??
aoaofeng 2007-06-12
  • 打赏
  • 举报
回复
看下这个!!
#include<sys/types.h>
#include<sys/socket.h>
#include<stdio.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
int main()
{ int server_sockfd,client_sockfd;
int server_len,client_len;
struct sockaddr_in server_address;
struct sockaddr_in client_address;
server_sockfd=socket(AF_INET,SOCK_STREAM,0);
server_address.sin_family=AF_INET;
server_address.sin_addr.s_addr=htonl(INADDR_ANY);
server_address.sin_port=htons(17725);
server_len=sizeof(server_address);
bind(server_sockfd,(struct sockaddr*)&server_address,server_len);
listen(server_sockfd,5);
while(1){
char ch;
printf("server waiting\n");
client_sockfd=accept(server_sockfd,
(struct sockaddr*)&client_address,&client_len);
read(client_sockfd,&ch,1);
ch++;
write(client_sockfd,&ch,1);
close(client_sockfd);
}
}
xin360 2007-06-07
  • 打赏
  • 举报
回复
为什么这么说?你也有遇过这样的问题?
xin360 2007-06-06
  • 打赏
  • 举报
回复
我现在用第三方类来打开串口已经可以发送了,可是接收的却是乱码(用类里的readBlock来读),不过用 while((nread = read(fd, buff, 512))>0)
{
printf("\nLen %d\n",nread);
buff[nread - 1] = '\0';
printf( "%s\n", buff);
} 这个代码收到的是正确的信息,这是怎么回事呢?
有大侠知道吗?指点下,谢谢!
kf701 2007-06-06
  • 打赏
  • 举报
回复
第三方类有问题
xin360 2007-06-05
  • 打赏
  • 举报
回复
已经都其他两台机子上试过了,也是这样,在xp下,用串口工具试过是可以接收的,那串口硬件应该没问题了吧,是不是linux下对串口还有其他操作才能激活的?
dai_weitao 2007-06-05
  • 打赏
  • 举报
回复
可能是你机器串口的问题吧
xin360 2007-06-05
  • 打赏
  • 举报
回复
不是在虚拟机下调试的,大侠有何想法吗?

恩,我再想是不是串口没打开过,因为现在调用第三方类qextserialport时发现不能去读open函数。

串口打开之前另外激活或是怎么设置的吗?直接上面那么编程可以吗?

还有我曾经把一样的程序在ARM机上运行是能收到结果的(自己接收),但是在PC下就无法收到了,是这样的,还是我的机子有问题?

谢谢大家的帮助。
kaqiusa 2007-06-05
  • 打赏
  • 举报
回复
串口发送和接收的时候许多设置应该一样,你看发送的串口和接收串口的设置是否相同,还有你可以把你的程序加上数据发送,然后将串口的2和3信号线短接,就可以用一个串口进行收发。
xin360 2007-06-05
  • 打赏
  • 举报
回复
恩,这个我也有,也测试过,也不行,不过这个跟我原本的程序应该是一样的吧,原来的有什么问题吗?
kf701 2007-06-05
  • 打赏
  • 举报
回复
这个程序是你要的。
1 /***************************************************************************
2 * tty_test.c
3 *
4 * Thu May 24 11:22:27 2007
5 * Copyright 2007 User
6 * Email <kf701.ye AT gmail.com>
7 ****************************************************************************/
8
9
10 #include <errno.h>
11 #include <sys/types.h>
12 #include <fcntl.h>
13
14 #include "kf701.h"
15
16 int main(int argc, char **argv)
17 {
18 int nread;
19 char buff[512];
20
21 int fd = open( "/dev/ttyS0", O_RDWR ); //| O_NOCTTY | O_NDELAY
22 if (-1 == fd)
23 {
24 perror("Can't Open Serial Port");
25 return -1;
26 }
27
28 set_speed(fd,115200);
29 if (set_parity(fd,8,1,'N') == false)
30 {
31 printf("Set Parity Error\n");
32 exit (0);
33 }
34 printf("Set Parity ok\n");
35
36 while (1)
37 {
38 while((nread = read(fd, buff, 512))>0)
39 {
40 printf("\nLen %d\n",nread);
41 buff[nread - 1] = '\0';
42 printf( "%s\n", buff);
43 }
44 }
45 close(fd);
46 exit (0);
47 }
pinkfrog 2007-06-04
  • 打赏
  • 举报
回复
打开,设置是否成功都输出点信息来方便调试
kaqiusa 2007-06-04
  • 打赏
  • 举报
回复
你是不是在虚拟机下调试的??

23,124

社区成员

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

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