Pages: [1]   Go Down
Author Topic: Confirming transmission using hardware and software serial on the same board  (Read 698 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

 I've been working on controlling a pH meter across RS-232 and I've tried many different things to get the set up to work, but nothing has succeeded.  I'm at the point where I just want to confirm that my softwareserial port is actually able to send and receive data as I've been able to confirm that the pH meter behaves as expected.

I am using a Leonardo board and I want the hardware serial to connect to the pc so I can record data, so the softwareserial will communicate with the pH meter.  To make sure the softwareserial connection is working, I've been trying to make a simple loop where the hardware serial sends a short string to the RX pin of the software serial.  When there are characters available an LED should light up.

I haven't been able to get pHmeter to show any signs that it's sending or receiving input.  pHmeter.available() is always zero. Writing from pHmeter never produces characters in the hardwareserial buffer either.

Is there a simple way to make sure that these serial connections are actually sending characters?

Code:
#include <SoftwareSerial.h>

int RXpin = 9;
int TXpin = 8;
int debugPin = 13;
SoftwareSerial pHmeter(RXpin,TXpin); //8 rx, 9 tx

String start_code = "S00";
String inputString = "";
boolean stringComplete = false;
char incomingByte = 0;   // for incoming serial data
int avail = -1;
void setup() {
      // pinMode(TXpin, OUTPUT);
      // pinMode(RXpin, INPUT);
       pinMode(debugPin, OUTPUT);
        Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps
        Serial.println("Attempting Contact");
        //while (!Serial){
        //  ;//wait for Leo to connect
        //}
        Serial.println("Connected to Leo");
        inputString.reserve(200);
       
        pHmeter.begin(9600);
        pHmeter.listen();
        Blink();
}

void loop() {
    Serial.println("Loop start");
    delay(20);
    pHmeter.print("S00");
    delay(20);
    pHmeter.println("S00");
    delay(20);
//  Serial.println(Serial.read());
  if (pHmeter.available()) {
    digitalWrite(debugPin, HIGH);
    delay(100);
    Serial.println("pHmeter is available");
    while (pHmeter.available() > 0) {
      char inChar = Serial.read();
      inputString += inChar;
    }
   
    digitalWrite(debugPin, LOW);
    delay(500);
    Serial.println(inputString);
    inputString = "";
  }
  if (stringComplete) {
        Serial.println(inputString);
        inputString = "";
        stringComplete = false;     
             }
        //delay(500);
}

void establishContact() {
  while (pHmeter.available() <= 0) {
    Serial.print(start_code);
    delay(300);
  }
}// establish contact


void serialEvent() {
  while (pHmeter.available()){
    char inChar = (char)Serial.read();
    inputString += inChar;
    if (inChar =='\n') {
      stringComplete = true;
    }
  }
}// serial event
 


void Blink(){
  digitalWrite(debugPin, HIGH);
  delay(200);
  digitalWrite(debugPin,LOW);
  delay(200);
  digitalWrite(debugPin, HIGH);
  delay(200);
  digitalWrite(debugPin,LOW);
  delay(200);
}// blink
Logged

Rome, Italy
Offline Offline
Sr. Member
****
Karma: 20
Posts: 442
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Is mySerial.available() returns 0 there are no incoming data.

There are other ways to detect some activity on the receive pin. One is to define a pin change interrupt: when the pin receives data it will change its logical state from HIGH to LOW (and/or the other way round) causing the interrupt service routine to be called (then you can blink the LED). I'm sorry I'm unprepared to provide details for the Leonardo at the moment, but on a Uno you would
Code:
  bitSet(PCICR, PCIE0);
  bitSet(PCMSK0, PCINT0);

// the ISR
ISR(PCINT0_vect) { // your code here }
This is based on pin 8 being associated to pin change interrupt 0 (on the Uno). You may also find a way to use attachInterrupt() to achieve the same.

I believe however that this is not going to give you more information, and that the problem is at the physical level. From your description it seems that the pH meter communicates at RS-232 signal levels, which cannot (and *must* not) be fed directly to an Arduino pin. The voltage can reach 25V and also be negative, and would certainly damage the arduino. If this is your case you should use a level converter. Please provide more information on the pH meter and on how it is connected (including TX, RX, VCC, GND).
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17262
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I recently read about a PH sensor module that used serial interface and it required all commands to be sent with adding just a ending carriage return character, if you sent it ending with CR and LF characters then it wouldn't respond to the request. The arduino serial println function ends with both CR and LF being sent.

 So do you have a clear description of the serial protocol your sensor is expecting?

Lefty
Logged

texas
Offline Offline
God Member
*****
Karma: 27
Posts: 862
old, but not dead
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Serialevent is only called for the hardware serial port.  You are in there waiting for the softwareserial (phmeter) port to have data available, then you read the hardware port.  Is that right?  Have you tried AltSoftSerial?
Logged

Experience, it's what you get when you were expecting something else.

Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Spatula:  I should have been more clear, I am using a 232Max to convert RS-232 to TTL.  That said, it is certainly possible that the converter isn't doing a good enough job.  Connections go GND->GND, VCC->5volt, meter RX -> leo TX, meter TX -> leo RX. 

The pH meter is a JENCO 6230N.  To request data you send the ASCII string "S00" (s-null-null) and it immediately responds with the state of the machine. The meter expects 9600,N,8,1 RS-232.  No handshake. I've connected the meter to a port monitor and have been able to query the meter successfully that way. 


Retrolefty:  I am forgetting if this meter is similarly sensitive to CR vs. LF vs CR+LF.  I believe it is based on playing with the port monitor but I can confirm (it lets you specify line ending characters).  I tried print and println in my code, though.   print with escape characters should give me the ability to control the end of line characters? (\n and \r). I've tried print with \n, \r, \r\n, and \n\r at the end with no luck.



Afremont:  The code I posted is basically a mashup of every serial com example I could find.  The data I care about will come through the pHmeter serial.  The hardware serial will be used for recording what comes from the meter.  So it sounds like I can just ditch serial event.  I will look at AltSoftSerial.


A dumb question here, but if I plug the hardware TX into the software RX (digital pin 1 into digital pin 9) and do

Code:
Serial.print("Test");
if (pHmeter.available()){
     digitalWrite(debugpin,HIGH);
     delay(500);
     digitalWrite(debugpin,LOW);
}

Should I not get some response?  Because even this does not work on my board.

Thanks for the feedback so far!
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

AltSoftSerial seems to be limited to certain boards.  Unfortunately, Leonardo isn't supported (though the same processor is used on other boards that are supported, so there's probably a way to make it work).  Otherwise it sounds promising.  My googling didn't find a solution for adding Leonardo as one of the known boards, but maybe someone has a link?
Logged

texas
Offline Offline
God Member
*****
Karma: 27
Posts: 862
old, but not dead
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You might want to e-mail the author of AltSoftSerial as he is a regular poster.  It shouldn't be difficult to make it work on the Leonardo.
Logged

Experience, it's what you get when you were expecting something else.

Rome, Italy
Offline Offline
Sr. Member
****
Karma: 20
Posts: 442
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

A dumb question here, but if I plug the hardware TX into the software RX (digital pin 1 into digital pin 9) and do

Code:
Serial.print("Test");
if (pHmeter.available()){
     digitalWrite(debugpin,HIGH);
     delay(500);
     digitalWrite(debugpin,LOW);
}

Should I not get some response?  Because even this does not work on my board.

Thanks for the feedback so far!

Regarding your "dumb question", I think it has something to do with the fact that on Leonardo the TX and RX pins are separate from the USB channel (addressed via Serial), but I may have misunderstood your test.

I was going to ask a dumb question myself: "since you have a Leonardo, why don't you try using the TX/RX pins to communicate with the device?", but now I think the issue deserves some further elaboration. On Leonardo you can use Serial1 as an instance of HardwareSerial, using the TX/RX pins. You get the user interface on Serial (USB) and another channel on Serial1, which in your case may come in really handy to exclude issues due to SoftwareSerial. You can be reasonably sure that if you cannot communicate using Serial1 and RX/TX (pins 0,1) the problem is elsewhere.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ah, that is very helpful.  I didn't realize that Serial is the USB and Serial1 is the actual pins, nor did I know that you could use the hardware for both independently!  Sure enough, this is documented on the Leonardo page.  I thought I read every word of that page a dozen times, but I clearly missed it.  How embarrassing.
Logged

Pages: [1]   Go Up
Jump to: