From Atizs, 8 Months ago, written in Plain Text.
  1. import jssc.SerialPortList;
  2. import jtermios.Termios;
  3. import jtermios.TimeVal;
  4. import purejavacomm.SerialPort;
  5.  
  6. import java.io.IOException;
  7. import java.io.UnsupportedEncodingException;
  8. import java.util.ArrayList;
  9. import java.util.Arrays;
  10. import java.util.List;
  11.  
  12. import static jtermios.JTermios.*;
  13. public class Read {
  14.  int fd;
  15.  String port = "/dev/ttyUSB0";
  16.  
  17.     public Read() {
  18.     }
  19.  
  20.  
  21.     public void readPort()
  22.     {
  23.         fd = open(port, O_RDWR | O_NOCTTY | O_NONBLOCK); // file descriptor, open port for reading and writing (O_RDWR), if path identifies a terminal device, open() shall not cause the terminal device to become the controlling terminal for the process.(O_NOCTTY)  an open() for reading-only shall return without delay. An open() for writing-only shall return an error if no process currently has the file open for reading.(O_NONBLOCK )
  24.         if (fd <= 0) {
  25.  
  26.  
  27.             System.out.println("Could not open " + port);
  28.  
  29.         }
  30.         fcntl(fd, F_SETFL, 0);//Set the file status flags to the value specified by arg.
  31.  
  32.         Termios opts = new Termios();
  33.         tcgetattr(fd, opts); //The tcgetattr() function shall get the parameters associated with the terminal referred to by fildes and store them in the termios structure referenced by termios_p. The fildes argument is an open file descriptor associated with a terminal.
  34.         opts.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // Disable canonical mode,echo,...,
  35.         opts.c_cflag |= (CLOCAL | CREAD);//Ignore modem control lines,  Enable receiver.
  36.         opts.c_cflag &= ~PARENB; // Disable parity generation on output and parity checking for input.
  37.         opts.c_cflag &= ~CSTOPB; //Set ONE stop bits, rather than TWO.
  38.         opts.c_cflag &= ~CSIZE; //Disable Character size mask
  39.         //opts.c_cflag |= CS8;    //cd character size mask size, commented out because there is no reason it is there now
  40.         opts.c_oflag &= ~OPOST; // Disable implementation-defined output processing?
  41.         opts.c_iflag &= ~INPCK; //Disable input parity checking?
  42.         opts.c_iflag &= ~(IXON | IXOFF | IXANY);// Disable XON/XOFF flow control on output AND input,and flowcontrol any char resume
  43.         opts.c_cc[VMIN] = 0; //Minimum number of characters for noncanonical read (MIN).
  44.         opts.c_cc[VTIME] = 1; //Timeout in deciseconds for noncanonical read (TIME).
  45.  
  46.         cfsetispeed(opts, B115200);//sets the input baud rate
  47.         cfsetospeed(opts, B115200);//sets the output baud rate
  48.  
  49.         tcsetattr(fd, TCSANOW, opts); // The change occurs immediately
  50.  
  51.         tcflush(fd, TCIOFLUSH); //flushes both data received but not read, and data written but not transmitted.
  52.     }
  53.  
  54.  
  55.     public void test() throws UnsupportedEncodingException {
  56.         byte[] openCommand = ("U+004Fr").getBytes("UTF-16");
  57.         byte[] rxInit1 = new byte[1];
  58.  
  59.         int n = write(fd,openCommand, openCommand.length);
  60.         if (n < 0) {
  61.             System.out.println("write() failed ");
  62.             System.exit(0);
  63.         }
  64.         FDSet rdset = newFDSet();
  65.         FD_ZERO(rdset);
  66.         FD_SET(fd, rdset);
  67.  
  68.         TimeVal tout = new TimeVal();
  69.         tout.tv_sec = 10;
  70.  
  71.         int s = select(fd + 1, rdset, null, null, tout);
  72.         if (s < 0) {
  73.             System.out.println("select() failed ");
  74.             System.exit(0);
  75.         }
  76.         int m = read(fd,rxInit1,rxInit1.length);
  77.         if (m < 0) {
  78.             System.out.println("read() failed ");
  79.             System.exit(0);
  80.         }
  81.         System.out.println(rxInit1[0]);
  82.         int ec = close(fd);
  83.  
  84.  
  85.  
  86.     }
  87. }
  88. // And here is the link to the dongle I try to read. http://www.can232.com/docs/can232_v3.pdf
  89.  
  90.  
  91. //I get 7 back which is error
captcha