Şerife Özdamarlar
Şerife Özdamarlar
Ankara
05/09/2014 tarihinden beri üye
80 GY Puanı
44K GY Sırası

Kişisel Sayfaları

İlgi Alanları

2 Rozet
2 Sertifika
1 Soru Sordu
1 Cevap Verdi
0 Blog Yazısı
0 Etiket Takibi

Hakkında

İş Tecrubesi

Kullanıcıya ait İş tecrübesi bilgisi bulunmamaktadır.

Eğitim Geçmişi

Çankaya Üniversitesi
| Aralık 2020 - Aralık 2020

Sertifikalar & Başarılar

GY Sertifikaları (2)
Android 201 Sertifikası
Veriliş Tarihi: Eylül 2014
Android 101 Sertifikası
Veriliş Tarihi: Eylül 2014
Diğer Sertifikaları (0)
Kullanıcıya ait sertifika bulunmamaktadır.
Test Sonuçları (0)

Kullanıcıya ait test sonucu bulunmamaktadır.

Dil Becerileri

Son Forum Aktiviteleri

2
Tümünü Gör

Android USB iletişimi

Merhaba arkadaşlar,

 

Android uygulamamda RS232 cihazı ile USB bağlantısı kurmaya çalışıyorum. Örnek koduma göre bağlantıyı kurup veri alışverişi yapabiliyorum. Fakat koddaki gibi date, time ve date-time okuma işlemi yapmak istediğimde; date, time verilerini doğru bir şekilde ayrı ayrı okuyabilirken date-time verisini bazen doğru bir şekilde okuyamıyorum. Geri döndürülen değer;

 

1 96 0 0 0....  gibi birşey oluyor. Bazen doğru okuyup bazen bu değeri okumasının nedeni ne olabilir?


 




 //Getting usb manager, it tells all devices that are connected
        um=(UsbManager) getSystemService(Context.USB_SERVICE);


            //hashmap will tel list of connected usb's
            myDeviceList = um.getDeviceList();


            //Iterator is made of usbDevices.. It one by one iterates through the list of usb in loop
            myIterator = myDeviceList.values().iterator();

            while (myIterator.hasNext()) {
                device = myIterator.next();

                //showing name of usb
                Toast.makeText(this, "NAME OF MY USB IS:" + device.getDeviceName(), Toast.LENGTH_SHORT).show();
            }

            //OR SIMPLY GET
            UsbDevice myDevice = myDeviceList.get("/dev/bus/usb/001/002");

        if(myDevice!=null)
              tv_dvc.setText(myDevice.getDeviceName());


            final BroadcastReceiver abcd = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent myIntent) {
                    String myPermission = myIntent.getAction();


                    if (permission.equals(myPermission)) {
                        synchronized (this) {
                            final UsbDevice myDevice = (UsbDevice) myIntent.getParcelableExtra(um.EXTRA_DEVICE);

                            //Permission given
                            if (myIntent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                                if (myDevice != null) {

                                    //Communication or data is being transferred

                                    //desired interface for desired device
                                    ui = myDevice.getInterface(0);


                                /*  Interface 0
                                    Endpoint 0: Inbound, type- bulk, max packet size- 64

                                    Endpoint 1: Outbound, type- bulk, max packet size- 64*/

                                    epOut = ui.getEndpoint(1);
                                    epIn = ui.getEndpoint(0);

                                    //desired connection
                                    con = um.openDevice(myDevice);
                                    con.claimInterface(ui, true);

                                    setPort(con);



                                    btn_get_buzzer.setOnClickListener(onClick);
                                    btn_get_date_time.setOnClickListener(onClick);
                                    btn_get_date.setOnClickListener(onClick);
                                    btn_get_time.setOnClickListener(onClick);


                                }
                            }

                        }
                    }

                }
            };

            //now in order to register broadcast receiver this is syntax
            PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(permission), 0);
            IntentFilter intentFilter = new IntentFilter(permission);
            registerReceiver(abcd, intentFilter);

            if (myDevice != null)
                um.requestPermission(myDevice, pendingIntent);




    }








public int sendCommand(UsbDeviceConnection con, UsbEndpoint epOut, UsbEndpoint epIn, byte cmd, int adr, int cnt, byte[] data) {


        setPort(con);

        int sent = 0; //count of bytes sent
        byte add = 0; //simple chksum
        int unsigned_cmd;


        byte[] header = {0x23};

        unsigned_cmd = unsigned_int(cmd);

        byte[] cmd_array = {(byte) unsigned_cmd};


        byte[] address = {(byte) (adr >> 8), (byte) (adr & 0xFF)};


        byte[] count = {(byte) (cnt >> 8), (byte) (cnt & 0xFF)};


        con.bulkTransfer(epOut, header, header.length, 0);  //send header  #, 0x23
        sent++;
        add += 0x23;


        con.bulkTransfer(epOut, cmd_array, 1, 0);  //send cmd byte
        sent++;
        add += cmd;

        con.bulkTransfer(epOut, address, address.length, 0);  //send address, msb first
        sent += address.length;
        add += (byte) ((adr >> 8) & 0xFF);
        add += (byte) (adr & 0xFF);


        con.bulkTransfer(epOut, count, count.length, 0);  //send count, msb first
        add += (byte) ((cnt >> 8) & 0xFF);
        add += (byte) (cnt & 0xFF);
        sent += count.length;


        //  if(cmd==(byte)0x80)
        if ((unsigned_cmd > 127) && (cnt > 0)) { //if it is a write command
            con.bulkTransfer(epOut, data, cnt, 0);  //send data, if it is a write command

        }
        add += adder(data, cnt);
        sent += cnt;

        con.bulkTransfer(epOut, new byte[]{add}, 1, 0);  //send chksum


        return sent;
    }





 public void setPort(UsbDeviceConnection con) {
        con.controlTransfer(0x40, 0, 0, 0, null, 0, 0);//reset
        con.controlTransfer(0x40, 0, 1, 0, null, 0, 0);//clear Rx
        con.controlTransfer(0x40, 0, 2, 0, null, 0, 0);//clear Tx
        con.controlTransfer(0x40, 0x03, 0x001A, 0, null, 0, 0);//baudrate
        con.controlTransfer(0x40, 0x02, 0x0000, 0, null, 0, 0);//flow control none
        con.controlTransfer(0x40, 0x04, 0x0008, 0, null, 0, 0); //data bit 8, parity none, stop bit 1, tx off
        // 1st parameter - 0x40 -request to set or get control data
        // 2nd parameter - 0x04 -type of data to be set or get
        // 3rd parameter - 0x0008- data bit 8, parity none, stop bit 1, tx off
    }

 

8 yıl 5 ay önce yanıtladın

Android USB iletişimi

06 Ekim 2015 tarihinde cevaplandı

Arkadaşlar sorumun cevabını buldum;

 

BulkTransfer çok hızlı bir şekilde iletim sağladığı için 50 ms'lik bir delay gerekiyormuş. sendCommand() methoduna delay koyduğumda problem ortadan kalktı.