*******关于 kylix下串口读写**********

piers 2003-09-21 09:23:24
linux下
可把串口当做文件/dev/ttyS0读写
但是在程序里设置波特率不知怎么做到

有没有类似mscomm的控件可用???
...全文
146 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
piers 2003-09-29
  • 打赏
  • 举报
回复
thanks
swites 2003-09-22
  • 打赏
  • 举报
回复
mscomm是第三方控件,这里时没用的,用下面语句:
/* this is a test program for using serial port */

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <time.h>
#include <string.h>


#define SER_DEV1 "/dev/ttyS0"
#define SER_DEV2 "/dev/ttyS1"
#define SER_DEV3 "/dev/ttyS2"
#define SER_DEV4 "/dev/ttyS3"

#define BR1 B19200
#define BR2 B9600
#define ESC 0x1b
#define CR 0x0d
#define LF 0x0a

static struct termios ser1_old, ser1_new;

/* open_port(device) -- open serial port */
int open_port(char *ser_port) {
int fd; /* port file descriptor */
fd = open(ser_port, O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1) {
printf ("open_port: Unable to open %s\n", SER_DEV1);
}
else fcntl(fd, F_SETFL, 0);
return (fd);
}

void init_port(int port_id) {
tcgetattr(port_id, &ser1_old); /* get the struct for old port settings */
bzero(ser1_new, sizeof(ser1_new)); /* clear struct for new port settings */
cfsetispeed(&ser1_new, BR1);
cfsetospeed(&ser1_new, BR1);
ser1_new.c_cflag |= (CLOCAL | CREAD); /* enable receiving, local state */
ser1_new.c_cflag &= ~PARENB;
ser1_new.c_cflag &= ~CSTOPB;
ser1_new.c_cflag &= ~CSIZE;
ser1_new.c_cflag |= CS8; /* set 8N1 */
ser1_new.c_cflag &= ~CRTSCTS; /* no flow control */
ser1_new.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); /* raw data input */
// ser1_new.c_oflag &= ~OPOST; /* raw data output */
// ser1_new.c_lflag = ICANON; /* input mode: non-canonical, no echo */
ser1_new.c_oflag = 0; /* raw output data */
ser1_new.c_iflag = IGNPAR | ICRNL; /* raw input data */
ser1_new.c_cc[VMIN]=1;
ser1_new.c_cc[VTIME]=0;
tcflush(port_id, TCIFLUSH); /* flush the buffer */
tcsetattr(port_id, TCSANOW, &ser1_new); /* set the new parameters */
}

void restore_port(int port_id) {
tcsetattr(port_id, TCSANOW, &ser1_old); /* restore the old port settings */
}

3,423

社区成员

发帖
与我相关
我的任务
社区描述
其他开发语言 其他开发语言
社区管理员
  • 其他开发语言社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