• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Sending "string" serially (byte code given)

akn10red

Lurker
Hi all,
I am developing an android app for my project which sends data serially, through RS232 + OTG cable, to a circuit developed by me (and the PC code for which is already done and developed in C#). I'm new to android and tried to get help from example apps. i found an app which establishes the serial communication successfully but the problem is that it sends data in the form of byte, but i want data to be sent as a string and also received as a string. portion of the activity of interest is being added which sends binary 10101010... but I want data in string format. like "abcdefg". it must also be noted that this code is ignoring the incoming data but i want the incoming data to be stored in a variable (which i think is not very difficult but it could be handy if someone can also guide about that)
if someone can help, it'll be highly appreciated

[HIGH]package android_serialport_api.sample;

import java.io.IOException;
import java.util.Arrays;

import android.os.Bundle;
import java.io.*;
import java.util.*;
//import javax.comm.*; // for SUN's serial/parallel port libraries

public class Sending01010101Activity extends SerialPortActivity {

SendingThread mSendingThread;
byte[] mBuffer;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sending01010101);
mBuffer = new byte[1024];
Arrays.fill(mBuffer, (byte) 0x55);
if (mSerialPort != null) {
mSendingThread = new SendingThread();
mSendingThread.start();
}
}

@Override
protected void onDataReceived(byte[] buffer, int size) {
// ignore incoming data
}

private class SendingThread extends Thread {
@Override
public void run() {
while (!isInterrupted()) {
try {
if (mOutputStream != null) {
mOutputStream.write(mBuffer);
} else {
return;
}
} catch (IOException e) {
e.printStackTrace();
return;
}
}
}
}
}
[/HIGH]
 
not enough info is availabe on the internet on this topic and I only expect help from this forum. its very important for me :(
 
When you say you want it as a string, do you mean you want the ASCII encoded string of the value you are sending (in your case, 01010101)? I am not quite sure I understand the question.

EDIT: Also, where is mOutpusStream coming from? Is it a member of SerialPortActivity? What specific kind of OutputStream is it?
 
When you say you want it as a string, do you mean you want the ASCII encoded string of the value you are sending (in your case, 01010101)? I am not quite sure I understand the question.

EDIT: Also, where is mOutpusStream coming from? Is it a member of SerialPortActivity? What specific kind of OutputStream is it?

actually I got this code from https://code.google.com/p/android-serialport-api/ and there is nothing changed by me in this code... I just want data to be sent as "string"... the way we send in (say) hyperterminal in windows... If, for example, I want to send string "ATSP" and then want to show the received data in a text box, how would I modify this code?

EDIT: and YES mOutputStream is a member of SerialPortActivity...
 
Something like this:
[high]
public class Sending01010101Activity extends SerialPortActivity {

SendingThread mSendingThread;
byte[] mBuffer;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sending01010101);
mBuffer = new byte[1024];
Arrays.fill(mBuffer, (byte) 0x55);
if (mSerialPort != null) {
mSendingThread = new SendingThread();
mSendingThread.start();
}
}

@Override
protected void onDataReceived(byte[] buffer, int size) {
// ignore incoming data
}

private class SendingThread extends Thread {
@Override
public void run() {
while (!isInterrupted()) {
try {
if (mOutputStream != null) {
String byteStr = new String(mBuffer);
PrintWriter pw = new PrintWriter(mOutputStream);
pw.write(byteStr);
} else {
return;
}
} catch (IOException e) {
e.printStackTrace();
return;
}
}
}
}
}
[/high]
 
thanks for the reply, but i probably couldn't explain what I want.... I want a string (lets say "abcd") to be sent serially. and the received data should be displayed in a text box...
 
Back
Top Bottom