问题描述
- rs485多路串口通信 。。 1C
- 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 47int set_com_config(int fdint baud_rateint data_bitschar parityint stop_bits){
struct termios new_cfgold_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_cfgspeed);cfsetospeed(&new_cfgspeed);/*停止位*/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(fdTCIFLUSH);/*激活新配置*/if((tcsetattr(fdTCSANOW&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(fdF_SETFL0)<0)
{
perror(""fcntl F_SETFL
"");
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 fdsi;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 pindavinci_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 pinioctl(fd_gpio IOCTL_PIO_SETSTA &arg); if((fds = open_port())<0)//打开设备{ perror(""open_port""); exit (-1);}if(set_com_config(fds24008'N'1)<0)//配置串口{ perror(""set_com_config""); exit (-1);}printf (""rev ready! "");len = read (fds buff sizeof (buff));if (len < 0){perror (""read err"");exit (-1);}printf (""%s "" buff);close (fds);printf (""test aaaaa "");
}
void *write_rs485(void)
{
int fdsi;
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 pindavinci_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 pinioctl(fd_gpio IOCTL_PIO_SETSTA &arg); if((fds = open_port())<0)//打开设备{ perror(""open_port""); exit (-1);}if(set_com_config(fds24008'N'1)<0)//配置串口{ perror(""set_com_config""); exit (-1);}
// while(1)
// {
printf(""send ready!
"");
char buff[] = ""aa 55 05 00 33 44 14 90 00"";
int len1 = write(fdsbuffsizeof (buff));
if (len1 < 0)
{
perror (""write err"");
exit (-1);
}
printf (""send ok!
"");// }
close (fds);
}int main (void)
{
pthread_t tTransCtrlThread;
if(pthread_create(&tTransCtrlThreadNULLwrite_rs485NULL))
{
perror (""read_rs485 create fail"");
exit (-1);
}
sleep (5);
read_485();
return 0;
}
解决方案
RS232 串口通信
RS232串口通信详解
RS232串口通信详解
解决方案二:
什么东西啊,可以教教我吗