聚优致成 2015-12-15 12:17 采纳率: 50%
浏览 3450
已结题

rs485多路串口通信 。。

DM368开发板和一个单片机板通过RS485串口通信。
DM368开发板同时发送一组数据给单片机板和云台比如:aa 55 05 00 33 44 14 90 00
单片机板接收后返回另一组数据给DM368开发板比如:AA 55 16 00 11 22 33 44 00 00 10 00 32 47 42 47 D2 01

这个程序改怎么写?
PS: RS485为半双工 只能读或者只能写。

附上:我已写程序。哪里不对?

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#define GIO_RS485_CTRL 51
#define URAT0_RXD 19
#define URAT0_TXD 18
#define RS485_DEVICE_NAME "/dev/ttyS1"

typedef struct {
int pin_idx;
int pin_dir;
int pin_sta;
} davinci_gio_arg;

typedef enum {
AT91PIO_DIR_OUT = 0,
AT91PIO_DIR_INP
} davinci_gio_dir;

#define IOCTL_PIO_SETDIR _IOW('Q', 0x01, int) //Set pio direct
#define IOCTL_PIO_GETDIR _IOR('Q', 0x02, int) //Get pio direct
#define IOCTL_PIO_SETSTA _IOW('Q', 0x03, int) //Set pio status
#define IOCTL_PIO_GETSTA _IOR('Q', 0x04, int) //Get pio status
//IOCTL for FIQ
#define IOCTL_FIQ_SETIRQ _IOW('Q', 0x05, int) //Set fiq handle

#define LEN 16
#define DEV_PIO_LED "/dev/pio"
#define PIO_NUM 47

int set_com_config(int fd,int baud_rate,int data_bits,char parity,int stop_bits){
struct termios new_cfg,old_cfg;
int speed;

/*保存并测试现有的串口参数设置*/
if(tcgetattr(fd,&old_cfg)!=0){
    perror("tcgetattr"),exit (-1);
}

/*设置字符大小*/
new_cfg = old_cfg;
cfmakeraw(&new_cfg);
new_cfg.c_cflag &= ~CSIZE;

/**/
switch(baud_rate)
{
    case 2400:
    {
        speed = B2400;
    }
    break;
    case 4800:
    {
        speed = B4800;
    }
    break;
    case 9600:
    {
        speed = B9600;
    }
    break;
    case 19200:
    {
        speed = B19200;
    }
    break;
    case 38400:
    {
        speed = B38400;
    }
    break;
    case 115200:
    {
        speed = B115200;
    }
    break;
}
cfsetispeed(&new_cfg,speed);
cfsetospeed(&new_cfg,speed);

/*停止位*/
switch(data_bits)
{
    case 7:
    {
        new_cfg.c_cflag |=CS7;
    }
    break;

    default:
    case 8:
    {
        new_cfg.c_cflag |=CS8;
    }
    break;
}

/*奇偶校验*/
switch(parity)
{
    default:
    case 'n':
    case 'N':
    {
        new_cfg.c_cflag &=~PARENB;
        new_cfg.c_iflag &=~INPCK;
    }
    break;

    case 'o':
    case 'O':
    {
        new_cfg.c_cflag |= (PARODD | PARENB);
        new_cfg.c_iflag |= INPCK;
    }
    break;

    case 'e':
    case 'E':
    {
        new_cfg.c_cflag |= PARENB;
        new_cfg.c_cflag    &= ~PARODD;
        new_cfg.c_iflag |= INPCK;
    }
    break;

    case 's':
    case 'S':
    {
        new_cfg.c_cflag &= ~PARENB;
        new_cfg.c_cflag &= ~CSTOPB;
    }
    break;
}

/*设置停止位*/
switch(stop_bits)
{
    default:
    case 1:
    {
        new_cfg.c_cflag &= ~CSTOPB;
    }
    break;

    case 2:
    {
    new_cfg.c_cflag |= CSTOPB;
    }
}

new_cfg.c_cc[VTIME] = 0;
new_cfg.c_cc[VMIN] = 1;

/*处理未接受字符*/
tcflush(fd,TCIFLUSH);
/*激活新配置*/
if((tcsetattr(fd,TCSANOW,&new_cfg))!=0){
    perror("tcsetattr"), exit (-1);
}

return 0;

}

int open_port(void){
int fd;
fd =open("dev/ttyS1",O_RDWR|O_NOCTTY|O_NDELAY);
if(fd < 0)

{

perror("open serial port"), exit (-1);

}

if(fcntl(fd,F_SETFL,0)<0)
{
perror("fcntl F_SETFL\n");
exit (-1);
}

if(isatty(STDIN_FILENO)==0)
{
    perror("standrd input is not a terminal device");
    exit (-1);
}
return fd;

}

void read_485 (void)
{
char buff[100] = {0};

int fds,i;
int len = 0;
int fd_gpio = 0;
unsigned char val;

fd_gpio = open(DEV_PIO_LED, O_RDWR);
if(fd_gpio < 0)
{
perror("fd_gpio open err");
exit (-1);
}

//set dir of the pin
davinci_gio_arg arg;            
arg.pin_idx = PIO_NUM;        
arg.pin_dir = AT91PIO_DIR_OUT;

arg.pin_sta = 0;  
ioctl(fd_gpio, IOCTL_PIO_SETDIR, &arg); 

//set the value of the pin
ioctl(fd_gpio, IOCTL_PIO_SETSTA, &arg); 


if((fds = open_port())<0)//打开设备
{
    perror("open_port");
    exit (-1);
}

if(set_com_config(fds,2400,8,'N',1)<0)//配置串口
{
    perror("set_com_config");
    exit (-1);
}


printf ("rev ready!\n");
len = read (fds, buff, sizeof (buff));
if (len < 0)
{
perror ("read err");
exit (-1);
}
printf ("%s\n", buff);
close (fds);
printf ("test aaaaa\n");

}

void *write_rs485(void)
{
int fds,i;
int len = 0;
int fd_gpio = 0;
unsigned char val;

fd_gpio = open(DEV_PIO_LED, O_RDWR);
if(fd_gpio < 0)
{
perror("fd_gpio open err");
exit (-1);
}

//set dir of the pin
davinci_gio_arg arg;            
arg.pin_idx = PIO_NUM;        
arg.pin_dir = AT91PIO_DIR_OUT;

arg.pin_sta = 1;  
ioctl(fd_gpio, IOCTL_PIO_SETDIR, &arg); 

//set the value of the pin
ioctl(fd_gpio, IOCTL_PIO_SETSTA, &arg); 


if((fds = open_port())<0)//打开设备
{
    perror("open_port");
    exit (-1);
}

if(set_com_config(fds,2400,8,'N',1)<0)//配置串口
{
    perror("set_com_config");
    exit (-1);
}

// while(1)
// {
printf("send ready!\n");
char buff[] = "aa 55 05 00 33 44 14 90 00";
int len1 = write(fds,buff,sizeof (buff));
if (len1 < 0)
{
perror ("write err");
exit (-1);
}
printf ("send ok!\n");

// }
close (fds);
}

int main (void)
{
pthread_t tTransCtrlThread;
if(pthread_create(&tTransCtrlThread,NULL,write_rs485,NULL))
{
perror ("read_rs485 create fail");
exit (-1);
}
sleep (5);
read_485();
return 0;
}

  • 写回答

1条回答 默认 最新

  • qq_33389824 2015-12-15 12:41
    关注

    什么东西啊,可以教教我吗

    评论

报告相同问题?

悬赏问题

  • ¥15 数学的三元一次方程求解
  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题