📄 testproc.c
字号:
/************************************************************/
/***** testproc.c, this is a test program for chardev.c *****/
/************************************************************/
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include "chardev.h"
void write_proc(void);
void read_proc(void);
main(int argc,char **argv) {
if(argc==1) {
puts("syntax: testprog[write|read]!");
exit(0);
}
if(!strcmp(argv[1],"write")) {
write_porc();
}
else if(!strcmp(argv[1],"read")) {
read_proc();
}
else {
puts("testprog: invalid command!");
}
return 0;
}
void write_proc() {
int fd,len,quit=0;
int dbg;
char buf[100];
fd=open("cdev0",O_WRONLY);
if(fd<=0) {
printf("Error opening device for writing!\n");
exit(1);
}
while(!quit) {
printf("\n write>>");
gets(buf);
if(!strcmp(buf,"exit"))
quit=1;
while(ioctl(fd,DYNCHAR_QUERY_NEW_MSG))
usleep(1000);
len=write(fd,buf,strlen(buf));
if(len<0) {
;
printf("Error writing to device!\n");
close(fd);
exit(1);
}
printf("%d bytes written to device!\n",len);
}
close(fd);
}
void read_proc() {
int fd,len,quit=0;
char *buf=NULL;
fd=open("cdev1",O_RDONLY);
if(fd<0) {
printf("Error opening device for reading!\n");
exit(1);
}
while(!quit) {
printf("\n read>>");
while(!ioctl(fd,DYNCHAR_QUERY_NEW_MSG))
usleep(1000);
// get the msg length
len=ioctl(fd,DYNCHAR_QUERY_MSG_LENGTH,NULL);
if(len) {
if(buf!=NULL)
free(buf);
buf=malloc(sizeof(char)*(len+1));
len=read(fd,buf,len);
if(len<0) {
printf("Error reading from device!");
}
else {
if(!strcmp(buf,"exit")) {
;
ioctl(fd,DYNCHAR_RESET); // reset
quit=1;
}
else
printf("%s\n",buf);
}
}
}
free(buf);
close(fd);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -