初级会员
- 积分
- 130
- 金钱
- 130
- 注册时间
- 2017-11-1
- 在线时间
- 18 小时
|
1金钱
本帖最后由 greentea 于 2020-3-17 20:10 编辑
运行时数据不会输出到/dev/ttymxc2,直接在调试口usb-ttl 口输出。
- int rs485_test(void)
- {
- int fd = 0;
- char buf[100] = "123456789";
- fd = rs485_init();
- if(fd < 0){
- printf("open rs485 erro");
- return -1;
- }
- while(1){
- send(fd , buf , strlen(buf) );
- sleep(5);
- }
- }
复制代码
- static int rs485_init(void)
- {
- int erro = 0;
- int fd = 0;
- struct termios options;
- fd = open("/dev/ttymxc2", O_RDWR | O_NOCTTY | O_NDELAY);
- if(fd < 0){
- erro = -1;
- printf("open 485 erro\r\n");
- goto open_erro;
- }
- tcgetattr(fd, &options);
- /* set rate */
- cfsetispeed(&options,B115200);
- cfsetospeed(&options,B115200);
- /* set no odd and even*/
- options.c_cflag &= ~PARENB;
- options.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP);
- options.c_cflag |= CLOCAL | CREAD;
- options.c_oflag &= ~(OPOST);
- options.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
- options.c_iflag &= ~(IXON | IXOFF | IXANY);
- options.c_iflag |= IXON; /* 禁止流控 */
- options.c_cflag &= ~CSTOPB; /* 设置停止位 1 */
- options.c_cflag &= ~CSIZE; /*用位掩码清空数据位的设置*/
复制代码
- static int send (int fd, char *data, int data_len)
- {
- int len = 0;
- len = write(fd, data, data_len);
- if(len == data_len) {
- printf("data_len = %d\r\n", data_len);
- return len;
- } else {
- printf("send fail\r\n");
- tcflush(fd, TCOFLUSH); //TCOFLUSH刷新写入的数据但不传送
- return -1;
- }
- }
复制代码
|
|