Results 1 to 7 of 7
  1. #1
    pthoonen is offline Member
    Join Date
    Dec 2008
    Posts
    4
    Rep Power
    0

    Default 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:

    Java 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:)

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default what does the C example show?

    Can you post how you would access the driver from a C program?

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  3. #3
    pthoonen is offline Member
    Join Date
    Dec 2008
    Posts
    4
    Rep Power
    0

    Default

    Hi Chris, thanks for the quick response. I wrote an email to the writer of the driver and he noted me that he mentioned the driver name in the wrong way. It should be "file = new File ("\\\\.\\FARC");"
    Next week I'm able to test that. Till then I will let It rest.
    Thanks again.

    Paul

  4. #4
    pthoonen is offline Member
    Join Date
    Dec 2008
    Posts
    4
    Rep Power
    0

    Default 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

    Java 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();
    }

  5. #5
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    7

    Lightbulb jni

    write it as C code, things like
    Java Code:
    CHAR szDriverName[256] = "\\\\.\\";
    will drive you nuts trying to do something like that in Java.

    Gets really Strange.java ( see Sedgewick ) do a driver in C and call the native code from Java. For a taste of what is going on, try writing a Unicode escape for a line ending in a literal string. If you get it working by 2010 then I will be impressed.

    Mount points in winnie are not robust, NP-Hard.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  6. #6
    pthoonen is offline Member
    Join Date
    Dec 2008
    Posts
    4
    Rep Power
    0

    Default

    Thanks Nicholas,

    I started to do some coding in Java for the first time. Read some books, saw some tutorials and 'they' all say 'avoid using native code'. Now the first real project that I'm writing I have to use native code otherwise it's not going to work. How limited is the I/O access in Java?
    If I want to write a class that does exactly the same as Windows Hyper terminal for instance, do I also need native code for accessing the COM port. If so, then I'm really disappointed in Java. If not, where is the limit? When do I need native code?
    I'm sure I have to look into how I should use native code. Any good books on that subject? I start with Google. :)

    After investigating the native code solution I think the following procedures or classes should be called as native code:
    CreateFile, DeviceIoControl, ReadFile and CloseHandle. So these peaces of code should be in a .dll file. What C(++) compiler should I use? Any suggestions for which compiler?


    grt.
    Paul
    Last edited by pthoonen; 12-14-2008 at 09:55 PM.

  7. #7
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    7

    Default

    Let's join forces, see Thread :: View topic - From Java with user interface Twain at Twain,...

    It's gonna get deep,...

    You hear a lot of things in Java, don't believe all of it == some of it is true.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

Similar Threads

  1. Random File Access
    By viper110110 in forum New To Java
    Replies: 11
    Last Post: 11-28-2008, 12:28 AM
  2. Access Linux shared folder from Windows OS
    By nitinpjairaj in forum Networking
    Replies: 3
    Last Post: 08-26-2008, 06:14 PM
  3. File Access Through Servlets
    By bipinkrishna15 in forum Java Servlet
    Replies: 1
    Last Post: 05-27-2008, 10:58 AM
  4. run a batch file as windows nt service
    By radhika123 in forum New To Java
    Replies: 0
    Last Post: 04-04-2008, 01:46 PM
  5. variable access from another file
    By riadmazloum in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 07:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •