1

Hi and first thank you for reading my question!

I have a device, which gives me an output of essentially just a RS232 serial signal (1200 baud, 8 databits, no parity bits, one stop bit). This output comes in to my android phone via the 3.5mm audio jack.

I want to store every data bit into a variable, display or calculate something and when the next bits come in, it should override the variable.

I know exactly what to do with it after I thave the databits in variables but I have no idea on how to do the basic input / stream / read from audio jack thing...

I found this but it dosn't help me very much: Android serial port via audio jack Maybe something like this: Android: Need to record mic input

Please help me, thank you!

Edit: More informations about my signal The payload is transmitted in 9 byte packets:

1: command byte as ASCII character ('I','A','S','L','R','C' or ' ')
2-6: time in ASCII chars (2:34:56)
7: checksum (64 + sum of time digits)
8: CR (carriage return, ASCII code 0x0D)
9: LF (line feed, ASCII code 0x0A)

Edit2: I also think I have to store the input into a buffer and then read the signals from the buffer... But how do I write serial input from audio jack into a buffer?

Edit3: Maybe something like this will work:

try
        {
            int N = AudioRecord.getMinBufferSize(8000,AudioFormat.CHANNEL_IN_MONO,AudioFormat.ENCODING_PCM_16BIT);
            recorder = new AudioRecord(AudioSource.MIC, 1200, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, N*10);
            track = new AudioTrack(AudioManager.STREAM_MUSIC, 1200, 
                    AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT, N*10, AudioTrack.MODE_STREAM);
            recorder.startRecording();
            track.play();
            /*
             * Loops until something outside of this thread stops it.
             * Reads the data from the recorder and writes it to the audio track for playback.
             */
            while(!stopped)
            { 
                Log.i("Map", "Writing new data to buffer");
                short[] buffer = buffers[ix++ % buffers.length];
                N = recorder.read(buffer,0,buffer.length);
                track.write(buffer, 0, buffer.length);
            }
        }
        catch(Throwable x)
        { 
            Log.w("Audio", "Error reading voice audio", x);
        }
        /*
         * Frees the thread's resources after the loop completes so that it can be run again
         */
        finally
        { 
            recorder.stop();
            recorder.release();
            track.stop();
            track.release();
        }
    }
Community
  • 1
  • 1
user2738829
  • 41
  • 1
  • 2
  • 6

1 Answers1

1

Whilst not a detailed answer to your problem, there are a number of problems with your current approach

  1. You are violating the Nyquist sampling theory by sampling your incoming signal at the line rate. Your system bandwidth needs to be at least twice the bitrate.

  2. You currently seem to have no way of framing your message to indicate a message start or end amongst a stream of bytes. In particular, both 0x0a and 0x0d might reasonably be expected elsewhere in the message body. You might consider using the MSB of each byte to signal framing, and only encode payload in the remaining 7-bits of each byte)

  3. You are attempting to sample a signal without clock recovery. The sender transmit clock and the receiver's sample clock are not synchronous - that is to say that the clock edges aren't aligned. They are likely to drift over time.

In addition, you will need need to take care of signal levels - RS232 is potentially signalled at 12v.

This is an already well-solved problem - I suggest looking at some of the modem standards. Some kind of modulation - such as Phase Shift Keying is typically used with some kind of clock recovery to deal with issues 1 and 3 above.

marko
  • 9,029
  • 4
  • 30
  • 46