reading stream of bytes from serial port
Hello.
I'm currently working on a project where I have to connect a camera to microcontroller and send a picture to PC.
The interfacing part is done but now I'm struggling with capturing the data on the PC side. Microcontroller sends a complete bmp file to PC in chunks: firstly header then info header and then data. And this is my problem I don't know how to capture all that and save it into bmp.
I appreciate any help
Regards
Kris
Re: reading stream of bytes from serial port
Can you just append each data chunk onto the end of an array or file?
Re: reading stream of bytes from serial port
Hi
Thank for the reply.
I am using java.comms package to communicate with microcontroller.
Yes my problem is with protocol.
I can read a single byte and send a byte but i have problems with reading loads of byte being sent at once. The data that are sent from microcontroller are image data that come from camera i connected to microcontroller so there is loads of data.
Code :
while(true){
System.out.println("Enter command");
while(in.hasNextInt()){
i = in.nextInt();
byte b = (byte)i;
a.UARTputc(b); //send command
byte s = a.UARTgetb(); //check if something came back
System.out.println(s); //for debug
if(s == 119){ //start of picture ascci for 'w'
do{
temp = a.UARTgetb();
pic[count] = temp;
count++;
System.out.println(count); //for debug
}while(temp != 10);//ascii for '\n'
this part i have now does not even get into the do-while loop
This is for reading the port:
Code :
case SerialPortEvent.DATA_AVAILABLE:
readBuffer = new byte[8];
try {
while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);
}
int byteCount = 0;
for (int i = 0; i < currentWrite.length; i++) {
if (currentWrite[i] == readBuffer[i]) {
byteCount++;
}
}
if (byteCount != currentWrite.length) {
dataIn = true;
}
} catch (IOException e) {System.out.println(e);}
break;
}
Code :
public void UARTputc(byte b) throws Exception{
byte[] data = {b};
sPort.write(data);
utils.pause(100);
}
public byte UARTgetb(){
byte t=0;
byte b[] = sPort.read();
t = b[0];
return t;
}
Any advice is appreciated
Regards