Fast Read/Write To Serial Port

"Jonathan Sharret" <j.sharret-vt2K7lilQm9Wk0Htik3J/[email protected]>
Newsgroups gmane.comp.hardware.microcontrollers.tini
Message-ID <[email protected]>
Hi
 
My company just bought the DSTINIs400/DSTINIm400 kit.  I’ve set up the
TINI system running SDK 1.12.
I’m trying to run a simple loopback program on the serial port.  Using a
straight-through cable from my PC’s COM1 to TINI’s serial0 I’d like TINI
to transmit back everything it receives.  I’m using the Comm API’s
blackbox program to test out my TINI program.  I would think that this
would be a relatively simple operation and would not tax the system
(after all, max serial port speed is 115,200 bps and the system runs at
70 MHz.  However, at any baud rate above 19,200 bps there is significant
byte loss (roughly half are lost).  I would like to get this application
working at 115,200.  I get byte loss even when I increase the input
buffer size.  I’ve tried using RTS/CTS but for some reason nothing
transfers when I use that setting.  However, I don’t want to use flow
control so it’s a moot point.
 
The main problem seems to be transmitting and not receiving.  I’ve tried
every combination of putting the receiver and transmitter in the same or
separate threads to no avail.  When I just have a receiver running I
always receive the correct number of bytes.  However the second a
transmitter is added the receiver no longer receives properly.  It seems
that the process waits for the transmitter to finish before receiving
again.  This happens regardless of whether the receiver and transmitter
are in separate threads.  This doesn’t seem to make sense to me since I
would think the separate threads would prevent the write from blocking
the read and vise versa.  Using javax.comm’s read and write together at
115,200 results in the read method always returning 1 byte.  Only at
19,200 does it work properly.  I decided to try the NativeComm’s read
and write methods and although it actually grabs large chunks of data
now the receiver still drops about half the bytes.  I’m trying to read
blocks of data at a time so I’m not byte-banging.  Here’s the receiver’s
code.
 
Using NativeComm:
 
import java.io.*;
import java.util.*;
import java.lang.*;
import com.dalsemi.comm.*;
 
public class SerialReceiver implements Runnable {
  private byte[] buffer = null;
  private int avail = 0;
  private int amtReceived = 0;
  private int total = 0;
  private int portHandleRx;
  private int portHandleTx;
 
  public SerialReceiver(SerialConnection owner) {
    portHandleRx = NativeComm.open(NativeComm.PORT_SERIAL0,
NativeComm.STREAM_STDIN);
    portHandleTx = NativeComm.open(NativeComm.PORT_SERIAL0,
NativeComm.STREAM_STDOUT);
  }
 
  public void run() {
      while (true) {
        avail = NativeComm.available(portHandleRx);
 
        if (buffer == null || buffer.length < avail)
          buffer = new byte[avail];
        amtReceived = NativeComm.read(portHandleRx, buffer, 100, true);
        if (avail > 0) {
          System.out.println("amtAvail: " + avail);
        }
        if (amtReceived > 0) {
          System.out.println("amtRx: " + amtReceived);
          NativeComm.write(portHandleTx,buffer,0,amtReceived);
          total += amtReceived;
          System.out.write(buffer,0,amtReceived);
          System.out.println("Rcv: " + total);
        }
      }
   }
}
 
The javax.comm version is essentially the same.  Can someone explain to
me what I’m doing wrong here?
 
Thanks,
Jonathan


_______________________________________________
TINI mailing list
TINI-6tN4nzCoH/[email protected]
To UNSUBSCRIBE, edit your profile, or see list archives:
http://lists.dalsemi.com/mailman/listinfo/tini
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.