Solved

usb midi communications

Posted on 2012-03-17
6
438 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Simple Linear Regression

695 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