Serial communication

For Short Term Learning Discussions ONLY. This area is for CURSORY questions and connecting with other users ONLY. ALL technical contributions need to be made in the appropriate forums and NOT HERE. All posts are temporary and will be deleted within weeks or months. You should have already search the extensive FAQs in each of the forums before posting here as your question may already be answered.
Pablo Llaguno
Posts: 50
Joined: Sun Feb 05, 2017 11:00 pm
Real name: Pablo Llaguno

Serial communication

Post by Pablo Llaguno » Thu Nov 16, 2017 4:11 am

Hello,

I am having issues building an Arduino module that can help me read what a pressure transducer outputs. I have an RS232 to TTL converter that is going to convert the data between the Arduino and the transducer, it looks like this:

Image

The transducer has a D-sub15 connection, I made a cable for it so it has an D-sub9 end, the diagram looks like this (the cable looks terrible! But it works):
Image

I have tested that the cable works by using an RS232 to USB cable, and using a serial terminal and sending the command to the transducer, and effectively receiving the ambient pressure (the command is: @253PR3?;FF).

However having the transducer connected to my computer is not efficient, so I want an Arduino to keep sending the string @253PR3?;FF while having the result in an LCD. As of now, I am just trying to have the Arduino send @253PR3?;FF through the TX pin and receiving data through the RX pin, then printing it in the serial monitor, obviously the TX, RX, VCC (5 or 3.3 V) and GND pins of the Arduino are connected to the max3232 converter.

The whole thing looks like this:
Image

The code:

Code: Select all

String sensorValue;

void setup() {
Serial.begin(9600);
}

void Pressure() {

Serial.print ("@253PR3?;FF");
sensorValue = Serial.readString();
//sensorValue = sensorValue.remove(0, 7); //Go from @253ACK7.10E+2;FF to 7.10E+2;FF
//sensorValue = sensorValue.remove(7); //To get 7.39E+2
Serial.println (sensorValue);
}
void loop() {
 //
Pressure(); //loop de sensorValue

}
When I open the serial monitor I only get @253PR3?;FF instead of the desired 7.10E+2, the Arduino TX led does blink, but the RX does not. I checked with a voltmeter and the max3232 seems to work fine. Could this be a problem with having a ground between the transducer and dsub9 head, and then having the Arduino ground? Or perhaps I can't print the result on the serial monitor?

Please help with anything you could see that could be the source of the problem,

Thanks

John Myers
Posts: 41
Joined: Wed Mar 08, 2017 12:13 am
Real name:
Location: SoCal

Re: Serial communication

Post by John Myers » Thu Nov 16, 2017 6:10 am

So, I'm not that familiar with Arduino but when you say serial monitor that's through the USB correct?
Your code looks like its trying to write the value back to the sensor instead of a different serial port.

Jeroen Vriesman
Posts: 280
Joined: Mon Feb 07, 2011 9:08 pm
Real name: Jeroen Vriesman
Location: Netherlands
Contact:

Re: Serial communication

Post by Jeroen Vriesman » Thu Nov 16, 2017 12:56 pm

Yes, your code seems to write the output back to the sensor...

but besides that, your rx led doesn't blink. So maybe you don't have any response at all from the sensor.

could be that:
a) parity or stop bits are different on the max chip compared to the usb-serial test you describe?
b) 7 or 8 bits?
c) from the diagram, you seem to connect chassis of the sensor to gnd of the max, is the chassis of the sensor the signal ground?

Pablo Llaguno
Posts: 50
Joined: Sun Feb 05, 2017 11:00 pm
Real name: Pablo Llaguno

Re: Serial communication

Post by Pablo Llaguno » Thu Nov 16, 2017 5:13 pm

The serial monitor is the serial terminal inside the Arduino IDE.
Your code looks like its trying to write the value back to the sensor instead of a different serial port.
Thanks, that could be the problem, maybe that is why the RX led doesn't blink because the data is just recirculating to the transducer. I've also been told that the serial hardware of the Arduino can be used for just one device, so I will try printing in an LDC and see if that works.

The Arduino is set to 9600 bauds, the same as the transducer. IIRC it is 8 bits.
c) The diagram is the one suggested in the MKS901p manual, the cable does work using a dsub9 RS232 to USB adapter. The sensor has its own power source that is grounded to the sensor and to the dsub9.

Pablo Llaguno
Posts: 50
Joined: Sun Feb 05, 2017 11:00 pm
Real name: Pablo Llaguno

Re: Serial communication

Post by Pablo Llaguno » Sat Jan 13, 2018 10:25 pm

Sorry for the long time to repost. But I've not had success and now I am thinking my problem could be with the RS232 TTL adapter, though when I check with a voltmeter it is sending some data. I changed the overall setup and instead did this Image
*The MAX232 chip and d-sub9 might be wired incorrectly in the diagram, but the real hardware is sold as one piece (so it must be wired correctly).

