Navin Reddy
Navin Reddy

Reputation: 51

Can DTR/RTS on RS232 be used as GPIO, to turn on/off a sensor? Does the protocol allow this kind of configuration?

I want to use these lines to turn on/off a sensor(5V Logic level). Conversion from 12V to 5V will be done, but my doubt is regarding the protocol. Does the UART/RS232 protocol allow data transfer when these lines are controlled through a program? I am going to use python serial to achieve this.

Upvotes: 2

Views: 2819

Answers (2)

Navin Reddy
Navin Reddy

Reputation: 51

I tested USB RS232 breakout board with FTDI chip, and it works perfectly fine! Comments in the code were used to test if the communication was possible during the change in state of RTS. I used an Arduino MEGA to verify this, and Full-duplex communication is possible.

import serial
import time
import sys

#rtccts should be set to False! Otherwise the output(Tx) stops after certain
#count(In this program after 86 print statements)

serPort = serial.Serial('/dev/ttyUSB0', 115200, timeout=1, rtscts=False, dsrdtr=False)

num = 0;
while True:
    try:
        #serPort.write((str(num)+"\n"))
        #print("Sent->" + str(num))
        #serPort.flushOutput()
        num = num + 1

        serPort.rts = 1
        #serPort.dtr = 1
        #print(serPort.readline())
        #serPort.flushInput()
        time.sleep(0.05)

        serPort.rts = 0
        #serPort.dtr = 0
        #print(serPort.readline())
        #serPort.flushInput()
        time.sleep(0.05)

    except KeyboardInterrupt:
        break

serPort.close()
sys.exit()

This is the code on Arduino:

int val = 0;
int inPin = 7;
int ledPin = 13;
char incomingByte = 0;

void setup()
{
    Serial.begin(115200);
    pinMode(inPin, INPUT);
    pinMode(ledPin, OUTPUT);
}

void loop()
{
    static unsigned int num = 0;
    //  Serial.println(num);
    //  num++;
    delay(10);

    //  if (Serial.available() > 0)
    //  {
    // read the incoming byte:
    //    incomingByte = Serial.read();

    //    Serial.print("I received: ");
    //    Serial.println(incomingByte);
    //  }

    val = digitalRead(inPin);
    if(val == 0)
    {
        //Serial.println("LOW");
        digitalWrite(ledPin, 0);
    }
    else if(val == 1)
    {
        //Serial.println("HIGH");
        digitalWrite(ledPin, 1);
    }
}

Upvotes: 2

kunif
kunif

Reputation: 4360

It will be possible if your sensor device does not require those signals and there is no requirement for time accuracy of the on/off of the rts/dtr signal line, or if the timing condition is loose.

In PySerial's Serial class __init__, flow control by signal line is disabled by default unless you specify it.
pySerial API Classes Native ports class serial.Serial

As it is, you can set rts and dtr to the desired values.

Upvotes: 1

Related Questions