手指恋上烟 发表于 2012-9-19 12:43:11

如何拉高或降低串口0RTS和CTS

#include <sys/ioctl.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <string.h>
#include <sys/types.h>
#include "math.h"
int main(int argc,char* argv)
{
struct termios newTermios,oldTermios;
char* serial_port="/dev/tq2440_serial0";
int fd = open(serial_port,O_RDWR,0);
unsigned char str;
memset(str,0,6);
char result;
int i = 0;
if(fd <0 )
{
printf("cant't open tq2440_serial0\n");
return -1;
}
tcgetattr(fd,&oldTermios);
tcflush(fd,TCIOFLUSH);
memcpy(&newTermios,&oldTermios,sizeof(struct termios));
cfsetispeed(&newTermios,B9600);
cfsetospeed(&newTermios,B9600);
if(fcntl(fd,F_SETFL,O_NONBLOCK) < 0)
{
printf(" fcntl failed");
}
// memcpy(&newTermios,&oldTermios,sizeof(struct termios));
newTermios.c_cflag |= (CLOCAL|CREAD);
      newTermios.c_cflag &=~CSIZE;
newTermios.c_cflag |= CS8;
      newTermios.c_cflag &= ~PARENB;
      newTermios.c_iflag &= ~INPCK;
newTermios.c_cflag &= ~CSTOPB;
      newTermios.c_cc = 0;
      newTermios.c_cc = 0;
newTermios.c_iflag |= IGNPAR|ICRNL;
      newTermios.c_oflag |= OPOST;
      newTermios.c_iflag &= ~(IXON|IXOFF|IXANY);
      newTermios.c_lflag &= ~(ICANON | ECHO | ECHOE |ISIG);
      tcflush(fd, TCIFLUSH);
tcsetattr(fd, TCSANOW, &newTermios);
unsigned int controll;
unsigned int RTS;
RTS = TIOCM_RTS;
unsigned int CTS;
CTS = TIOCM_CTS;
while(1){
   if(ioctl(fd,TIOCMBIS,&RTS) == 0 )
{
   printf("RTS set success!\n");
}
if(ioctl(fd,TIOCMBIS,&CTS)== 0)
{
   printf("CTS set success!\n");
}
ioctl(fd,TIOCMGET,&controll);
printf("%x\n",controll);
}
return 0;
}
用IOCTL函数也能正确返回,但是RTS和CTS引脚测量根本没有变化?这是什么情况

embedsky_lhh 发表于 2012-9-19 13:31:08

你是怎么测的?
TIOCM_RTS 有效后是把串口的 RTS 设置为有信号,串口的电平为低时是有信号,为高时为无信号, 控制串口的相应引脚是否有信号,并不反应当前串口的真实电平高低,
你可以用ioctl先去查看下他的状态

手指恋上烟 发表于 2012-9-19 15:45:31

embedsky_lhh 发表于 2012-9-19 13:31 static/image/common/back.gif
你是怎么测的?
TIOCM_RTS 有效后是把串口的 RTS 设置为有信号,串口的电平为低时是有信号,为高时为无信号 ...

比如设RTS为高高就是讲RTS置为无信号,用ioctl(,TIOCMBIC,)还是用ioctl(,TIOCMSET,)?我现在2种方法都试过,RTS电平没有发生变化

embedsky_lhh 发表于 2012-9-19 18:02:29

这种程序的开发,你可以先到PC上测试

手指恋上烟 发表于 2012-9-19 19:04:29

embedsky_lhh 发表于 2012-9-19 18:02 static/image/common/back.gif
这种程序的开发,你可以先到PC上测试

我用的笔记本没有串口

手指恋上烟 发表于 2012-9-19 19:53:40

embedsky_lhh 发表于 2012-9-19 18:02 static/image/common/back.gif
这种程序的开发,你可以先到PC上测试

我现在是通过ioctl函数循环将RTS设为有信号,状态为检测也是有信号,但是用万能表接串口的GND和8号引脚,一直是5.5V左右,正常的话应该是0V吧

embedsky_lhh 发表于 2012-9-20 09:01:43

用万用表测不出来吧

手指恋上烟 发表于 2012-9-20 09:12:47

embedsky_lhh 发表于 2012-9-20 09:01 static/image/common/back.gif
用万用表测不出来吧

一边接GND,一边接8号脚,可以测出是5.5V左右但是无法控制其降低

embedsky_lhh 发表于 2012-9-20 09:15:08

你控制PC的时候,你用万用表能测出来吗?

手指恋上烟 发表于 2012-9-20 10:12:47

embedsky_lhh 发表于 2012-9-20 09:15 static/image/common/back.gif
你控制PC的时候,你用万用表能测出来吗?

我是直接在ARM班上运行串口程序,然后用万能表测串口0的GND和8号脚

手指恋上烟 发表于 2012-9-20 10:18:26

embedsky_lhh 发表于 2012-9-20 09:15 static/image/common/back.gif
你控制PC的时候,你用万用表能测出来吗?

2440的串口支持硬件流控吗?

TQ-ZQL 发表于 2012-9-20 10:56:59

2440的串口支持的

手指恋上烟 发表于 2012-9-20 11:21:40

TQ-ZQL 发表于 2012-9-20 10:56 static/image/common/back.gif
2440的串口支持的

打开硬件流控和关闭硬件流控,RTS电平始终没有变化

TQ-ZQL 发表于 2012-9-20 11:47:29

本帖最后由 TQ-ZQL 于 2012-9-20 11:48 编辑

你看下源码,这个函数是设置cts的
linux/drivers/tty/serial/samsung.c
static void s3c24xx_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
{
         /* todo - possibly remove AFC and do manual CTS */
}
他都不支持这种方法。
你先看下驱动再写应用吧

手指恋上烟 发表于 2012-9-21 10:22:51

TQ-ZQL 发表于 2012-9-20 11:47 static/image/common/back.gif
你看下源码,这个函数是设置cts的
linux/drivers/tty/serial/samsung.c
static void s3c24xx_serial_se ...

这个源码里面没找到设置RTS的,是不是说明2440的板子串口支持硬件流控,但是linux2.6.30内核不支持硬件流控
页: [1]
查看完整版本: 如何拉高或降低串口0RTS和CTS