I'm reading a rs232 input and outputting it , reading on tx1 and sending on rx1 receiving on rx0 and sending to hyper terminal on tx0
I'm getting information to hyper terminal but its just numbers for example :
54139175971051731391911911591591591919561229235
the code I'm using to read the data and send is :
void setup() {
Serial2.begin(9600);
Serial1.begin(9600);
}
void loop() {
// read from port 2, send to port 1:
if (Serial2.available()) {
Serial1.println((String)Serial2.read()) ;
}
}
why would it be numbers and not the formatted string I'm sending it , any ideas how i would get the formatted string outputted i then want to store the formatted string in a String class variable to do something else with the String before i send it back out again
As ascii values are often between 0..127 the line below has statistically the best chance
54 13 91 75 97 10 51 73 13 91 91 19 115 91 59 15 91 91 95 61 22 92 35
"6 \n [ K a \r 3 i \n [ [ ?? s [ ; ?? [ [ _ = ?? \ #"
looks like random keyboard strokes ...
BUt as said above, we don't know what you send to the input....
void setup()
{
Serial2.begin(9600);
Serial1.begin(9600);
}
void loop()
{
// read from port 2, send to port 1:
if (Serial2.available())
{
char c = Serial2.read();
Serial1.println(c) ;
}
}
String s;
void setup()
{
Serial2.begin(9600);
Serial1.begin(9600);
}
void loop()
{
// read from port 2, send to port 1:
if (Serial2.available())
{
char c = Serial2.read();
s += c;
Serial1.println(s.toCharArray()) ;
}
}
In function 'void loop()':
Converter:22: error: no matching function for call to 'String::toCharArray()'
/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/WString.h:80: note: candidates are: void String::toCharArray(char*, unsigned int)
Port 2 is connected to a instrument that send out a rs232 data stream, if i connect the instrument to hyper terminal i get <01> :TEST:MORETEST for example , instrument is setup as 9600 , 8 , 1 ,none same as hyper terminal and can see <01> :TEST:MORETEST printed to the terminal
if i send a String from the mega 2560 again using default 9600 , 8 , 1 , (from port 1 )i can see the string in hyper terminal that i assigned it to send.
but as soon as i connect the instrument to the mega 2560 on Serial2 and connect hyper terminal to Serial1 (via ttl converter) i just get a bunch of numbers i.e 54139175971051731391911911591591591919561229235
Help very much appreciated been on it now most the day ... driven me crazy...
You can try the below code with the serial monitor (the normal serial monitor setup is different than hyperterminal).
// zoomkat 8-6-10 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later
String readString;
void setup() {
Serial.begin(9600);
Serial.println("serial test 0021"); // so I can keep track of what is loaded
}
void loop() {
while (Serial.available()) {
delay(1);
if (Serial.available() >0) {
char c = Serial.read();
readString += c;}
}
if (readString.length() >0) {
Serial.println(readString);
readString="";
}
}
String readString;
void setup() {
Serial.begin(9600);
Serial1.begin(9600);
Serial.println("serial test 0021"); // so I can keep track of what is loaded
}
void loop() {
while (Serial1.available()) {
delay(1);
if (Serial1.available() >0) {
char c = Serial1.read();
readString += c;}
}
if (readString.length() >0) {
Serial.println(readString);
readString="";
}
}
100% failed char's.... have you checked baudrate at both sides? cabling?
BTW code looks OK but please use CRTL-T to auto-indent your code ... makes it easier to find matching brackets
void loop()
{
while (Serial1.available())
{
delay(1);
//if (Serial1.available() >0) // <<<<< not needed as the while did test the availability allready
// {
char c = Serial1.read();
readString += c;
//}
}
if (readString.length() >0)
{
Serial.println(readString);
readString="";
}
}
Thank you you was correct it was my connection into the Arduino , i was connecting direct to Arduino i now have it connecting through a MAX232 is before it enters Arduino on Serial3
i now have a new issue , the code I'm using is
SoftwareSerial mySerialOut(4,5);//rx - tx
String readString;
void setup()
{
Serial3.begin(9600);
mySerialOut.begin(9600);
}
void loop() {
if (Serial3.available() >0)
{
while (Serial3.available()) // when the port as data get it until it as none
{
char c = Serial3.read();
readString += c;
}
String dataString = readString ;//assing the sting to process
readString = "" ;// we are now done with readString array destroy
the string from the rs232 is held in dataString when i output this string it sometimes misses the first character for example if my input is
Serial communications doesn't necessarily happen at the same time and rate you expect it to. Remember that the entire transmission is in characters, not in words or messages.
If your other device outputs a newline after each command, you could do something like this:
void loop() {
while ( Serial3.available() ) {
char c = Serial3.read();
if ( c == '<' ) readString = ""; // clear if new character
readString += c;
if ( c== '\n' ) dataString = readString; // only set dataString if newline.
}
}
while (Serial1.available())
{
delay(1);
//if (Serial1.available() >0) // <<<<< not needed as the while did test the availability allready
// {
Won't do any good, robtillaart. It's been pointed out many times that the delay() and if test are not needed, but zoomkat keeps posting the same poor code over and over.
Won't do any good, robtillaart. It's been pointed out many times that the delay() and if test are not needed, but zoomkat keeps posting the same poor code over and over
PaulS, it is not perfect code and can use improvements. At least robtillaart has posted the improvements. That being said, I have never seen PaulS post any complete working code. PaulS over and over tells new users to use end of packet delimiters, but in my time in this forum I've never seen him post an actual complete working example. Somebody find an example where has. My feeling is you can't. I'm waiting.