Find the answer to your Linux question:
Results 1 to 4 of 4
Hey there, I have a program to read usb values in windows but Im looking to modify this for SuSe Linux. My current code is shown below: Code: // readusbdong.cpp ...
  1. #1
    Just Joined!
    Join Date
    Jun 2009
    Posts
    6

    Reading USB Values

    Hey there, I have a program to read usb values in windows but Im looking to modify this for SuSe Linux. My current code is shown below:

    Code:
    // readusbdong.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include <iostream>
    #include <string.h>
    #include <windows.h>
    #include "ftd2xx.h" // Needs to go after others
    
    using namespace std;
    
    // Function Prototypes
    void showUsage();
    
    // Some global variables
    bool bMask(false);            // Should we return all 8 bits?
    bool bEcho(false);            // Should we also echo to screen?
    
    int main(int argc, char* argv[])
    {
    #define READ_COMMAND 0x80
    #define READ_LENGTH  4
    #define WRITE_LENGTH 1
    
    
        // First see if we have any switches
        int i=1;
        while (i < argc)
        {
            if (strcmp("/mask", argv[i]) == 0)
            {
                // Mask specified
                bMask = true;
                i++;
            }
            else if (strcmp("/echo", argv[i]) == 0)
            {
                // Echo specified
                bEcho = true;
                i++;
            }
            else
            {
                // Don't understand the parameter
                showUsage();
                return 0;
            }
        } // Wend
    
        // Now try to read dongle
    
        FT_STATUS ftStatus;
        FT_HANDLE ftHandle;
    
        ftStatus = FT_OpenEx (
                    "FT245R USB FIFO",
                    FT_OPEN_BY_DESCRIPTION,
                    &ftHandle);
    
        if (ftStatus != FT_OK)
        {
            cout << "Failed to obtain file handle to device FT245R USB FIFO" << endl;
            return 255; // As we use this as a legit return, need to return illegal
                        // value
        }
    
    
        DWORD BytesWritten;
    
        char WriteBuffer [WRITE_LENGTH];
    
    
        WriteBuffer [0] = (char) READ_COMMAND; // 1st byte, read command
    
        ftStatus = FT_Write (
                    ftHandle,
                    &WriteBuffer,
                    WRITE_LENGTH, // Bytes to write
                    &BytesWritten);
    
        if ( (ftStatus != FT_OK) || (BytesWritten != WRITE_LENGTH) )
        {
            cout << "Failed to write" << endl;
            return 255; // As we use this as a legit return, need to return illegal
                        // value
        }
    
    
        Sleep (1) ; // Delay 1 ms
    
    
        DWORD BytesRead;
    
        char ReadBuffer [READ_LENGTH];
    
        ftStatus = FT_Read (
                    ftHandle,
                    &ReadBuffer,
                    READ_LENGTH, // Bytes to read
                    &BytesRead);
    
        if ( (ftStatus != FT_OK) || (BytesRead != READ_LENGTH) )
        {
            cout << "Failed to read" << endl;
            return 255; // As we use this as a legit return, need to return illegal
                        // value
        }
    
    
        char XOR_Result = ReadBuffer [0];
    
        XOR_Result ^= ReadBuffer [1];
        XOR_Result ^= ReadBuffer [2];
        XOR_Result ^= ReadBuffer [3];
    
        if (XOR_Result != 0)
        {
            cout << "Failed checksum" << endl;
            return 255; // As we use this as a legit return, need to return illegal
                        // value
        }
    
    
        ftStatus = FT_Close (
                    ftHandle);
    
        if (ftStatus != FT_OK)
        {
            cout << "Failed to close" << endl;
            return 255; // As we use this as a legit return, need to return illegal
                        // value
        }
    
    
    
        unsigned char nDongleValue = (unsigned char) ReadBuffer [1]; // 2nd byte
    
    
        // OK, if we've arrived here, we have legit values
        if(bMask)
        {
            // Mask out top two bits
            nDongleValue &= 63;
        }
    
        if(bEcho)
        {
            // Echo specified, so print to console
            cout << "Dongle id is " << (int)nDongleValue << endl;
        }
    
        // All done, just return the value
        return nDongleValue;
    }
    
    void showUsage()
    {
        cout << "readusbdong: Sets %ERROR_LEVEL% to value of USB Dongle" << endl;
        cout << "   usage: readusbdong [/full] [/echo]" << endl;
        cout << "   where /mask returns all 8 bits, default is lower 6" << endl;
        cout << "         /echo also prints value to console" << endl;
        return;
    }
    Is there an equivalent way to access the USB port in SuSe Linux?

    Cheers, Jag

  2. #2
    Linux Guru gogalthorp's Avatar
    Join Date
    Oct 2006
    Location
    West (by God) Virginia
    Posts
    3,105
    All devices in Linux show up in the /dev directory as files just open and use accordingly.

  3. #3
    Just Joined!
    Join Date
    Jun 2009
    Posts
    6
    Ah ok, I came across this directory before but had no clue as to what it was? How would I go about using one of these files?

  4. #4
    Linux Guru gogalthorp's Avatar
    Join Date
    Oct 2006
    Location
    West (by God) Virginia
    Posts
    3,105
    It's a file just open it like any other file.

Posting Permissions

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