Solved

usb midi communications

Posted on 2012-03-17
6
420 Views
Last Modified: 2012-03-21
I am trying to communicate with a music keyboard through the usb port.  When I plug in my usb to midi cable "midi1" appears in the /dev directory.  Can I open it and read and write to it like a file.  Does the code below make any sense?

import java.io.BufferedWriter;
import java.io.FileWriter;

/**
 *
 * @author al
 */
public class WriteMidiFile {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

   try{
  // Create file
System.out.println("writing midi file");      
  FileWriter fstream = new FileWriter("midi1");
  BufferedWriter out = new BufferedWriter(fstream);
  out.write("Hello Java");
  //Close the output stream
  out.close();
  }catch (Exception e){//Catch exception if any
  System.err.println("Error: " + e.getMessage());
       
  }
    }
}
0
Comment
Question by:sargent240
  • 2
  • 2
  • 2
6 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 37733946
Most probably it would not work.
It would rather need a  a special driver.
why you have it this way
  FileWriter fstream = new FileWriter("midi1");

and not this way:

  FileWriter fstream = new FileWriter("/dev/midi1");


but Just compile and try - you have device - it is easy to try
0
 

Author Comment

by:sargent240
ID: 37733992
I had it set to /dev/midi1.  It was just a flyer.  It didn't work.  I wrote a c routine to communicate with the rs232 port and call it from my java program.  It works fine.  I was just messing with the usb port out of curiosity.  It's a lot of screwin' around to use the usb port.  Not worth the hastle.  Thanks.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37733997
Yes, these are tough things, I very much agree. This is more ofr system level programming than for java
0
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 37734527
and not this way:

  FileWriter fstream = new FileWriter("/dev/midi1");

Even if it would work (doubtful) you would never use a Writer to do non-text operations. OutputStream
0
 

Author Closing Comment

by:sargent240
ID: 37750292
I ended up using the following code and it worked.  Thanks!


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package readcharfromfile;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
 
public class ReadCharFromFile {

   
  public static void main(String[] args) {
 
    String strFilePath = "/dev/midi1";
   
    try
    {
    int count = 0;
      //create FileInputStream object
      FileInputStream fin = new FileInputStream(strFilePath);
     
      /*
       * To create DataInputStream object, use
       * DataInputStream(InputStream in) constructor.
       */

       DataInputStream din = new DataInputStream(fin);
//din.close();    
       /*
        * To read a Java character primitive from file, use
        * byte readChar() method of Java DataInputStream class.
        *
        * This method reads 2 bytes and returns unicode char value(Unicode char
        * occupies 2 bytes).
*/
       char ch;
       boolean i = true;
       while(i = true) {
         ch = din.readChar();
if (Integer.valueOf(ch) != 248 ) {
    if (Integer.valueOf(ch) != 254) {
        System.out.println("Char: " + Integer.valueOf(ch));
        count++;
        System.out.println("count = " + count);
    }
}
        if (count == 39) { break; }
       }
        /*
         * To close DataInputStream, use
         * void close() method.
         */
         din.close();
         count = 0;
    }
    catch(FileNotFoundException fe)
    {
      System.out.println("FileNotFoundException : " + fe);
    }
    catch(IOException ioe)
    {
      System.out.println("IOException : " + ioe);
    }
  }
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 37750300
:)
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
LINUX backups with VEEAM 8 78
Delphi: barcode reading on android platform 1 26
error log using ftp 7 39
Java Restore security prompts not working 10 9
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question