RS232 interface with hardware handshaking to send file to the CNC machine

Hi,

I struggling with some issues sending data/file to my CNC machine, my issue is that if I send a smaller file then it's all fine and goes well, but if I try to send a little bigger file then it's to much for the CNC machine.

So, I found some RS232 shield with handshaking but I don't know how to implement to my existing code.

Interface what I found on the internet is located here.

#include "SPI.h"
#include "SD.h"

#define CSL 4

void setup() {
  // Open serial communications and wait for port to open:
  delay(5000);
  Serial.begin(9600,SERIAL_7E2);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }




  // see if the card is present and can be initialized:
  if (!SD.begin(CSL)) {
    Serial.println();
    // don't do anything more:
    return;
  }
  Serial.println();

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dFile = SD.open("9001.NC");

  // if the file is available, write to it:
  if (dFile) {
    while (dFile.available()) {
      Serial.write(dFile.read());
    }
    dFile.close();
  }
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening 9001.NC");
  }
}

void loop() {
}

Thank you for your help and time.

So, I found some RS232 shield with handshaking but I don't know how to implement to my existing code.

Handshaking simply means that the two devices exchange some kind of message so the sender knows when the receiver is ready to receive more data.

What is the CNC machine going to send to tell the Arduino that it is ready to receive more data?

PaulS:
Handshaking simply means that the two devices exchange some kind of message so the sender knows when the receiver is ready to receive more data.

What is the CNC machine going to send to tell the Arduino that it is ready to receive more data?

I knew that you will be the first responder PaulS as always! :smiley:

The CNC machine will send the signal to Arduino to stop, he is busy, after the CNC prosessed the half of the data the CNC machine will go to Ready to Receive state.

Ok - first off, the code that shows how to use the interface is formatted a bit odd - it should read:

#define RTS 6
#define CTS 8

digitalWrite(CTS,HIGH); // Tell the remote end it's OK to transmit 

while(Serial.available()==0); // Wait for data to be available

myChar = Serial.read(); // Read a character of data

digitalWrite(CTS,LOW); // Stop the remote end transmitting while we do stuff

while(digitalRead(RTS)==HIGH); // Wait for remote end to allow us to transmit 

Serial.print("Testing 123");

So - basically you need to define which pins you want to use for the RTS and CTS lines, then put the proper code in place to allow you to "pause" (ie, tell the computer to stop sending data):

digitalWrite(CTS,LOW); // Stop the remote end transmitting while we do stuff

...until your code can "catch up" - then your code would need to tell the computer to continue transmitting data:

digitalWrite(CTS,HIGH); // Tell the remote end it's OK to transmit

Where or how you do that in your code will be up to you; as it stands, your code isn't clear enough (or it isn't complete enough) to have any clue where to put these statements. Basically, though, you're going to want to read the data for "so many bytes", pause, process the data (whatever that means for you), then tell the computer to unpause and send another chunk of bytes.

Note that if you are counting out for a specific number of bytes, you may end up with an "odd amount" at the end - so make sure while you are reading, you are also looking for whatever constitutes the "end of file" (EOF) marker as well as the count - if you hit EOF before your chunk count maximum is hit, then you know you have read the complete file/data.

cr0sh:
Ok - first off, the code that shows how to use the interface is formatted a bit odd - it should read:

#define RTS 6

#define CTS 8

digitalWrite(CTS,HIGH); // Tell the remote end it's OK to transmit

while(Serial.available()==0); // Wait for data to be available

myChar = Serial.read(); // Read a character of data

digitalWrite(CTS,LOW); // Stop the remote end transmitting while we do stuff

while(digitalRead(RTS)==HIGH); // Wait for remote end to allow us to transmit

Serial.print("Testing 123");




So - basically you need to define which pins you want to use for the RTS and CTS lines, then put the proper code in place to allow you to "pause" (ie, tell the computer to stop sending data):



digitalWrite(CTS,LOW); // Stop the remote end transmitting while we do stuff




...until your code can "catch up" - then your code would need to tell the computer to continue transmitting data:



digitalWrite(CTS,HIGH); // Tell the remote end it's OK to transmit




Where or how you do that in your code will be up to you; as it stands, your code isn't clear enough (or it isn't complete enough) to have any clue where to put these statements. Basically, though, you're going to want to read the data for "so many bytes", pause, process the data (whatever that means for you), then tell the computer to unpause and send another chunk of bytes.

Note that if you are counting out for a specific number of bytes, you may end up with an "odd amount" at the end - so make sure while you are reading, you are also looking for whatever constitutes the "end of file" (EOF) marker as well as the count - if you hit EOF before your chunk count maximum is hit, then you know you have read the complete file/data.

Yeah, you are right about that, so I need to know how many amount of data can receive the CNC buffer and I need to make chunks/data splitting, right?

A second thing, I can't figure out from that link, the schematic of the RS232 iterface, I know how to build a basic RS232 interface like this, but where are those CTS and RTS connections coming out from the interface?

beic:
Yeah, you are right about that, so I need to know how many amount of data can receive the CNC buffer and I need to make chunks/data splitting, right?

A second thing, I can't figure out from that link, the schematic of the RS232 iterface, I know how to build a basic RS232 interface like this, but where are those CTS and RTS connections coming out from the interface?

The link you gave for the RS232 interface shows: The normal Pin 0/Pin 1 for the built-in serial of the Arduino is passed through the MAX-232 chip as per normal, and the two extra channels of the MAX-232 chip are linked between the RTS and CTS connections of the DB9 connector, and two digital IO pins of your choice.

These are the 4 connections needed to talk to your CNC machine. Before sending any byte to the CNC machine, you need to be sure to set RTS to high, if it is not already high. Then before sending each byte, check CTS to be sure it is high. If LOW, wait for the CNC machine to set it high.

When you are all through sending to the CNC machine, set RTS to LOW.

Paul

When you are all through sending to the CNC machine, set RTS to LOW.

Of course, the CNC machine must be diddling with the CTS pin, so that the sender knows when it is Clear To Send.

PaulS:
Of course, the CNC machine must be diddling with the CTS pin, so that the sender knows when it is Clear To Send.

Yup.

Paul