Solved

usb midi communications

Posted on 2012-03-17
6
409 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
Comment Utility
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
Comment Utility
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
Comment Utility
Yes, these are tough things, I very much agree. This is more ofr system level programming than for java
0
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!

 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
:)
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
LDAP find expired users 8 41
change time in cron 4 37
Error with Java/Cache JDBC Classpath 2 19
maven project error 5 17
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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:
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now