读书人

哪位高手来帮小弟我看看啊要崩溃了!

发布时间: 2013-07-04 11:45:33 作者: rapoo

谁来帮我看看啊!!!!!!!!!!要崩溃了!!!!!!!!!!!!!!!!
我现在有两台电脑A和B,打印机一台。在电脑(A)给打印机发送数据时,我需要电脑(B)读取(A)发送的数据,我有并口直通线(1-1,2-2.。。25-25),和一般的并口线(2-15,3-13。。。。。),程序如下:
#include <stdio.h>
#include <sys/time.h> // select()
#include <fcntl.h> // open()
#include <unistd.h> // read() write() close()
#include <signal.h> // signal()
#include <termios.h>
#include <term.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <stdlib.h> //exit()
#include <linux/ppdev.h>
#include <linux/parport.h>

int main(void)
{
int ret_w;
int mode; /* We'll need this later. */
unsigned char status, control, data;
unsigned char outbuf[1]={0x55};
unsigned char mask = (PARPORT_STATUS_ERROR | PARPORT_STATUS_BUSY);
unsigned char val = (PARPORT_STATUS_ERROR | PARPORT_STATUS_BUSY);
int irqc;
int busy ;//= nAck | nFault;
int acking; // = nFault;
int ready; // = Busy | nAck | nFault;
char ch;
int fd, pp_cntl;
struct timespec ts;

if( (fd = open ("/dev/parport0", O_RDWR))<0)
return 1;
if (fd == -1) {
perror ("open");
return 1;
}
printf ("fd = %d\t", fd);
while(1)
{
if (ioctl (fd, PPCLAIM)) {
perror ("PPCLAIM");
close (fd);
return 1;
}

mode = PARPORT_MODE_ECP;

if (ioctl (fd, PPSETMODE, &mode)) {
perror ("PPNEGOT");
close (fd);
return 1;
}
ioctl (fd, PPRSTATUS, &status);
printf("status = %d\t",status);
fflush(NULL);
sleep(3);

ready = 0xFa;
ioctl(fd, PPWCONTROL, &ready);
ts.tv_sec = 0;
ts.tv_nsec = 500;
nanosleep(&ts, NULL);
ioctl (fd, PPRSTATUS, &status);

ts.tv_nsec = 500;
ioctl(fd, PPRCONTROL, &pp_cntl);
pp_cntl |= 0X2;
ioctl(fd, PPWCONTROL, &pp_cntl);
nanosleep(&ts, NULL);
ioctl (fd, PPRSTATUS, &status);
printf("status = %d\t",status);

while(1)
{
ioctl(fd, PPRDATA, &data); //read
printf("data = %x\n",data);

ts.tv_nsec = 500;
ioctl(fd, PPRCONTROL, &pp_cntl);
pp_cntl &= 0Xfd;
ioctl(fd, PPWCONTROL, &pp_cntl);
nanosleep(&ts, NULL);
ioctl (fd, PPRSTATUS, &status);
printf("status = %d\t",status);
ioctl (fd, PPRELEASE);

}
close (fd);
}
运行后,能读到一次数据,但是数据不正确,而且连打印机打印的也错误了。
[解决办法]
线不能太长,阻抗要足够大才能保证并入的线不影响原来线路上的信号传输。
[解决办法]
用示波器或逻辑分析仪或portmon软件监视对比并线前和并线后的信号和数据。

搜“并口一拖二”?
[解决办法]
1. 你先不用连打印机,直接只用电脑B接收数据试试看是否正确。
2. 如果1不正确,则程序有问题。
3. 如果1正确,则可以考虑连接线(器)的电气特性有问题。
------解决方案--------------------


提醒:有时打印机开始打印乱码后,如果不拔掉打印机电源,再接通打印机电源,只靠软件方法似乎永远也不能让打印机不打印乱码了。
[解决办法]
if( (fd = open ("/dev/parport0", O_RDWR))<0)
恐怕要改为
if( (fd = open ("/dev/parport0", O_RDWR
[解决办法]
O_BINARY))<0)
吧!
[解决办法]
忘了Linux下没有O_BINARY了。

用O_DIRECT ?
Try to minimize cache effects of the I/O to and from this file. In general this will degrade performance, but it is useful in special situations, such as when applications do their own caching. File I/O is done directly to/from user space buffers. The I/O is synchronous, i.e., at the completion of the read(2) or write(2) system call, data is guaranteed to have been transferred. Under Linux 2.4 transfer sizes, and the alignment of user buffer and file offset must all be multiples of the logical block size of the file system. Under Linux 2.6 alignment to 512-byte boundaries suffices.
A semantically similar interface for block devices is described in raw(8).

读书人网 >C++

热点推荐