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;
}