たむらです
On Tue, 14 Feb 2006 20:34:44 +0900
Kaz <okui@xxxxxxxxxxxx> wrote:
Subject : [vine-users:074011] Re: プリンタポート
Message-Id : <20060214203444.59122f5a.okui@xxxxxxxxxxxx>
> スティッキービットを立てた状態だとLEDは点灯するのでプログラムは間違っていないのですが
> まだ何か足らないんでしょうか?
エラー発生時にエラー内容を確認してますでしょうか ?
ためしに PG をperror()出力するように改造してみましたので参考にしてみてください。
==========================================================
#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdarg.h>
#include <errno.h>
/*** パラレルポートのポートアドレス ***/
#define DPORT 0x0378
#define RPORT 0x0379
#define CPORT 0x037a
#define LOOP_MAX 5 /* LED ON OFF させるループ上限 */
/*------------------------------------------------------------------*/
int body(int fd);
void error_snap(char *file, int line, char *fmt, ...);
void my_perror(char* file, int line, const char* s);
int seek_and_write( const int fd, unsigned int port, unsigned char data);
/*------------------------------------------------------------------*/
#define ERROR(fmt, ...) error_snap(__FILE__, __LINE__, fmt, __VA_ARGS__)
#define MYPERROR(s) my_perror( __FILE__, __LINE__, s )
#define PRINT_LOG(s) printf("LOG %s:%-4d - %s \n", __FILE__, __LINE__, s);
/*------------------------------------------------------------------*/
int main(void)
{
int pfd = -1;
int ret = 0;
pfd = open( "/dev/port", O_RDWR );
if( pfd == -1 ) {
MYPERROR("open error ");
return -1;
}
PRINT_LOG("body() in");
ret = body(pfd); /* 処理の本体 */
PRINT_LOG("body() end");
close(pfd);
return 0;
}
/**/
/*** 実際の処理の本体 ***/
int body(int fd)
{
int cnt = 0;
int ret = 0;
ret = seek_and_write( fd, CPORT , 0x00);
if( ret != 0 )
return ret;
for(cnt = 0; cnt < LOOP_MAX; cnt++){
PRINT_LOG("loop ");
ret = seek_and_write( fd, DPORT , 0xFF );
if( ret != 0 )
return ret;
sleep(1);
ret = seek_and_write( fd, DPORT , 0x00 );
if( ret != 0 )
return ret;
sleep(1);
}
return 0;
}
/**/
/*** ERR ファイル : 行番号 vsprintf風メッセージと perror ***/
void error_snap( char* file, int line, char*fmt, ...)
{
char msg[1024];
va_list ap;
va_start(ap, fmt);
fprintf(stderr, "ERR %s:%-4d ", file, line);
vsprintf(msg, fmt, ap);
va_end(ap);
perror(msg);
return;
}
/**/
/*** ERR ファイル : 行番号 + perror ***/
void my_perror(char* file, int line, const char* s)
{
fprintf(stderr, "ERR %s:%-4d ", file, line);
perror(s);
return;
}
/**/
/*** 指定のポートにシークして 1Byte 書き込む(エラーログ付き) ***/
int seek_and_write( const int fd, unsigned int port, unsigned char data)
{
if( lseek(fd, port, SEEK_SET) == -1 ) {
ERROR("lseek error [ port=%08XH ]", port);
return -1;
}
if( write(fd, &data, 1) == -1 ) {
ERROR("write error [ port=%08XH ]", port);
return -1;
}
return 0;
}
==========================================================
-- M.Tamura : camel@xxxxxxx
GnuPG : 9B3C 0C3D C32C FF26 C2B6 2191 FB9B 7F0C A359 691A