超时无法读取 /dev/parport

Cannot read from /dev/parport by timeout

请帮忙解答一个问题
我正在尝试解决 parport 的程序示例 环回 wtire/read.
由于 select 超时,在读取程序时跳过前 6 个符号 我不知道在那种情况下该怎么办。
如何追踪什么是 hold 函数以及如何修复它。
谢谢。

一些个人资料数据:

Kernel: Linux pc-Cantiga-ICH9M-Chipset 5.4.0-33-generic  
Ubuntu: bullseye/sid  
Compiler: gcc (Ubuntu 9.3.0-10ubuntu2) 9.3.0

写入端口

# write.c
#include "testlpt.h"
#include <sys/types.h>

/* example how to write data */
void  *write_data() {
unsigned char status='[=12=]';

  for(int i=0;i<SIZE_BUF;i++)  {


    int mode=0xFF;
int     res=ioctl(fd, PPDATADIR, &mode);

      fprintf(stdout,"Data= %c\n",data);
#if 1
#endif

counter=counter+0;
  res=ioctl(fd, PPWDATA, &data);

      if (res < 0)   {
       fprintf(stdout,"Error write (%d)(%s)\n",errno,strerror(errno));
       break;
    }
  data++;
  usleep(USLEEP);
   }
 int res=ioctl(fd, PPRELEASE);
 if (res < 0 ){
fprintf(stdout,"Release error \n"); 
 }
pthread_exit(0);
}

/* example how to read the status lines. */
int status_pins(int fd)
{
    unsigned char val;

    int res=ioctl(fd, PPRSTATUS, &val);
    if (res <0){
        fprintf(stdout,"PPRSTATUS ERROR res = %d\n",res);
        return -1;
    }
// val ^= PARPORT_STATUS_BUSY; /* /BUSY needs to get inverted */
fprintf(stdout,"ERROR  = 0x%02x \n", (val & (PARPORT_STATUS_ERROR | PARPORT_STATUS_BUSY))); //==PARPORT_STATUS_ERROR)?"HI":"LO",val ^= PARPORT_STATUS_BUSY);
fprintf(stdout,"BUSY = 0x%02x \n", (val & PARPORT_STATUS_BUSY)); //==PARPORT_STATUS_ERROR)?"HI":"LO",val ^= PARPORT_STATUS_BUSY);
fprintf(stdout,"SELECT  = 0x%02x \n", (val & PARPORT_STATUS_SELECT)); //==PARPORT_STATUS_ERROR)?"HI":"LO",val ^= PARPORT_STATUS_BUSY);
fprintf(stdout,"ACK  = 0x%02x \n", (val & PARPORT_STATUS_ACK)); //==PARPORT_STATUS_ERROR)?"HI":"LO",val ^= PARPORT_STATUS_BUSY);
}

页眉

# testlpt.h

#ifndef _TESTLPT_H
#define _TESTLPT_H

#include <stdio.h>
#include <fcntl.h>      
#include <sys/types.h>      
#include <sys/stat.h>       
#include <sys/ioctl.h>
#include <linux/parport.h>
#include <linux/ppdev.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <signal.h>
#include <sys/io.h>
#include <errno.h>
#include <error.h>
#include <string.h> 

#define DEVICE "/dev/parport0"
#define SIZE_BUF 26
#define USLEEP 1000
#define TIMEOUT 1

#include <poll.h>

#include <sys/epoll.h>

void *reads_data();
void *write_data();

int claim_port ();
int port_select(int fd);

extern int fd;
extern unsigned char data;
extern int counter;

static pthread_mutex_t lock;

#endif /* testlpt.h */

从端口读取

/* read.c */

#include <sys/select.h>
#include "testlpt.h"



