ok i have officially given up and i am in need of help. I have trying to read the data from my zeitronix zt-2 which is a wideband oxygen sensor controller. The zeitronix is able to be connected to the computer via serial to interface with their software. I know from internet searching that it communicates at 9600 8N1. Again from searching i found out it sends out 14 bytes continuously. the first three are 0, 1, 2 always. The arduino is connected to the zeitronix via a max232. Both hardware and software serial give me the same problem. It will read correctly the 14 bytes 3-4 times that i can observe using the serial monitor then it goes crazy. I start getting the bytes all out of order. Here is the basic code i am using.
#include <SoftwareSerial.h>
int incomingByte = 0; // for incoming serial data
SoftwareSerial mySerial(10, 11); // RX, TX
void setup() {
Serial.begin(57600); // opens serial port, sets data rate to 9600 bps
mySerial.begin(9600);
}
void loop() {
// send data only when you receive data:
while (mySerial.available()) {
// read the incoming byte:
incomingByte = mySerial.read();
// say what you got:*/
Serial.print("I received: ");
Serial.println(incomingByte , DEC);
}
}
I have tried reading it into an array that was 14 bytes in size to see if i kept getting the same number because i figured even if it is out of order it would print the same number over and over but it still gives me random numbers.
thanks for the reply PaulS. I was hoping you would chime in to smack me around a little bit. As you can tell i am not the best when it comes to programming but i do give it my best shot before i give up.
So what i take from your reply is that i can't possibly keep up with the read if i am busy outputting to the serial monitor at a baud that is so much higher than the read baud.
Is the solution to slow down the hardware serial output baud? because i tried below the 9600 and still got the same exact result.
bgct9a:
So what i take from your reply is that i can't possibly keep up with the read if i am busy outputting to the serial monitor at a baud that is so much higher than the read baud.
I think you have that back to front.
You are sending too much stuff - 14 bytes for every one received - so you would need a faster sending speed. Or just get rid of the long "I received ".
If you're receiving a steady data stream on an input stream and want to output a more verbose copy of the data an output stream, which isn't fast enough to let you output every single message you received, then I suggest you maintain a buffer of the most recent complete message on the input and update it with every message received. Separately, send the current content of the buffer to the output stream at regular intervals, at a lower frequency chosen to stay within the speed of your output connection.
bgct9a:
Robin2, I tried it with just the variable being printed and still it goes haywire.
Can you explain haywire in detail so that we might see if there is a pattern to the problem?
As @PeterH says try saving the incoming data to an array - perhaps 200 or 500 bytes - and then printing them to the Serial Monitor. While they are being printed the incoming stuff will be lost - but don't worry about that. If the full array of bytes is rubbish you will know the problem is in the receiving.
Robin2 and PeterH, you guys are right. I put the incoming data into an array and then had it print the array out. It showed no more problems. the data was repeated perfectly without any loss. Thank you all very much for the help.
I might have to revisit this thread for more help if i get stuck trying to get the data out of an array quick enough to be useful. I intend to display the data in real time on a screen and have a warning buzzer in case of an unsafe condition for the car's engine.
If the data is being read into the array properly it should also be possible to send it on to the PC as it arrives as you were originally trying to do. I think you need to study your "haywire" carefully to identify where the problem is.
Robin2:
If the data is being read into the array properly it should also be possible to send it on to the PC as it arrives as you were originally trying to do.
Not if the output stream runs out of bandwidth before the input stream does. which is the case here.
A simple swinging buffer system as I suggested lets you have an output sample frequency which is lower than the incoming sample frequency, which lets you control the output stream bandwidth.
bgct9a:
I might have to revisit this thread for more help if i get stuck trying to get the data out of an array quick enough to be useful.
This approach lets you send the output data in the format you want up to the maximum speed of the output stream. If that gives you too much latency, you need to either make the output format more compact or make the stream faster.
PeterH:
Not if the output stream runs out of bandwidth before the input stream does. which is the case here.
In this case the output baudrate is much higher than the input rate and the OP seemed to have a problem even when he was only re-transmitting the received data. I think it should be possible to get that to work.
Robin2:
In this case the output baudrate is much higher than the input rate and the OP seemed to have a problem even when he was only re-transmitting the received data. I think it should be possible to get that to work.
If that's the case then yes, I agree. I thought it wasn't the case, but I may have misunderstood.