2 Replies Latest reply: Apr 11, 2011 2:21 PM by jschellSomeoneStoleMyAlias RSS

    microcontroller doesn't receive input

    843939
      Finally found the correct forum to paste this question of mine.

      Hi! I have this Java code to write to a serial port. The code compiles all right, and the hardware is a USB-to-Serial and has no problem (I have tried it on hyperterminal).
      NO EXCEPTION occurs, but the microcontroller on the other end of the serial doesn't receive ANY input.
          SerialPort s_port;
          OutputStream o_stream;
          Enumeration<?> en;
          CommPortIdentifier portId[];
      
              s_port = null;
              o_stream = null;
               en=CommPortIdentifier.getPortIdentifiers();
               int i = 0;
              while(en.hasMoreElements())
              {
                   en.nextElement();
                   i++;
              }
               System.out.println("there are "+i +" ports");
               en=CommPortIdentifier.getPortIdentifiers();
               portId = new CommPortIdentifier;
           i = 0;
      while(en.hasMoreElements())
      {
           portId[i] = (CommPortIdentifier) en.nextElement();
           System.out.println("port "+portId[i].toString() +" is a " +portId[i].getName()
                     +" of type " +portId[i].getPortType());
           i++;
      }
      for(int a=0; a<i; a++)
      {
           if(portId[a].getName().equals("COM5"))
           {
                System.err.println("COM5 is in index "+a);
                addressInArray = a;
                break;
           }
      }

      try {
           System.out.println("A: initializing it");
           if(addressInArray!=1000)
                s_port = (SerialPort)portId[addressInArray].open("tesis Server v1.0", 30000);
           s_port.setSerialPortParams(19200,
                          SerialPort.DATABITS_8,
                          SerialPort.STOPBITS_1,
                          SerialPort.PARITY_NONE);
           s_port.setFlowControlMode(SerialPort.FLOWCONTROL_NONE
                     | SerialPort.FLOWCONTROL_NONE);
           o_stream = s_port.getOutputStream();
           } catch (IOException e) {
                System.err.println("unknown exception 4");
                e.printStackTrace();
           }
                char c1 = 'L';
                char c2 = '\n';
                try
                {
           System.out.println("A: writing");
                     o_stream.write(c1);
                     o_stream.write(c2);
                }catch(Exception ex){
                     System.err.println("cannot write");
                }

      I know that the microcontroller doesn't receive any input because it's supposed to light a certain LED when it receives any input. Hyperterminal does that well.

      Any ideas pls?
        • 1. Re: microcontroller doesn't receive input
          sabre150
          840936 wrote:

          I know that the microcontroller doesn't receive any input because it's supposed to light a certain LED when it receives any input. Hyperterminal does that well.
          You don't know that. There are any number of possibilities (wrong serial port, baud rate wrong, wrong handshake, crossed input and output etc) most of which can be checked by attaching an oscilloscope (or similar) to the port. The first thing I do with any serial port stuff is to create a loopback connector to see if I am talking to the correct port at the correct speed using the correct handshake.
          • 2. Re: microcontroller doesn't receive input
            jschellSomeoneStoleMyAlias
            If it was me I would try adding a flush.

            And '\n' is not the only way end of line can be sent, and isn't what gets sent on windows.