void *reads_data() {

unsigned char data;
data='[=14=]';


int res=0;

fd_set rfds;

counter =0;

for(;;) {
struct timeval tv;  
tv.tv_sec = 0;
tv.tv_usec = 100;
errno =0;



FD_ZERO(&rfds);
FD_SET(fd, &rfds);

#if 1
    int mode=0x00;
    res=ioctl(fd, PPDATADIR, &mode);
  res=select (fd + 1, &rfds, (fd_set *)NULL, (fd_set *)NULL, &tv);
    fprintf(stdout,"res select = %d counter = %d \n",res,counter);
  if( res== -1 ) {
    fprintf(stdout,"End Read by select \n");
       continue;

    } else if (res == 0){
    fprintf(stdout,"Select timeout\n");
    } else {
    counter++;
    if ( FD_ISSET(fd , &rfds ) ) {
    res=ioctl(fd, PPRDATA, &data);  /* now fetch the data! */


    if (res < 0) {
    fprintf(stdout,"End Read by end of array\n");
    break;
        }
    }
   }
#endif  


    fflush(stdout);
    usleep(USLEEP);

 if (( data > 0x40 ) && ( data < 0x5B)) {
    fprintf(stdout,"counter = %d data = %c\n",counter,data);
    }
counter++;
}

FD_ZERO(&rfds);
    fprintf(stdout,"End Read\n");
pthread_exit(0);
}

主要功能

#include <testlpt.h>

int fd;
unsigned char data=0x41;
int counter=0;

int main(int argc, char **argv)
{
    pthread_t id[1]; 
    pthread_attr_t attr;        

    if ((fd=open(DEVICE, O_RDWR | O_NOCTTY )) < 0) {
        fprintf(stderr, "can not open %s\n", DEVICE);
        return 5;
    }
    int  mode = IEEE1284_MODE_COMPAT;
     int res=ioctl(fd, PPSETMODE, &mode);

     if( res== -1 ) {
    fprintf(stdout,"PPSETMODE ERROR\n");
  }
unsigned int modes;
     res=ioctl(fd, PPGETMODE, &modes);  
  if( res ==0 ) {
    fprintf(stdout,"MODES= 0x%02x\n", modes);
  }

#if 1
  while (claim_port() <0 ){
fprintf (stdout,"Claimt port error = %d\n",0);
sleep(1);
continue;
}
#endif

pthread_attr_init(&attr);
pthread_mutex_init(&lock, NULL);

#if 1
    if (pthread_create(&id[0],0,&write_data,NULL)){
perror("pthread create error!");
    }
    if (pthread_create(&id[1],0,&reads_data,NULL)){
perror("pthread create error!");
    }

if (pthread_join(id[0],NULL)) {
        perror("pthread exit error!");
        }
if (pthread_join(id[1],NULL)) {
        perror("pthread exit error!");
        }

    ioctl(fd, PPRELEASE);
    close(fd);
    exit(EXIT_SUCCESS);
#endif
}

int claim_port() {
  if (ioctl(fd, PPCLAIM) < 0) {
//  if (ioctl(fd, PPEXCL) < 0) {
  int errsv = errno;
  fprintf(stdout,"CLAIM errno %s \n",strerror(errsv));
        close(fd);
        return -1;
    }
        return 0;
}

程序输出

Data= A
Data= B
Select timeout
Data= C
Select timeout
Data= D
Select timeout
Data= E
Select timeout
Data= F
Select timeout
Data= G
Select timeout
Data= H
Data= I
Data= J
counter = 7 data = I
Data= K
counter = 9 data = J
Data= L
counter = 11 data = K
Data= M
counter = 13 data = L
Data= N
counter = 15 data = M
counter = 17 data = N
Data= O
counter = 19 data = N
Data= P
counter = 21 data = O
Data= Q
counter = 23 data = P
Data= R
Data= S
counter = 25 data = Q
Data= T
counter = 27 data = S
Data= U
counter = 29 data = T
Data= V
counter = 31 data = U
Data= W
counter = 33 data = V
Data= X
counter = 35 data = W
counter = 37 data = X
Data= Y
Data= Z
counter = 39 data = X
counter = 41 data = Z

这很神奇,但我添加了

int intrpt=0x09;  
ioctl(fd,PPWDATA,&intrpt);

在编写和程序开始正常工作之前