serial port (2) 初始化com口

本文介绍了如何将COM口配置为中断模式和FIFO模式,详细阐述了设置过程,包括8259中断控制器、MCR、IER、FCR等寄存器的配置,以及中断使能、FIFO缓冲大小的设定。
摘要由CSDN通过智能技术生成

如何让com口工作在中断模式:
1, 在8259中把com口对应的中断打开
2, MCR BIT3(OUT2)设1(X86架构)
3, IER 把需要的中断置起来,

如何让com口工作在FIFO模式:
1。设置FCR BIT0让COM知道下面是设置FIFO模式的命令。
2。设置FCR BIT7-BIT6 来决定缓冲大小
            0    0    1byte
            0    1    4byte
            1    0    8byte
            1    1    14byte
3. 设置FCR BIT3来 启动FIFO模式

下面这段代码就是初始化COM口,并工作在FIFO模式。


Sample Code

; Set IRQ enable and out2 to set enable interrupts
                                       
        mov     cl, COM_IRQ             ;
        mov     dx, 21h
        cmp     cl,7                    ; judge IRQ > 7
        jbe     short wlsp_00  
        mov     dx, 0a1h
        sub     cl, 8

wlsp_00

要在 Linux C 语言中初始化 485 ,可以通过以下步骤实现: 1. 打开串设备文件,例如 /dev/ttyS1。 2. 通过 ioctl 系统调用设置串属性,包括波特率、数据位、停止位、奇偶校验等。对于 485 ,需要使用 RS485 模式,可以通过设置 RTS 和 CTS 的状态控制传输方向。 3. 可以使用 write 函数向串发送数据,使用 read 函数从串读取数据。 以下是一个简单的示例代码: ``` #include <stdio.h> #include <fcntl.h> #include <termios.h> #include <unistd.h> #include <sys/ioctl.h> int main() { int fd = open("/dev/ttyS1", O_RDWR); if (fd < 0) { printf("Failed to open serial port\n"); return -1; } struct termios options; tcgetattr(fd, &options); cfsetispeed(&options, B9600); cfsetospeed(&options, B9600); options.c_cflag |= CLOCAL | CREAD; options.c_cflag &= ~CSIZE; options.c_cflag |= CS8; options.c_cflag &= ~PARENB; options.c_cflag &= ~CSTOPB; options.c_cflag &= ~CRTSCTS; options.c_cflag |= CREAD | CLOCAL | IXON | IXOFF; options.c_iflag &= ~(IXON | IXOFF | IXANY); options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); options.c_oflag &= ~OPOST; options.c_cc[VMIN] = 1; options.c_cc[VTIME] = 0; tcsetattr(fd, TCSANOW, &options); int status; ioctl(fd, TIOCMGET, &status); status &= ~TIOCM_RTS; ioctl(fd, TIOCMSET, &status); char data[] = "Hello, world!"; write(fd, data, sizeof(data)); char buffer[256]; int n = read(fd, buffer, sizeof(buffer)); printf("Received %d bytes: %s\n", n, buffer); close(fd); return 0; } ``` 注意,上述代码只是一个简单的示例,实际使用时需要根据具体的硬件和应用场景进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值