본문 바로가기

Software/JAVA

(이전글 2009년) - 리눅스 환경에서 JAVA를 이용한 USB이용에 대한 문의드립니다.

반응형

실제로 어떤 사이트에 문의를 한 내용이다....

답변은.... 없었다.


-------------------------------------------------------------------------------------------------------------------------------------

리눅스 환경에서 자바프로젝트를 진행중에 있습니다

기기간의 CAN 통신을 사용하기 위해

serial USB converter FTDI사의 FT245BM USB로서 인식하여 (D2XX)

USB Bulk in, Bulk out  Endpoint를 사용하여 통신을 하려고 하고있습니다.

 

설치환경 :

cpu : via on bord C3

chipset : CLE266

linux : fedora core 5

java : jdk-1_5_08-linux-i586.rpm

jusb api : jusb-0.4.4-src

FT245BM linux driver (d2xx) : libusb-0.1.12

USB device : FT245BM

 

 

문의내용 :

 

1. USB관련 개발을 처음하는지라 BulkStream이 어떤코드로 되어있는지에 대해서 잘 알지 못하고 있습니다.

계속 숙지하기위해서 노력하지만어려움을 느끼고 있습니다참고할 만한 자료나 링크를 알려주셨으면 감사하겠습니다.

 

2. 기본 제공되는 소스코드에 추가 하여 out을 만들어 낼수 있었는데아무리해도 해석이 안되는 결과가 있는데

작은 부분이라도 해석가능한부분있으시면 많은 도움이 될 것 같습니다.


 

source : ListUSB.java

 

