Voltage Measurement through Channels in PSLab
The Pocket Science Lab multimeter has got three channels namely CH1,CH2 and CH3 with different ranges for measuring the voltages.This blog will give a brief description on how we measure voltages in channels. Measuring Voltages at channels can be divided into three parts:- Communication between between device and Android. Setting up analog channel (analog constants) Voltage measuring function of android. Communication between PSLab device and Android App The communication between the PSLab device and Android occurs through the help of UsbManger package of CommunicationHandler class of the app. The main two functions involved in the communication are read and write functions in which we send particular number of bytes and then we receive certain bytes. The read function :- public int read(byte[] dest, int bytesToBeRead, int timeoutMillis) throws IOException { int numBytesRead = 0; //synchronized (mReadBufferLock) { int readNow; Log.v(TAG, "TO read : " + bytesToBeRead); int bytesToBeReadTemp = bytesToBeRead; while (numBytesRead < bytesToBeRead) { readNow = mConnection.bulkTransfer(mReadEndpoint, mReadBuffer, bytesToBeReadTemp, timeoutMillis); if (readNow < 0) { Log.e(TAG, "Read Error: " + bytesToBeReadTemp); return numBytesRead; } else { //Log.v(TAG, "Read something" + mReadBuffer); System.arraycopy(mReadBuffer, 0, dest, numBytesRead, readNow); numBytesRead += readNow; bytesToBeReadTemp -= readNow; //Log.v(TAG, "READ : " + numBytesRead); //Log.v(TAG, "REMAINING: " + bytesToBeRead); } } //} Log.v("Bytes Read", "" + numBytesRead); return numBytesRead; } Similarly the write function is - public int write(byte[] src, int timeoutMillis) throws IOException { if (Build.VERSION.SDK_INT < 18) { return writeSupportAPI(src, timeoutMillis); } int written = 0; while (written < src.length) { int writeLength, amtWritten; //synchronized (mWriteBufferLock) { writeLength = Math.min(mWriteBuffer.length, src.length - written); // bulk transfer supports offset from API 18 amtWritten = mConnection.bulkTransfer(mWriteEndpoint, src, written, writeLength, timeoutMillis); //} if (amtWritten < 0) { throw new IOException("Error writing " + writeLength + " bytes at offset " + written + " length=" + src.length); } written += amtWritten; } return written; } Although these are the core functions used for communication but the data received through these functions are further processed using another class known as PacketHandler. In the PacketHandler class also there are two major functions i.e sendByte and getByte(), these are the main functions which are further used in other classes for communication. The sendByte function:- public void sendByte(int val) throws IOException { if (!connected) { throw new IOException("Device not connected"); } if (!loadBurst) { try { mCommunicationHandler.write(new byte[] { (byte)(val & 0xff), (byte)((val >> 8) & 0xff) }, timeout); } catch (IOException e) { Log.e("Error in sending int", e.toString()); e.printStackTrace(); } } else { burstBuffer.put(new byte[] { (byte)(val & 0xff), (byte)((val >> 8) & 0xff) }); } } As we can see that in this function also the main function used is the write function of communicationHandler but in this class the data is further processed. Setting Up the Analog Constants For setting up the ranges, gains and other properties of channels, a different class of AnalogConstants is implemented in the android app, in this class all the properties which are used by the channels are defined which are further…
