Solved

usb midi communications

Posted on 2012-03-17
6
430 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

This is about my first experience with programming Arduino.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

726 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