001.import java.io.BufferedReader;
002.import java.io.IOException;
003.import java.io.InputStream;
004.import java.io.InputStreamReader;
005.import java.io.OutputStream;
006.import usb.core.Bus;
007.import usb.core.Configuration;
008.import usb.core.Device;
009.import usb.core.DeviceDescriptor;
010.import usb.core.DeviceSPI;
011.import usb.core.Endpoint;
012.import usb.core.Host;
013.import usb.core.HostFactory;
014.import usb.core.Interface;
015. 
016.public class ListUSB
017.{
018. 
019.public static void main(String[] args)
020.{
021.try
022.{
023.// Bootstrap by getting the USB Host from the HostFactory.
024.Host   host = HostFactory.getHost();
025. 
026.// Obtain a list of the USB buses available on the Host.
027.Bus[]  bus  = host.getBusses();
028.int    total_bus = bus.length;
029.System.out.print("total_bus is : " + total_bus+ "\n");
030.// Traverse through all the USB buses.
031.for (int i=0; i<total_bus; i++)
032.{
033.// Access the root hub on the USB bus and obtain the
034.// number of USB ports available on the root hub.
035.Device root = bus[i].getRootHub();
036.int total_port = root.getNumPorts();
037.System.out.print("total_port : " + total_port+ "\n");
038.// Traverse through all the USB ports available on the
039.// root hub. It should be mentioned that the numbering
040.// starts from 1, not 0.
041.for (int j=1; j<=total_port; j++)
042.{
043.// Obtain the Device connected to the port.
044.Device device = root.getChild(j);
045. 
046. 
047.if (device != null)
048.{
049. 
050.DeviceDescriptor dds = device.getDeviceDescriptor();
051. 
052.System.out.print("getUSBVersion : " + dds.getUSBVersion()+ "\n");
053.System.out.print("getDeviceClass : " + dds.getDeviceClass()+ "\n");
054.System.out.print("getDeviceClassName : " + dds.getDeviceClassName()+ "\n");
055.System.out.print("getDeviceSubClass : " + dds.getDeviceSubClass()+ "\n");
056.System.out.print("getDeviceProtocol : " + dds.getDeviceClass()+ "\n");
057.System.out.print("getMaxPacketSize0 : " + dds.getMaxPacketSize0()+ "\n");
058.System.out.print("getVendorId : " + dds.getVendorId()+ "\n");
059.System.out.print("getProductId : " + dds.getProductId()+ "\n");
060.System.out.print("getDeviceId : " + dds.getDeviceId()+ "\n");
061.System.out.print("getManufacturer : " + dds.getManufacturer(j)+ "\n");
062.System.out.print("getManufacturerStringId : " + dds.getManufacturerStringId()+ "\n");
063.System.out.print("getProduct : " + dds.getProduct(j)+ "\n");
064.System.out.print("getProductStringIds : " + dds.getProductStringId()+ "\n");
065.System.out.print("getSerial : " + dds.getSerial(j)+ "\n");
066.System.out.print("getSerialStringId : " + dds.getSerialStringId()+ "\n");
067.System.out.print("getNumConfigurations : " + dds.getNumConfigurations()+ "\n");
068. 
069. 
070. 
071. 
072. 
073. 
074. 
075. 
076.// Obtain the current Configuration of the device and the number of
077.// Interfaces available under the current Configuration.
078.Configuration config = device.getConfiguration();
079.int total_interface = config.getNumInterfaces();
080.System.out.print("total_interface : " + total_interface+ "\n");
081.// Traverse through the Interfaces
082.for (int k=0; k<total_interface; k++)
083.{
084.// Access the currently Interface and obtain the number of
085.// endpoints available on the Interface.
086.Interface itf = config.getInterface(k, 0);
087. 
088.int total_ep  = itf.getNumEndpoints();
089.System.out.print("total_ep : " + total_ep + "\n");
090.// Traverse through all the endpoints.
091.for (int l=0; l<total_ep; l++)
092.{
093.// Access the endpoint, and obtain its I/O type.
094.Endpoint ep = itf.getEndpoint(l);
095.String io_type = ep.getType();
096.System.out.print("io_type : " + io_type + "\n");
097. 
098.if (io_type == "bulk")
099.{
100.boolean input  = ep.isInput();
101.// If the endpoint is an input endpoint, obtain its
102.// InputStream and read in data.
103. 
104.if (input)
105.{
106.System.out.print("run InputStream code \n");
107.InputStream in;
108.in = ep.getInputStream();
109.BufferedReader in_buf  = new BufferedReader(new InputStreamReader(in));
110.String ss;
111.ss = in_buf.toString();
112.System.out.print("BufferedReader values  : " + ss +"\n");
113.in_buf.close();
114. 
115. 
116. 
117. 
118. 
119.//System.out.print("ep.getInputStream() : " + in +"\n");
120.// Read in data here
121.in.close();
122.}
123.// If the Endpoint is and output Endpoint, obtain its
124.// OutputStream and write out data.
125.else
126.{
127.OutputStream out;
128.byte[]  x = {90,30,40,20,30};
129.int y = 3;
130. 
131.rwsion bw = new rwsion();
132.bw.writeBulk(y, x);
133. 
134.out = ep.getOutputStream();
135.System.out.print("run OutputStream code \n");
136.System.out.print("ep.getOutStream() : " + out +"\n");
137.// Write out data here.
138.out.close();
139.}
140.}
141. 
142.}
143.}
144.}
145.}
146.}
147.catch (Exception e)
148.{
149.System.out.println(e.getMessage());
150.}
151.}
152. 
153. 
154. 
155. 
156.}
157. 
158.class rwsion implements DeviceSPI
159.{
160.public void claimInterface(int arg0) throws IOException {
161. 
162.}
163.public int clearHalt(byte arg0) throws IOException {
164.return 0;
165.}
166.public Device getChild(int arg0) throws IOException {
167.return null;
168.}
169.public String getClaimer(int arg0) throws IOException {
170.return null;
171.}
172.public byte[] getConfigBuf(int arg0) throws IOException {
173.return null;
174.}
175.public byte[] readBulk(int arg0, int arg1) throws IOException {
176.return null;
177.}
178.public byte[] readControl(byte arg0, byte arg1, short arg2, short arg3, short arg4) throws IOException {
179.return null;
180.}
181.public byte[] readIntr(int arg0, int arg1) throws IOException {
182.return null;
183.}
184.public void releaseInterface(int arg0) throws IOException {
185. 
186.}
187.public void setInterface(int arg0, int arg1) throws IOException {
188. 
189.}
190.public void writeBulk(int arg0, byte[] arg1) throws IOException {
191. 
192. 
193.}
194. 
195.public void writeControl(byte arg0, byte arg1, short arg2, short arg3, byte[] arg4) throws IOException {
196. 
197.}
198.public void writeIntr(int arg0, byte[] arg1) throws IOException {
199. 
200.}
201. 
202.}


Output

 

01.total_bus is : 4
02.total_port : 6
03.total_port : 2
04.total_port : 2
05.total_port : 2
06.getUSBVersion : 2.0
07.getDeviceClass : 0
08.getDeviceClassName : device
09.getDeviceSubClass : 0
10.getDeviceProtocol : 0
11.getMaxPacketSize0 : 8
12.getVendorId : XXX
13.getProductId : XXXX
14.getDeviceId : 4.0
15.getManufacturer : XXXX
16.getManufacturerStringId : 1
17.getProduct : XXXX XXXX
18.getProductStringIds : 2
19.getSerial : XXXXXXX
20.getSerialStringId : 3
21.getNumConfigurations : 1
22.total_interface : 1
23.total_ep : 2
24.io_type : bulk
25.run InputStream code
26.BufferedReader values  : java.io.BufferedReader@ec16a4
27.io_type : bulk
28.run OutputStream code
29.ep.getOutStream() : usb.core.Endpoint$BulkOutputStream@13a328f



반응형