我使用 pc1602f PowerTip 直接连接到 PC 并行端口,使用此方案: http://www.beyondlogic.org/parlcd/parlcd.htm
一切都很好,为 LCD 供电,并用黑色 block 显示前排,直到那时还好,但现在我想通过并行端口发送信息。
如果您查看该页面,您会看到有一个源可以将信息发送到 lcd,但使用的是 Windows 库:呵呵:
我离开我的代码试图成为 linux。
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/io.h>
#define PORTADDRESS 0x3f8
#define DATA PORTADDRESS+0
#define STATUS PORTADDRESS+1
#define CONTROL PORTADDRESS+2
main(int argc, char **argv)
{char string[] = {"Testing 1,2,3"};
int count;
int len;
char init[10];
init[0] = 0x0F; /* Init Display */
init[1] = 0x01; /* Clear Display */
init[2] = 0x38; /* Dual Line / 8 Bits */
if (ioperm(PORTADDRESS,1,1))
fprintf(stderr, "No se puede acceder al: %x\n", PORTADDRESS), exit(1);
outb(CONTROL, inb(CONTROL) & 0xDF);
outb(CONTROL, inb(CONTROL) & 0x08);
for (count = 0; count <= 2; count++)
{
outb(DATA, init[count]);
outb(CONTROL,inb(CONTROL) | 0x01);
sleep(20);
outb(CONTROL,inb(CONTROL) & 0xFE);
sleep(20);
}
outb(CONTROL, inb(CONTROL) & 0xF7);
len = strlen(string);
for (count = 0; count < len; count++)
{
outb(DATA, string[count]);
outb(CONTROL,inb(CONTROL) | 0x01);
sleep(2);
outb(CONTROL,inb(CONTROL) & 0xFE);
sleep(2);
}
}
编译完美,但当我想以 root 身份尝试并运行它时,它会抛出我
root@ubuntu: /
media/E80C-30D5/LCD/build #./lcd
Segmentation fault (`core 'generated)
root@ubuntu: /media/E80C-30D5/LCD/build #
查看 dmesg 我发现了这一点。
[3176.691837] lcd [3867] general protection ip: 400cb4 sp: 7fff887ad290 error: 0 in lcd [+2000 400 000]root@ubuntu: / media/E80C-30D5/LCD/build #
我放了ttyS*的dmesg日志
root @ ubuntu: / media/E80C-30D5/LCD/build # dmesg | grep ttyS
[2.335717] serial8250: ttyS0 at I / O 0x3f8 (irq = 4) is a 16550A
[2.335817] serial8250: ttyS1 at I / O 0x2f8 (irq = 3) is a 16550A
[2.336100] 00:0 b: ttyS1 at I / O 0x2f8 (irq = 3) is a 16550A
[2.336207] 00:0 c: ttyS0 at I / O 0x3f8 (irq = 4) is a 16550A
root @ ubuntu: / media/E80C-30D5/LCD/build #
不要让它运行,你能帮帮我吗?
最佳答案
您尝试运行的代码不会在“现代”操作系统中运行。 Linux 和 16 位之后的 Windows 版本将不允许您执行直接访问端口或内存的操作。对内存的访问通过内存管理器进行,因此您指定的地址并不是您最终访问的地址;并且端口只能由作为操作系统内核一部分的设备驱动程序访问。
我不是很确定,但如果你的主要目的只是通过串行端口输出一些字节,那么你的大部分代码都没有必要,因为你可以简单地写入一个文件,将设备驱动程序包装到港口。关于这个 in this article 有更多细节.
如果你真的需要对串口芯片进行位操作,你可以在这个serial port mini-howto的底部找到更多信息。 .请注意,编写您自己的设备驱动程序被认为是高级的。
编辑:我刚刚注意到您的问题提到了并行端口,但您代码中的端口号 (3F8) 是其中一个串行端口的端口号。提示:如果您连接的插头是 9 针端口,那么它肯定是串行端口。如果它是一个 25 针端口和母端口(孔,而不是引脚),那么它可能是一个并行端口,如果是公端口,它可能是一个串行端口。或者您可以测量电压:串行端口信号上升到 +/- 9V 左右,而并行端口信号从 0 到 5V。
关于c - 将 rs232 编程为 lcd (Linux),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3246279/