timeshredder
Member
O.K. I have built a quick device to be able to drip feed CNC programs to my CNC machine. It has an old control and the floppy drive has broken with zero hope for repair. Enter: Teensy, since Teensy has a real UART and hardware serial, i figured it would make a great fit. For the most part it has done well, it is able to send data to the CNC and get it to start running. On the CNC, I enter "Tape mode", and just press start to start receiving a file. I have this working correctly with another computer that also has a hardware serial. The goal is to get rid of USB-Serial eventually and be able to dump programs from the SD card, replacing the floppy. So far I am able to start dumping the file to the CNC machine and it runs for a bit, but it (seems) like it gets hung up on flow control. Software flow control refuses to work, whereas, on the other test system, it doesn't seem to matter which type of flow control i set but it works. RTS/CTS is available on the CNC and I am aware that you are supposed to connect RTS on the Teensy to CTS on the CNC and Vice Versa. The first question I have is there any code that needs to be written to further implement RTS/CTS beyond the serial1.attachRts(pin) and serial1.attachCts(pin)?
Code:
/* USB to Serial - Teensy becomes a USB to Serial converter
http://dorkbotpdx.org/blog/paul/teensy_as_benito_at_57600_baud
You must select Serial from the "Tools > USB Type" menu
This example code is in the public domain.
*/
// set this to the hardware serial port you wish to use
#define HWSERIAL Serial1
unsigned long baud = 4800;
const int reset_pin = 4;
const int led_pin = 13; // 13 = Teensy 3.X & LC
// 11 = Teensy 2.0
// 6 = Teensy++ 2.0
const int rts_pin = 2;
const int cts_pin = 18;
void setup()
{
pinMode(led_pin, OUTPUT);
digitalWrite(led_pin, LOW);
digitalWrite(reset_pin, HIGH);
pinMode(reset_pin, OUTPUT);
// Next 4 lines debug Tx/Rx
//pinMode(0, OUTPUT);
//digitalWrite(0, LOW);
//pinMode(1, OUTPUT);
//pinMode(1, LOW);
// Next four lines Debug RTS/CTS
//pinMode(rts_pin, OUTPUT);
//digitalWrite(rts_pin, HIGH);
//pinMode(cts_pin, OUTPUT);
//digitalWrite(cts_pin, HIGH);
//
Serial.begin(baud); // USB, communication to PC or Mac
HWSERIAL.begin(baud, SERIAL_7E1); // communication to hardware serial
HWSERIAL.attachRts(rts_pin);
HWSERIAL.attachCts(cts_pin);
}
long led_on_time=0;
byte buffer[80];
unsigned char prev_dtr = 0;
void loop()
{
unsigned char dtr;
int rd, wr, n;
// check if any data has arrived on the USB virtual serial port
rd = Serial.available();
if (rd > 0) {
// check if the hardware serial port is ready to transmit
wr = HWSERIAL.availableForWrite();
if (wr > 0) {
// compute how much data to move, the smallest
// of rd, wr and the buffer size
if (rd > wr) rd = wr;
if (rd > 80) rd = 80;
// read data from the USB port
n = Serial.readBytes((char *)buffer, rd);
// write it to the hardware serial port
HWSERIAL.write(buffer, n);
// turn on the LED to indicate activity
digitalWrite(led_pin, HIGH);
led_on_time = millis();
}
}
// check if any data has arrived on the hardware serial port
rd = HWSERIAL.available();
if (rd > 0) {
// check if the USB virtual serial port is ready to transmit
wr = Serial.availableForWrite();
if (wr > 0) {
// compute how much data to move, the smallest
// of rd, wr and the buffer size
if (rd > wr) rd = wr;
if (rd > 80) rd = 80;
// read data from the hardware serial port
n = HWSERIAL.readBytes((char *)buffer, rd);
// write it to the USB port
Serial.write(buffer, n);
// turn on the LED to indicate activity
digitalWrite(led_pin, HIGH);
led_on_time = millis();
}
}
// check if the USB virtual serial port has raised DTR
dtr = Serial.dtr();
if (dtr && !prev_dtr) {
digitalWrite(reset_pin, LOW);
delayMicroseconds(250);
digitalWrite(reset_pin, HIGH);
}
prev_dtr = dtr;
// if the LED has been left on without more activity, turn it off
if (millis() - led_on_time > 3) {
digitalWrite(led_pin, LOW);
}
// check if the USB virtual serial wants a new baud rate
if (Serial.baud() != baud) {
baud = Serial.baud();
if (baud == 57600) {
// This ugly hack is necessary for talking
// to the arduino bootloader, which actually
// communicates at 58824 baud (+2.1% error).
// Teensyduino will configure the UART for
// the closest baud rate, which is 57143
// baud (-0.8% error). Serial communication
// can tolerate about 2.5% error, so the
// combined error is too large. Simply
// setting the baud rate to the same as
// arduino's actual baud rate works.
HWSERIAL.begin(58824);
} else {
HWSERIAL.begin(baud);
}
}
}