[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 466
  • Last Modified:

usb midi communications

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
sargent240
Asked:
sargent240
  • 2
  • 2
  • 2
1 Solution
 
for_yanCommented:
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
 
sargent240Author Commented:
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
 
for_yanCommented:
Yes, these are tough things, I very much agree. This is more ofr system level programming than for java
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
CEHJCommented:
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
 
sargent240Author Commented:
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
 
CEHJCommented:
:)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now