Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Beginner
955 Views

How to receive bytes that include zeros via Java MRAA Uart class?

I have Edison serial communication working in a simple Java program in the Intel System Studio IoT Edition of Eclipse. The program runs, and receives heartbeat packets from a flight controller module, except for one little problem: The packets are truncated as soon as a zero is encountered! Here's the code:

import mraa.Result;

import mraa.Uart;

import mraa.UartParity;

public class Test

{

public static void main(String[] args)

{

Uart uart = new Uart(0);

if (uart.setBaudRate(921600) != Result.SUCCESS) {

System.err.println("Error setting baud rate");

System.exit(1);

}

if (uart.setMode(8, UartParity.UART_PARITY_NONE, 1) != Result.SUCCESS) {

System.err.println("Error setting mode");

System.exit(1);

}

if (uart.setFlowcontrol(false, false) != Result.SUCCESS) {

System.err.println("Error setting flow control");

System.exit(1);

}

while (true)

{

String s = uart.readStr(2048);

for(int i = 0; i < s.length(); i++)

{

char c = s.charAt(i);

String hex = String.format("%04x", (int) c);

System.out.print(hex + " ");

}

System.out.println();

}

}

}

Like I said, it works, except that the Uart.readStr() method truncates at nulls. I would like to use the int read(char *data, int length) method that's available in C/C++, but it's not available in the Java port, for some bizarre reason. Any workarounds? Thanks!

-- Will

0 Kudos
7 Replies
Highlighted
Community Manager
12 Views

Hi Will,

 

 

Thanks for your interest in the Intel Edison Platform.

 

 

We appreciate the information provided, could you please help us to clarify if when you are referring that the Uart.readStr() method truncates at nulls, it is because you are not receiving any data through the uart port ?

 

 

Also, we would like to suggest you to use the function dataAvailable() (more details regarding this function here: https://iotdk.intel.com/docs/master/mraa/java/classmraa_1_1_uart.html# a20841ec8d91109a304e082e421bea790 Uart Class Reference), and you will be able to read data only when it is available, please try adding this to your code and let us know your results.

 

 

Regards,

 

-Yermi

 

0 Kudos
Highlighted
Community Manager
12 Views

Hi Will,

 

 

We would like to know if you have any update.

 

 

Regards,

 

-Yermi A.

 

0 Kudos
Highlighted
Beginner
12 Views

I am definitely receiving data through the UART port. It's just incomplete. Here's an example of the data that I receive using readStr in Java:

00fe 0009 0016 0001 0001

00fe 0009 0017 0001 0001

00fe 0009 0018 0001 0001

00fe 0009 0019 0001 0001

00fe 0033 001a 0001 0001 00fd 0002 0050 0072 0065 0041 0072 006d 003a 0020 0052 0043 0020 006e 006f 0074 0020 0063 0061 006c 0069 0062 0072 0061 0074 0065 0064

0041 0072 006d 003a 0020 0043 006f 006d 0070 0061 0073 0073 0020 006e 006f 0074 0020 0063 0061 006c 0069 0062 0072 0061 0074 0065 0064

00fe 0009 001d 0001 0001

00fe 0009 001e 0001 0001

00fe 0009 001f 0001 0001

00fe 0009 0020 0001 0001

and here's an example of some correct data that I receive using a python script:

fe09360101000000000002035103037d2c

fe09370101000000000002035103039752

fe093801010000000000020351030373cd

fe093901010000000000020351030399b3

fe333a0101fd0250726541726d3a205243206e6f742063616c69627261746564000000000000000000000000000000000000000000000000005f32

fe333b0101fd0250726541726d3a20436f6d70617373206e6f742063616c6962726174656400000000000000000000000000000000000000001dd8

fe093c010100000000000203510303ca3e

fe093d0101000000000002035103032040

fe093e0101000000000002035103031ec3

fe093f010100000000000203510303f4bd

This is the python script:

import serial

import codecs

ser = serial.Serial('/dev/ttyMFD1', baudrate=921600)

packet = ''

while True:

byte = ser.read(size=1)

hexstr = codecs.encode(byte, 'hex_codec')

packet += str(hexstr)

if hexstr == 'fe':

packet = packet[:-2]

print(packet)

packet = 'fe'

Observe the similarity (except for the formatting, which doesn't bother me). The main problem is that readStr simply terminates at 0000, which makes sense, because strings are often null-terminated, right? That's why I am asking about a workaround for the lack of a read(int) method.

0 Kudos
Highlighted
Community Manager
12 Views

Hi Will,

 

 

Thank you for all the information provided and clarify me that. I have found this information regarding the Java strings: https://www.quora.com/Do-strings-in-Java-also-terminate-with-a-0-character-like-strings-in-C Do strings in Java also terminate with a '\0' character like strings in C? and http://stackoverflow.com/questions/17595209/why-strings-are-terminated-with-null-character-in-java why strings are terminated with null character in java?, there is mentioned that the Java strings are not null terminated, however, the behavior you are getting is very strange since the readStr terminates at 0000. I'd suggest you to report this issue in the "issues" tab in the https://github.com/intel-iot-devkit/mraa/issues mraa web site since they could give you a more accurate answer.

 

 

Moreover, we would like to know if could be an option to use C/C++ instead of Java in the Intel System Studio IoT Edition in order to use int read (char *data, int length) and test if it works.

 

 

Regards,

 

-Yermi

 

0 Kudos
Highlighted
Community Manager
12 Views

Hi Will,

 

 

I would like to know if you have considered the option to use C/C++ in the Intel System Studio IoT Edition.

 

 

Regards,

 

-Yermi

 

0 Kudos
Highlighted
Beginner
12 Views

I have considered and rejected that option. I have opted to program in Python for the Edison (without the Intel System Studio IoT Edition) for now. I may consider returning to Intel System Studio IoT Edition if the Java libraries are completed in future.

0 Kudos
Highlighted
Community Manager
12 Views

Hi Will,

 

 

Thanks for your reply, I understand and using Python is the best option, good luck with your project, also, we'd suggest you to report this behavior in the "issues" tab in the https://github.com/intel-iot-devkit/mraa/issues Mraa web site since they are in charge of it and they would be aware of this behavior.

 

 

Regards,

 

-Yermi A.

 

0 Kudos