Hello,
I am attempting to read data from the serial port of a Sparling TigerMag Flow meter. I am able to do this via an Allen Bradley PLC, but I am wanting to use an UNO to serial read the flow meter in order to save me from having to waste a PLC serial port on the flow meter. Eventually, I will be retrieving this data via an Ethernet shield and Modbus.
I am using an UNO with a so-mystech rs232 to ttl convertor.
I am using the below simplified sketch.
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); // RX, TX
int burst=0x13;
int available_data = mySerial.available();
void setup() {
Serial.begin(9600);
mySerial.begin(1200);
mySerial.println(burst);
}
void loop() {
if (mySerial.isListening()) {
Serial.println("mySerial is listening!"); }
Serial.println(available_data);
String input = mySerial.readString();//read string
Serial.println(input);
int input1 = mySerial.listen();//read string
Serial.println(input1);
delay(500);
}
I have to send the Sparling a hex13 in order to put it into burst mode. In this mode it streams its data constantly. The sketch does not work until I lift the VCC from the 232 convertor and replace it. Then and only then do I read the correct (or any)data. It seems the longer I leave VCC disconnected, the larger data file that i get after I reconnect. It seems like it is filling the buffer on the convertor chip.
The Sparling data port is configured at 1200, 8, ODD, 1. It is unchangeable. Data received still looks valid when I receive it. Sketch returns that port is listening and returns a 0 for available data.
Thanks in advance for looking.
Bob
BOB_324:
Sketch returns that port is listening and returns a 0 for available data.
available() doesn't work that way, you have to call it each time, not just once when you initialize available_data.
BOB_324:
int input1 = mySerial.listen();//read string
[/code]
I'm not sure what you're trying to do here by you're definitely not reading a string. The reference page for listen() says it returns "None" but looking at the source code it turns out this is incorrect, it actually returns a bool:
// This function sets the current object as the "listening"
// one and returns true if it replaces another
Actually, I have tried the sample sketches with no luck. The above lines were added , in order to determine if the data from the device was getting to to port.
The simplified version of the sketch is here.
As I said in the original post, I can get it to read data if I remove VCC from the 232 to ttl converter. Upon restoring the VCC, I get the data burst exactly as expected. I don't know if it is a hardware issue or code.
Delay was added so I could see it better. Works fine attached to using a computer running hyperterminal or putty to input data.
Thanks for the responses on such a simple idea.
Bob
"so-mystech rs232 to ttl convertor." doesn't show any useful-looking results on Google. Can you send us an actual link? It may be important if it's a cheaper type converter.
mySerial.readString() has no termination condition. How does it know when the string has finished? It will probably wait for the default timeout of 1 second and then you get "whateverwhatev" was sent.
Then even if you got nothing, you try to print that. You should always check you got something.
I couldn't really find much on it either other than it uses the MAX232CSE chip. The data I am trying to receive is constant stream, unless I turn it off. It will stream 80 characters (2 line 40 column display). I have done this very successfully with various PLC's. Since serial ports are going away, I needed another way to cheaply grab serial data from these flow meters. I can turn the data on with a (Control S) and off with a (Control U). I have been playing it today and it seems as if the data is getting "stuck" in a buffer. As I said before I can remove VCC from the 232 converter and then when I re apply the VCC the data shows up on the Serial Monitor. I have another brand of converter and it does the same thing, except it display data when VCC is removed, not when it is applied.
The reason I think it is a buffer issue is I can remove the VCC, wait for a few seconds, unplug my 232 connection and then power back up and it puts data on the serial monitor, without the serial line connected.
Using a computer as the data source and it works flawlessly. In fact, I tested it with some parsing and it worked as expected, only when I hooked it up to my real data source, did I have problems.
Maybe this won't work, I think it should, but who knows. I appreciate all of the advice as I am new to this platform, but write PLC and SCADA programs for a living. It's all the same, just different.
Thanks,
Bob
BOB_324:
I couldn't really find much on it either other than it uses the MAX232CSE chip.
Good. Should be no problems.
Since serial ports are going away,
Not in the next 20 years.
As I said before I can remove VCC from the 232 converter and then when I re apply the VCC the data shows up on the Serial Monitor. I have another brand of converter and it does the same thing, except it display data when VCC is removed, not when it is applied.
The reason I think it is a buffer issue is I can remove the VCC, wait for a few seconds, unplug my 232 connection and then power back up and it puts data on the serial monitor, without the serial line connected.
There's no buffer in a MAX232. That can't be the problem.
But, the sending device may be using hardware flow control. It needs you to hold CTS "Clear To Send" high (or low) in order to permit data to send. Check the status of these lines on the device. Often you find MAX232 variant chips have more than 2 converters. You can use them for multiple serial devices or you use the additional converters to send RTS and CTS signals. If CTS is connected on the TTL-voltage side through the MAX232, you can probably just hardwire it to +5V or 0V as appropriate. If it's not, then find the supply capacitors adjacent to the MAX232 which will have voltages greater than +/-6V. Jumper one of them to CTS.
Have you seen a desktop machine that had a true serial port lately?
I'm actually still using one (but not the serial port anymore, though I did have an IR receiver on it until recently). I think it was made at the very end of the serial port era. It's still sufficient for my needs as the computer in my workshop but has been unreliable for the last couple years due to some bad caps on the motherboard. I'm torn between trying to fix it or just using it until it dies.
Not to argue that serial ports on computers haven't gone away.
pert:
I'm actually still using one (but not the serial port anymore, though I did have an IR receiver on it until recently). I think it was made at the very end of the serial port era. It's still sufficient for my needs as the computer in my workshop but has been unreliable for the last couple years due to some bad caps on the motherboard. I'm torn between trying to fix it or just using it until it dies.
Not to argue that serial ports on computers haven't gone away.
I was going to add something about also not being covered by a layer of dust and dirt that no amount of cleaning will remove.