That is the overall setup now, with an LCD display and the code being as straightforward as it can:

Code: Select all

// include the library code:
#include <LiquidCrystal.h>  
 LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
 
String sensorValue;

void setup() {
Serial.begin(9600);
 //Contrast
 analogWrite(6,100);
  // set up the LCD's number of columns and rows:
 lcd.begin(16, 2);
}

void Pressure() {
lcd.setCursor(0, 0);
lcd.print("Pressure:"); //text

Serial.println ("@253PR3?;FF");
sensorValue = Serial.readString();
sensorValue.remove(0, 7); //Remove first 7 characters of @253ACK7.10E+2;FF to get 7.10E+2;FF
sensorValue.remove(7); //Remove characters from the 7th, to get 7.10E+2

lcd.setCursor(0,1);
lcd.print(sensorValue);
lcd.print(" Torr"); 
}

void loop() {
 //
Pressure(); //loop de sensorValue
}
And as before I don't get any value on the LCD display. I have already tested the gauge and it works using an RS232 to USB cable through a Serial Terminal, and before I used the analog reading and it worked fine (though not as accurate).

Should I replace the TTL adapter or am I missing something?

User avatar
Richard Hull
Site Admin
Posts: 10581
Joined: Fri Jun 15, 2001 1:44 pm
Real name: Richard Hull

Re: Serial communication

Post by Richard Hull » Sun Jan 14, 2018 12:39 am

I would go to the Arduino home page and there is a very adroit crowd there in the troubleshooting forum, just itchin' to help if you define your problem well.

I have never tried to send-receive out of the serial port on the arduino I tend to go for I2C communication (two wire interface). I do use the serial monitor a lot, but that is all ready to rock and roll through the USB connection. At 71 I am a bit old to go looking for the better before first having secured the good. I have worked with the arduino since 2014 and am still learning as the immediate need arises. I have found many solutions on the Arduino home site forum board that were already asked and answered. The vast arduino libraries found on GitHub and the arduino site save a lot of time and heartburn It is a very rich and vibrant community of folks in the Arduino world, tap into their knowledge base.

Richard Hull
Progress may have been a good thing once, but it just went on too long. - Yogi Berra
Fusion is the energy of the future....and it always will be
Retired now...Doing only what I want and not what I should...every day is a saturday.

Jerry Biehler
Posts: 759
Joined: Tue Nov 24, 2009 8:08 am
Real name:
Location: Beaverton, OR

Re: Serial communication

Post by Jerry Biehler » Sun Jan 14, 2018 6:52 am

readstring may be timing out before it gets a response. Use Serial.available() to check if there is anything in the serial buffer before trying to read it out. Then do an if statement to read it in. You also might want to be using a Serial.write instead too.

This is an old program I have for running a luxim lamp I have, it is controlled via serial (in this case uart)

Code: Select all


#include <Encoder.h>

#include <LiquidCrystal.h>

HardwareSerial Uart = HardwareSerial();
Encoder brtenc(0, 1);
int inPin = 45;         // the number of the input pin
int outPin = 44;       // the number of the output pin
int lampstate = 2;  //lamp off at startup
int state = HIGH;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin
long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers
int lastlampstate = 2; // Lamp off at startup
long encoderPos = 128;
long lastReportedPos = 128;
int inByte = 0;     
int statusByte = 0; // incoming status serial byte
int laststatusByte = 0;
int lampretstat;
int lastlampretstat;
float lampbright;
float lastlampbright;
float lastlampbrightoff;
float lampbrightoff;
boolean A_set = false;
boolean B_set = false;
LiquidCrystal lcd(4, 5, 6, 7, 8, 9);
int stat = 38;
unsigned long time2 = millis(); 
unsigned long time3 = millis(); 
char inData[30]; 

void setup() {



  Serial.begin(19200);
  Uart.begin(19200);
  UCSR1C |= (1 << UPM11);
  pinMode(inPin, INPUT);
  pinMode(outPin, OUTPUT);
  pinMode(stat, INPUT);
  digitalWrite(stat, HIGH);
  digitalWrite(inPin, HIGH);
  lcd.begin(24, 2);
  lcd.setCursor(0, 0);
  lcd.print("Normal: Ready");
  //lcd.setCursor(23,0);
  //lcd.print("%");
  lcd.setCursor(17,0);
  lcd.print("100.00%");
  brtenc.write(128);


}


