How to access driver in Windows like a file?
Goal: Communicating via Arcnet (network card in PC) with a generic driver that should be accessed like a driver.
Question: How to refer to the driver in the File class?
So far: I wrote a class that can read from a normal file. (see code below)
Now I have to change the referring the file to referring to the driver. But how?
If I use as Filename "\\.\Drivername", as the driver writer instructed (C-example) it will not compile in Java 6. If I use as Filename "\\\\Drivername" it will compile but it thows an filenotfound exception. What should be the syntax for this?
The code I wrote:
Code:
import java.io.*;
import java.util.*;
/** Demonstrate reading primitive type values from a binary file. **/
public class BinInputFileApp
{
public static void main (String arg[]) {
File file = null;
int i_data = 0;
int bytenummer = 0;
byte b_data = 0;
// Get the file from the argument line.
if (arg.length > 0) file = new File (arg[0]);
if (file == null) {
System.out.println ("Default: numerical.dat");
// file name should be: \\.\FARC or \\\\FARC
file = new File ("\\\\FARC");
}
try {
// Wrap the FileInputStream with a DataInputStream
FileInputStream file_input = new FileInputStream (file);
DataInputStream data_in = new DataInputStream (file_input );
while (true) {
try {
b_data = data_in.readByte();
bytenummer++;
}
catch (EOFException eof) {
System.out.printf ("End of Telegram, %3d bytes %n", bytenummer-9);
break;
}
// Node-ID of transmitter(SID) 1 byte node-id of the transmitter
// Node-ID of receiver(DID) 1 byte 0, if packet is broadcast message, otherwise node-id of the receiver
// Reserved 4 byte not yet in use, ignore
// Data 1-253 byte or 257-508 byte transferred raw data
// SENDBUF[0]:=INT_TO_BYTE(D0); (*Set DIN*)
// SENDBUF[1]:=INT_TO_BYTE(JOB AND 255); (*Set JOB_LOW*)
// SENDBUF[2]:=INT_TO_BYTE(JOB / 256); (*Set JOB_HIGH*)
i_data = b_data & 0xFF; // unsignedInt = signedByte & 0xFF;
b_data = (byte) i_data; // signedByte = (byte) unsignedInt;
if (bytenummer == 1) {
System.out.printf ("Node-ID of transmitter(SID) = %3d %n", i_data );}
else {
if (bytenummer == 2) {
System.out.printf ("Node-ID of receiver(DID) = %3d %n", i_data );}
else {
if (bytenummer > 2 & bytenummer < 7) {
System.out.printf ("reserved = %3d %n", i_data );}
else {
if (bytenummer == 7) {
System.out.printf ("DIN = %3d %n", i_data );
System.out.printf ("--------------------------%n");}
else {
if (bytenummer == 8) {
System.out.printf ("JOB (low) = %3d %n", i_data );}
else {
if (bytenummer == 9) {
System.out.printf ("JOB (high) = %3d %n", i_data );}
else {
System.out.printf ("data = %3d %n", i_data );
}
}
}
}
}
}
}
data_in.close ();
} catch (IOException e) {
System.out.println ( "IO Exception =: " + e );
}
} // main
} // class BinInputApp
Long story, maybe short answer.
Thanks in advance
Paul:)
what does the C example show?
Can you post how you would access the driver from a C program?
CJSL
C DeviceIoControl convert to Java
Hello Chris, I'm stuck again with translating the C example to Java. So here is the C program. CreateFile and ReadFile I can figure out probably but what about the DeviceIoControl? How should I handle that?
grt.
Paul
Code:
/* BEGIN HEADER ===============================================================
Project: Arcnet Driver for Windows NT
Programname: ARCREAD
Modulename:
Filename: arcread.c
Description: Simple Example for Controller Access and receive
Current Version: 1.2
Created: 21.11.2000
Author: S. Menger
Company: SoHard AG
Changes:
Version Date ch. by Ch-Idx Ch.-Description
------- -------- ------ ------ --------
1.0 21.11.00 MEN Creation
1.1 06.04.04 MEN Message changed (CTRL-C)
1.2 04.10.04 MEN Bugfix (szDriverName size)
(c) SoHard AG 1996-2000 All rights reserved
** END HEADER =============================================================*/
/* ------------------ COMPILER SWITCHES ------------------*/
/* ------------------ INCLUDES ------------------*/
#include "windows.h"
#include "stdio.h"
#include "conio.h"
#include "winioctl.h"
#include "stdlib.h"
#include "farc.h"
/* ------------------ DEFINITIONS ------------------*/
#define VERSION "V1.2"
#define ARCDATAMAX 514
/* ------------------ CONSTANTS ------------------*/
/* ------------------ TYPES ------------------*/
/* ------------------ MODULE-GLOBAL VARIABLES ---------------*/
/* ------------------ EXTERNAL VARIABLES ------------------*/
/* ------------------ EXTERNAL FUNCTIONENS ------------------*/
VOID showUsage(
VOID
);
int __cdecl main(
int argc,
char **argv
) {
HANDLE hDriver;
DWORD dwErrorCode;
ULONG dwInterruptCount;
DWORD dwBytesReceived;
DWORD dwReadTimeout;
ARCNET_DCB dcb;
CHAR szDriverName[256] = "\\\\.\\" ;
BOOL zLeaveLoop;
CHAR szVersion[26];
BYTE packet[514];
printf("ARCREAD " VERSION ", SOHARD ARCNET driver read-sample\n\n");
if ( argc < 3 ) {
showUsage();
return 1;
}
if ( (dcb.b_node_id = (unsigned char)atoi( argv[2] )) == 0) { // NodeID from 1st Argument
showUsage();
return 1;
}
strcat(szDriverName, argv[1] );
if ((hDriver = CreateFile(
szDriverName, // Name
GENERIC_READ | GENERIC_WRITE, // Access
0, // ShareMode
NULL, // Security_Attributes
OPEN_EXISTING, // Create
FILE_ATTRIBUTE_NORMAL, // File Attributes
NULL // Template
)) != INVALID_HANDLE_VALUE ) {
printf( "Driver opened!\n" );
// initialize Controller
// set DCB-Structure
dcb.z_et_1 = TRUE;
dcb.z_et_2 = TRUE;
dcb.z_et_3 = FALSE;
dcb.z_ckp_1 = FALSE;
dcb.z_ckp_2 = FALSE;
dcb.z_ckp_3 = FALSE;
dcb.z_backplane = FALSE;
dcb.z_p1mode = FALSE;
dcb.z_four_naks = FALSE;
dcb.b_init_behaviour = eARC_INIT_FORCE; //[1]
if (DeviceIoControl(
hDriver, // Handle
IOCTL_FARC_INIT, // Controlcode
&dcb, // Input-Buffer
sizeof(dcb), // Bufferlength
NULL,//buffer, // Output-Buffer
0, //sizeof(buffer), // Bufferlength
&dwBytesReceived, // Bytes read
NULL // Pointer to Overlapped Struktur, here not used
) == FALSE) {
dwErrorCode = GetLastError();
if (dwErrorCode == E_FARC_INIT_FORCED) {
printf( "No activity/token found. Controller-Init forced.\n"
"This maybe a quite normal situation, if you have no\n"
"other active node in the network.\n\n");
}
else {
printf( "Controller-Init fails. Errorcode %lu\n", dwErrorCode );
}
}
else {
dwErrorCode = 0;
printf( "Controller-Init success.\n" );
}
if ((dwErrorCode == E_FARC_INIT_FORCED) || (dwErrorCode == 0))
{
dwReadTimeout = 10000;
if (DeviceIoControl(
hDriver,
IOCTL_FARC_SET_READ_TIMEOUT,
&dwReadTimeout,
sizeof( ULONG ),
NULL,
0,
&dwBytesReceived,
NULL
) == TRUE ) {
printf("Try to start ReadFile with 10 seconds read timeout...\n\n");
}
else {
printf("Try to start ReadFile. Waiting infinitely for ARCNET data...\n\n");
}
// wait for datapacket
zLeaveLoop = FALSE;
do {
if ( ReadFile(
hDriver,
packet,
ARCDATAMAX,
&dwBytesReceived,
NULL
) == FALSE) {
dwErrorCode = GetLastError();
if (dwErrorCode == E_FARC_EXCESSIVE_RECON) {
printf("E_FARC_EXCESSIVE_RECON: Press 'r' to restart ReadFile, any other key to abort\n");
if (getch() != 'r') {
zLeaveLoop = TRUE;
printf("User break!\n\n");
}
}
else {
printf( "Read fails. Errorcode %lu\n", dwErrorCode );
zLeaveLoop = TRUE;
}
}
else {
printf( "Packet received.\nSID: %u\nDID: %u\nSize: %u\n", packet[0], packet[1], dwBytesReceived );
zLeaveLoop = TRUE;
}
} while (!zLeaveLoop);
if ( DeviceIoControl(
hDriver,
IOCTL_FARC_GET_INTERRUPTS,
NULL,
0,
&dwInterruptCount,
sizeof( ULONG ),
&dwBytesReceived,
NULL
) == TRUE ) {
printf( "Interrupt counter: %lu\n", dwInterruptCount );
printf( "Note, that RECON-Events causes also interrupts!\n\n" );
}
if ( DeviceIoControl(
hDriver,
IOCTL_FARC_GET_SWVERSION,
NULL,
0,
szVersion,
sizeof( szVersion ),
&dwBytesReceived,
NULL
) == TRUE ) {
printf( "Driver version: %s\n", szVersion );
}
else {
dwErrorCode = GetLastError();
printf( "Version read fails. Errorcode: %lu\n", dwErrorCode );
}
DeviceIoControl(
hDriver,
IOCTL_FARC_DEINIT,
NULL,
0,
NULL,
0,
&dwBytesReceived,
NULL
);
}
CloseHandle( hDriver );
}
else {
dwErrorCode = GetLastError();
printf("Can't get a handle to Driver %s. Errorcode: %lu\n",argv[1],dwErrorCode);
}
// if program not started in console window
printf("\n\nPress any key...\n");
do ; while (!kbhit());
getch();
return 0;
}
VOID showUsage(VOID)
{
printf("Usage: arcread DriverAccessName NodeID\n"
" e.g. arcread FARC 5\n"
"Note, that this program will not return,"
"until the controller received an arcnet packet!"
"\nPress any key...\n"
);
do ; while (!kbhit());
getch();
}