在内核中不能读写串口设备文件,哪位有什么解决方案,最好有例子原代码,多谢
送高分
---------------------------------------------------------------
在kernel中可以直接用outb()和inb()对串口进行读写。
---------------------------------------------------------------
这个你要参考串口的规范,参考资料:《PC技术内幕》第12章。
---------------------------------------------------------------
outb(0x80,0x3fb);
outb(0x0c,0x03f8);
outb(0x00,0x03f9);
设置波特率为9600
---------------------------------------------------------------
//我也在找相关资料,有什么好东东,大家拿出来共享,祝你好运。
#include
1<sys types.h="">
2#include <sys stat.h="">
3#include <fcntl.h>
4#include <termios.h>
5#include <stdio.h>
6
7/* baudrate settings are defined in <asm termbits.h="">, which is
8included by <termios.h> */
9#define BAUDRATE B38400
10/* change this definition for the correct port */
11#define MODEMDEVICE "/dev/ttyS1"
12#define _POSIX_SOURCE 1 /* POSIX compliant source */
13
14#define FALSE 0
15#define TRUE 1
16
17volatile int STOP=FALSE;
18
19main()
20{
21int fd,c, res;
22struct termios oldtio,newtio;
23char buf[255];
24/*
25Open modem device for reading and writing and not as controlling tty
26because we don't want to get killed if linenoise sends CTRL-C.
27*/
28fd = open(MODEMDEVICE, O_RDWR ¦ O_NOCTTY );
29if (fd <0) {perror(MODEMDEVICE); exit(-1); }
30
31tcgetattr(fd,&oldtio); /* save current serial port settings */
32bzero(&newtio, sizeof(newtio)); /* clear struct for new port settings */
33
34/*
35BAUDRATE: Set bps rate. You could also use cfsetispeed and cfsetospeed.
36CRTSCTS : output hardware flow control (only used if the cable has
37all necessary lines. See sect. 7 of Serial-HOWTO)
38CS8 : 8n1 (8bit,no parity,1 stopbit)
39CLOCAL : local connection, no modem contol
40CREAD : enable receiving characters
41*/
42newtio.c_cflag = BAUDRATE ¦ CRTSCTS ¦ CS8 ¦ CLOCAL ¦ CREAD;
43
44/*
45IGNPAR : ignore bytes with parity errors
46ICRNL : map CR to NL (otherwise a CR input on the other computer
47will not terminate input)
48otherwise make device raw (no other input processing)
49*/
50newtio.c_iflag = IGNPAR ¦ ICRNL;
51
52/*
53Raw output.
54*/
55newtio.c_oflag = 0;
56
57/*
58ICANON : enable canonical input
59disable all echo functionality, and don't send signals to calling program
60*/
61newtio.c_lflag = ICANON;
62
63/*
64initialize all control characters
65default values can be found in /usr/include/termios.h, and are given
66in the comments, but we don't need them here
67*/
68newtio.c_cc[VINTR] = 0; /* Ctrl-c */
69newtio.c_cc[VQUIT] = 0; /* Ctrl-\ */
70newtio.c_cc[VERASE] = 0; /* del */
71newtio.c_cc[VKILL] = 0; /* @ */
72newtio.c_cc[VEOF] = 4; /* Ctrl-d */
73newtio.c_cc[VTIME] = 0; /* inter-character timer unused */
74newtio.c_cc[VMIN] = 1; /* blocking read until 1 character arrives */
75newtio.c_cc[VSWTC] = 0; /* '\0' */
76newtio.c_cc[VSTART] = 0; /* Ctrl-q */
77newtio.c_cc[VSTOP] = 0; /* Ctrl-s */
78newtio.c_cc[VSUSP] = 0; /* Ctrl-z */
79newtio.c_cc[VEOL] = 0; /* '\0' */
80newtio.c_cc[VREPRINT] = 0; /* Ctrl-r */
81newtio.c_cc[VDISCARD] = 0; /* Ctrl-u */
82newtio.c_cc[VWERASE] = 0; /* Ctrl-w */
83newtio.c_cc[VLNEXT] = 0; /* Ctrl-v */
84newtio.c_cc[VEOL2] = 0; /* '\0' */
85
86/*
87now clean the modem line and activate the settings for the port
88*/
89tcflush(fd, TCIFLUSH);
90tcsetattr(fd,TCSANOW,&newtio);
91
92/*
93terminal settings done, now handle input
94In this example, inputting a 'z' at the beginning of a line will
95exit the program.
96*/
97while (STOP==FALSE) { /* loop until we have a terminating condition */
98/* read blocks program execution until a line terminating character is
99input, even if more than 255 chars are input. If the number
100of characters read is smaller than the number of chars available,
101subsequent reads will return the remaining chars. res will be set
102to the actual number of characters actually read */
103res = read(fd,buf,255);
104buf[res]=0; /* set end of string, so we can printf */
105printf(":%s:%d\n", buf, res);
106if (buf[0]=='z') STOP=TRUE;
107}
108/* restore the old port settings */
109tcsetattr(fd,TCSANOW,&oldtio);
110}</termios.h></asm></stdio.h></termios.h></fcntl.h></sys></sys>