title: What encoding is this string?
body: <p>I am trying to write this string out of a serial port and I get an error java.io.UnsupportedEncodingException: *995R15,67,3#.  As far as i can see this is just ASCII characters?  Can anyone tell me why i get this error?</p>
  
  <p>I have edited my Question to include everything in case there is something else wrong  The first one is EBIAlarm.java</p>
  
  <pre><code>import java.io.*;
  
  import javax.swing.*;
  
  import java.io.IOException;
  import java.io.OutputStream;
  import java.io.UnsupportedEncodingException;
  import java.nio.charset.Charset;
  import java.nio.charset.StandardCharsets;
  
  
  public class EBIAlarm extends javax.swing.JFrame {
  
  public TwoWaySerialComm twoWaySerCom;
  public TwoWaySerialComm.SerialWriter serialWriter;
  
  
  public EBIAlarm() {
      initComponents();
      twoWaySerCom = new TwoWaySerialComm();
  
  
  
      try {
  
          twoWaySerCom.connect("COM7");
      } 
      catch (Exception e) {
  
          e.printStackTrace();
          }
  }
  
  
  
  private void initComponents() {
  
      jTextArea1 = new javax.swing.JTextArea();
      jTextArea2 = new javax.swing.JTextArea();
      jScrollPane1 = new javax.swing.JScrollPane();
      jButton1 = new javax.swing.JButton();
      jButton2 = new javax.swing.JButton();
      jButton3 = new javax.swing.JButton();
      jButton4 = new javax.swing.JButton();
      jTextArea1.setColumns(60);
      jScrollPane1.setViewportView(jTextArea2);
  
  
  
  
      setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
      setTitle("Alarm Generator");
  
      jTextArea1.setText("Enter custom string here");
      jTextArea2.setText("Result");
      jButton1.setText("Alarm 1");
      jButton2.setText("Alarm 2");
      jButton3.setText("Alarm 3");
      jButton4.setText("Custom Alarm");
  
      javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
      getContentPane().setLayout(layout);
      layout.setHorizontalGroup(
          layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addGroup(layout.createSequentialGroup()               
  
                      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)    
                      .addComponent(jButton1)
                      .addComponent(jButton2)
                      .addComponent(jButton3)
                      .addComponent(jButton4)
                      .addComponent(jTextArea1)
                      .addComponent(jTextArea2)
                      .addComponent(jScrollPane1)
  
  
      )));
      layout.setVerticalGroup(
          layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addGroup(layout.createSequentialGroup()
  
              .addComponent(jButton1)
              .addGap(20)
              .addComponent(jButton2)
               .addGap(20)
              .addComponent(jButton3)
               .addGap(20)
              .addComponent(jButton4)
              .addGap(20)
              .addComponent(jTextArea1)
              .addGap(20)
              .addComponent(jTextArea2)
              .addContainerGap(0,0))
              .addComponent(jScrollPane1)
      );
  
      pack();
  
      jButton1.addActionListener(new java.awt.event.ActionListener() {
          public void actionPerformed(java.awt.event.ActionEvent evt){
              jButton1ActionPerformed(evt);
          }
  
          private synchronized void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
              jTextArea1.setText("Alarm 1 Activated, String: "+alarm1);
              try {
                  alarm1.getBytes(alarm1);
                  serialWriter.out.write(alarm1.getBytes("US-ASCII"));
              } catch (IOException e) {
                  // Do something to handle the exception
                  e.printStackTrace();
              }
          }
  
      });
  
      jButton2.addActionListener(new java.awt.event.ActionListener() {
          public void actionPerformed(java.awt.event.ActionEvent evt) {
              jButton2ActionPerformed(evt);
          }
  
          private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
              jTextArea2.setText("Alarm 2 Activated, String: "+alarm2);
          }
      });
  
      jButton3.addActionListener(new java.awt.event.ActionListener() {
          public void actionPerformed(java.awt.event.ActionEvent evt) {
              jButton3ActionPerformed(evt);
          }
  
          private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
              jTextArea2.setText("Alarm 3 Activated, String: "+alarm3);
          }
      });
  
      jButton4.addActionListener(new java.awt.event.ActionListener() {
          public void actionPerformed(java.awt.event.ActionEvent evt) {
              jButton4ActionPerformed(evt);
          }
  
          private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
              jTextArea2.setText("Custom alarm, string sent: "+jTextArea1.getText ());
          }
      });
  }
  
  
  public static void main(String args[]) {        
  
      java.awt.EventQueue.invokeLater(new Runnable() {
          public void run() {
              new EBIAlarm().setVisible(true);
  
  
          }
      });
  
  
  }
  
  
  
      private void updateTextArea(final String text) {
            SwingUtilities.invokeLater(new Runnable() {
              public void run() {
                jTextArea2.append(text);
              }
            });
          }
      private void redirectSystemStreams() {
            OutputStream out = new OutputStream() {
              @Override
              public void write(int b) throws IOException {
                updateTextArea(String.valueOf((char) b));
              }
  
              @Override
              public void write(byte[] b, int off, int len) throws IOException {
                updateTextArea(new String(b, off, len));
              }
  
              @Override
              public void write(byte[] b) throws IOException {
                write(b, 0, b.length);
              }
            };
  
            System.setOut(new PrintStream(out, true));
            System.setErr(new PrintStream(out, true));
          }
  
  // Variables declaration                    
  private static javax.swing.JButton jButton1;
  private javax.swing.JButton jButton2;
  private javax.swing.JButton jButton3;
  private javax.swing.JButton jButton4;
  private javax.swing.JTextArea jTextArea1;
  public javax.swing.JTextArea jTextArea2;
  private javax.swing.JScrollPane jScrollPane1;
  String alarm1 = "*995R15,67,3#";
  String alarm2 = "*993R03,67,6#";
  String alarm3 = "*994R14,67,1#";
  
  // End of variables declaration                   
  }
  </code></pre>
  
  <p>This is TwoWaySerialComm.java</p>
  
  <pre><code>import gnu.io.CommPort;
  import gnu.io.CommPortIdentifier;
  import gnu.io.SerialPort;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.OutputStream;
  
  
  
  
  
  public class TwoWaySerialComm {
  public TwoWaySerialComm()
  
  {
      super();
  }
  
  void connect ( String portName ) throws Exception
  {
  
      CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
      if ( portIdentifier.isCurrentlyOwned() )
      {
  
          System.out.println("Error: Port is currently in use");
      }
      else
      {
          CommPort commPort = portIdentifier.open(this.getClass().getName(),2000);
  
          if ( commPort instanceof SerialPort )
          {
              SerialPort serialPort = (SerialPort) commPort;
              serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
  
              InputStream in = serialPort.getInputStream();
              OutputStream out = serialPort.getOutputStream();
  
              (new Thread(new SerialReader(in))).start();
              (new Thread(new SerialWriter(out))).start();
  
          }
          else
          {
              System.out.println("Error: Only serial ports are handled by this.");
          }
      }     
  }
  
  /** */
  public static class SerialReader implements Runnable 
  {
      InputStream in;
  
      public SerialReader ( InputStream in )
      {
          this.in = in;
      }
  
      public void run ()
      {
          byte[] buffer = new byte[1024];
          int len = -1;
          try
          {
              while ( ( len = this.in.read(buffer)) &gt; -1 )
              {
                  System.out.print(new String(buffer,0,len));
  
              }
          }
          catch ( IOException e )
          {
              e.printStackTrace();
          }            
      }
  }
  
  /** */
  public static class SerialWriter implements Runnable 
  {
      OutputStream out;
  
      public SerialWriter ( OutputStream out )
      {
          this.out = out;
      }
  
      public void run ()
      {
          try
          {                
              int c = 0;
              while ( ( c = System.in.read()) &gt; -1 )
              {
                  this.out.write(c);
              }                
          }
          catch ( IOException e )
          {
              e.printStackTrace();
          }            
      }
  }
  
  
  
  }
  </code></pre>