void loop(){ 
  reading = digitalRead(inPin);
  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    if (state == HIGH)
      state = LOW;
    else
      state = HIGH;
    time = millis();    
  }
  digitalWrite(outPin, state);
  if (state == HIGH) lampstate = 0x01;
  if (state == LOW) lampstate = 0x02;
  if (lastlampstate != lampstate) {
    Uart.write(lampstate);
    lastlampstate = lampstate;
  }
  previous = reading;
  long encoderPos = brtenc.read();
  if (lastReportedPos != encoderPos) {
    if (encoderPos >= 255) {
      encoderPos = 255; 
      brtenc.write(255);
    }
    if (encoderPos <= 128) {
      encoderPos = 128;
      brtenc.write(128);
    }
    Uart.write(encoderPos);
    lastReportedPos = encoderPos;
  }
  delay(10);
  if (Uart.available() > 0) {
    inByte = Uart.read();
    if (inByte == 1) lampretstat = 1;
    if (inByte == 2) lampretstat = 0;
    if (inByte != 1 && inByte != 2) {
      lampbright = inByte;
      lampbrightoff = lampbright-128.000;
      lampbrightoff = lampbrightoff*.625;
      lampbrightoff = lampbrightoff-100;
      lampbrightoff = abs(lampbrightoff);
    }
  }
  {
    if (lastlampbright != lampbrightoff) {
      if (lampbrightoff == 100) {
        lcd.setCursor(17,0);
      }
      else {
        lcd.setCursor(17,0);
        lcd.print(" ")
          ;
      }
      lcd.print(lampbrightoff);
      lcd.print("%");
      lastlampbright = lampbrightoff;
    }
  }
  if (millis() - time2 > 100)
  {
    Uart.write(0x21);
    time2 = millis();

    delay(10);
    if (Uart.available() > 0) {
      statusByte = Uart.read();

      if (laststatusByte != statusByte){
        lcd.setCursor(0,0);
        if (statusByte == 0x00) lcd.print("Normal: Ready");
        if (statusByte == 0x01) lcd.print("Alarm: Start Failure    ");
        if (statusByte == 0x02) lcd.print("Alarm: Over Temp        ");
        if (statusByte == 0x04) lcd.print("Alarm: Run Failure      ");
        if (statusByte == 0x08) lcd.print("Warning: General Failure");
        if (statusByte == 0x10) lcd.print("Warning: Too Warm");
        if (statusByte == 0x20) lcd.print("Warning:     ");
        if (statusByte == 0x40) lcd.print("Normal: Start");
        if (statusByte == 0x80) lcd.print("Normal: Start");
        if (statusByte == 0xC0) {
          lcd.print("Normal: Run  ");
          Uart.write(encoderPos);
        }
        laststatusByte = statusByte;
      }
    }
  }


//delay(50);
  if (millis() - time3 > 500) {
    Uart.write("[M4D]");
    time3 = millis();
    delay(30); 
  }
  {
    while (Uart.available() > 0) {
      for (int i = 0; i < 29; i++) {
        inData[i] = Uart.read();
          Serial.println(inData);
      }


    }


  }


}






































Bruce Meagher
Posts: 84
Joined: Fri May 13, 2011 3:25 am
Real name: Bruce Meagher
Location: San Diego

Re: Serial communication

Post by Bruce Meagher » Sun Jan 14, 2018 3:11 pm

As Jerry mentions your Arduino serial read is most likely timing out (default is 1 second), and that’s why you are not displaying a pressure value on the LCD. There's nothing wrong with your code that's causing a problem, but a simple check if the string is of zero length and displaying an error would be a good practice.

It appears you have wired the DB-15 connector incorrectly. Pin 2 on the DB-9 should go to pin 2 on the DB-15, and pin 3 should go to pin 1 on the DB-15. This would be the first thing to check. Then, is the green light illuminated on the MKS-901p? Finally, is the ground of the power supply you’re using to power the 901p also connected to the Arduino’s ground?

Bruce

Pablo Llaguno
Posts: 50
Joined: Sun Feb 05, 2017 11:00 pm
Real name: Pablo Llaguno

Re: Serial communication

Post by Pablo Llaguno » Sun Jan 14, 2018 7:19 pm

Richard, indeed I've made a post in the Arduino forum but since people have already made similar Arduinos here I am actually getting richer information here. But thanks for the suggestion, and hey even though you have little time with Arduino you've done great with your projects as I've seen!

Jerry thanks for the tip, I will be sure to try Serial.available() and Serial.write in the code. For those interested in how it works see: https://www.arduino.cc/en/Serial/Available

Bruce the diagram is the one given by MKS and I'm sure it is wired correctly since I use that cable coupled with the DB-9 to USB and it works on my computer through a serial terminal. Also yes the green light is illuminated.

Lastly, no, the ground of the power supply isn't connected to the Arduino's ground, could this affect readings?

Jerry Biehler
Posts: 759
Joined: Tue Nov 24, 2009 8:08 am
Real name:
Location: Beaverton, OR

Re: Serial communication

Post by Jerry Biehler » Mon Jan 15, 2018 2:55 am

The power supply and the arduino are probably connected together through the ground pin for the serial. Unless it is internally isolated, which I really doubt.

Post Reply