This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Sending data from TM4C123GXL to java using UART

Hi, 

I am new to this field and using Tiva TM4C123GXL to send data to java through UART0 at a Baud rate of 9600.

115200 Baud rate was not able to receive the whole string It was only able to receive 18 characters, but 9600 baud rate is working perfectly fine for receiving the data.

I was using UARTSend() of uartecho example code to send data to JAVA. I am unable to send large string, its sending garbage values into java. I actually need to send double or float through UART, only 8 characters are being sent if I convert them to a string or use sprintf along with UARTSend. Is there any other way to send Double or Float values?

Also the message is getting printed twice in JAVA and empty spaces are represented as []

I have also tried Uartprintf but that is not working.  

Here is the UARTSend function that I am using:

void
UARTSend(const uint32_t *pui8Buffer, uint32_t ui32Count)
{
//
// Loop while there are more characters to send.
//
while(ui32Count--)
{
//
// Write the next character to the UART.
//
ROM_UARTCharPutNonBlocking(UART0_BASE, *pui8Buffer++);
}
}

PS: I have included all the required header files like #include "utils/uartstdio.h" #include "utils/uartstdio.c" for the UARTPrintf and all the Predefined Symbols by going through other threads.

Please help.

Thank You.

  • Hi Kaushik,

    Have you tried using a generic serial port monitor to receive the data from the Tiva? The problem could be just because of your java program. Right now i have SPU, in a lack of a better program for now.
    There is UARTStdio included in the utilities of TivaWare. It makes things so much easier and it usually works so well, why would you use a loop to send strings? It just adds the variable of maybe what you created to send strings is not working well.


    And what do you mean send data do JAVA? JAVA is a abstraction layer, not a specific program, could you specify? It's like saying, i send data to C#, doesn't make too much sense.

  • Hi Luis Afonso,

    I am using Eclipse Luna for java and this is the code that I am using for receiving the data. I require the values from the Tiva for moving objects in Java Swing

    import java.io.*;
    import java.util.*;
    
    //import javax.comm.*;
    import gnu.io.*;
    
    public class Newcomm implements Runnable, SerialPortEventListener {
        static CommPortIdentifier portId;
        static Enumeration portList;
        static OutputStream outputStream;
        InputStream inputStream;
        SerialPort serialPort;
        Thread readThread;
        
        public static String readString;
    
       public static void main(String[] args) {
    	   
    	   portList = CommPortIdentifier.getPortIdentifiers();
    	   while (portList.hasMoreElements()) {
               portId = (CommPortIdentifier) portList.nextElement();
               if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                    if (portId.getName().equalsIgnoreCase("COM4")) {
    			      Newcomm reader= new Newcomm();
                       reader.read();
                       
                   }
               }
           }
       }
    
        public void read() {
        	try {
                serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
            } catch (PortInUseException e) {System.out.println(e);}
            try {
                inputStream = serialPort.getInputStream();
            } catch (IOException e) {System.out.println(e);}
    	try {
                serialPort.addEventListener(this);
    	} catch (TooManyListenersException e) {System.out.println(e);}
            serialPort.notifyOnDataAvailable(true);
            try {
                serialPort.setSerialPortParams(9600,
                    SerialPort.DATABITS_8,
                    SerialPort.STOPBITS_1,
                    SerialPort.PARITY_NONE);
            } catch (UnsupportedCommOperationException e) {System.out.println(e);}
            readThread = new Thread(this);
            readThread.start();
        }
           
     public void run() {
            try {
                Thread.sleep(20000);
            } catch (InterruptedException e) {System.out.println(e);}
        }
    
        public void serialEvent(SerialPortEvent event) {
            switch(event.getEventType()) {
            case SerialPortEvent.BI:
            case SerialPortEvent.OE:
            case SerialPortEvent.FE:
            case SerialPortEvent.PE:
            case SerialPortEvent.CD:
            case SerialPortEvent.CTS:
            case SerialPortEvent.DSR:
            case SerialPortEvent.RI:
            case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
                break;
            case SerialPortEvent.DATA_AVAILABLE:
                byte[] readBuffer = new byte[20];
    
                try {
                    while (inputStream.available() > 0) {
                        int numBytes = inputStream.read(readBuffer);
                    }
                    readString=new String(readBuffer);
                    //readString=readString.trim();
                    System.out.print(readString);
                } catch (IOException e) {System.out.println(e);}
                break;
            }
        }
    }

    I am using the following Code in Code Composer. Firstly converting the float to string but its not able to convert the large double numbers greater than 8 digits.

    void ftoa(double f,char buf[50])
    {
        int pos=0,ix,dp,num;
        if (f<0)
        {
            buf[pos]='-';
            f = -f;
            //UARTCharPut(UART0_BASE,buf[pos]);
            pos++;
        }
        dp=0;
        while (f>=10.0)
        {
            f=f/10.0;
            dp++;
        }
        for (ix=1;ix<8;ix++)
        {
                num = (int)f;
                f=f-num;
                if (num>9){
                    buf[pos]='#';
                    //UARTCharPut(UART0_BASE,buf[pos]);
                    pos++;
                }
                else{
                    buf[pos]='0'+num;
                    UARTCharPut(UART0_BASE,buf[pos]);
                    pos++;
                }
                if (dp==0){
                	buf[pos]='.';
                	//UARTCharPut(UART0_BASE,buf[pos]);
                	pos++;
                }
                f=f*10.0;
                dp--;
        }
    }
    void
    UARTIntHandler(void)
    {
        uint32_t ui32Status;
    
        //
        // Get the interrupt status.
        //
        ui32Status = ROM_UARTIntStatus(UART0_BASE, true);
    
        //
        // Clear the asserted interrupts.
        //
        ROM_UARTIntClear(UART0_BASE, ui32Status);
         ftoa(temp[4],b[12]);//temp contains a large float val
    					              UARTSend((uint8_t *) b[12],20 ); 
    						//sprintf(b[12],"%f",temp[4]);
    }

    I have come to know that there is a problem in ftoa() as the value of b[12] is not right.

     The output in Eclipse is as follows:

     Can u suggest me any other function to use apart from UARTprintf as its not working.

    Thank You.

  • As i am not fluent in JAVA i can't help you there. But i would use another program just to monitor the serial port, just to rule out if the problem is with eclipse.

    I never had any problems with UARTprintf, so no reason for that being the problem. I would assume that that is not the problem. 

    Unfortunately i don't have time to test your code on my board

    Btw in your ftoa() you seem to be sending data, as well inside your interrupt handler. Maybe that's why it's printed twice?

    Edit:

    You keep pasting bits and pieces of your code. Could you zip your project files for the Tiva and upload it using the "Insert File" feature in the editor? 

  • I have tried the code using SPU as you said, I am encountering the same problem, so the java code is fine.

    Attaching my code by removing the irrelevant parts.

    3302.CommunicationProblem.zip

    I have tried using UARTprintf for sending both strings as well as double values, but its not working 

    with UARTSend  I am receiving garbage values (As u have suggested I hope i can find a way out using utilities/uartstdio rather than using while loop)