Pages: [1] 2   Go Down
Author Topic: What is the serial monitor actually doing?  (Read 2064 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 30
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi guys, I am working on a project with Serial. I am sending 2 commands to the slave device, but only the first command appears to be read. The second command executes, albeit appears to execute incorrectly, only when I open the serial monitor. It's kind of doing what I want.....but not quite. Any suggestions would be helpful.

I'm not even sure the first command is quite right. It goes kinda slow, but idk. Maybe that command is supposed to be slower. It's hard to say.


Code:
uint8_t arr[]={0x42, 0x10, 0xcc};
uint8_t new_arr[]={0x43,0x50,0x3F,0x22,0xFF,0xFF};
void setup()
{

  Serial.begin(9600);
  pinMode(1,OUTPUT);
  digitalWrite(1, HIGH); //set high as per the data sheet recommendation to mitigate spurious data
//  UCSR0C = UCSR0C | B00000110;
delay(1000);
  Serial.write(0x55);
serial_response();
  delay(1000);
    dostuff(arr, sizeof(arr)); // the name of an array points to the array & you are passing 10 values
    dostuff(new_arr, sizeof(new_arr)); // the name of an array points to the array & you are passing 10 values
}

void loop()
{
  serial_response();
}

void dostuff(uint8_t *a, int BUF)
{
  Serial.write(a, BUF);  // Serial.write wants the array pointer and the number of elements
  /*more stuff*/
  serial_response();
 // memset(&a, 0, BUF);
}

void  serial_response(){
// // delay(1);
//    int incomingByte=0;
//      if (Serial.available() > 0) {
//      // read the incoming byte:
//      incomingByte = Serial.read();
//      //if (incomingByte = 0x15)
//      //  Serial.print("I received: ");
//       Serial.println(incomingByte, HEX);
//      }
 }


Moderator edit: [code] [/code] tags added.
« Last Edit: May 14, 2012, 02:14:41 am by Coding Badly » Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 108
Posts: 5144
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You don't explain what you're expecting and you don't explain what you get. Your sketch is a best spitting out 9 bytes to the serial interface, commands are received, there is no code for it. Don't forget: we don't have your context to get anything meaningful out of some very few lines of code with entropy-less function names (dostuff).
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Maybe if you commented out a few more lines, your problem, whatever it is, would go away.

Code:
  Serial.begin(9600);
  pinMode(1,OUTPUT);
  digitalWrite(1, HIGH); //set high as per the data sheet recommendation to mitigate spurious data
I don't know what datasheet you are reading, but it is WRONG!
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 30
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You don't explain what you're expecting and you don't explain what you get. Your sketch is a best spitting out 9 bytes to the serial interface, commands are received, there is no code for it. Don't forget: we don't have your context to get anything meaningful out of some very few lines of code with entropy-less function names (dostuff).
Oh Sorry, I am controlling an LED display. I am sending two commands to the display.....err 3 commands to the display: 0x55 is a command as well, it auto-detects and mates master/slave baudrate.

The other 2 commands are strings of bits. The reason I have 2 arrays with strings of bits is because I want my code to do something else later on, so it's a place holder to be rewritten you could say. Ideally, "dostuff" is just supposed to send the bits and collect the handshake responses. I'm not sure why I don't send 0x55 through dostuff.....maybe I'll try that after I get this working.

The display is supposed to handshack with the master, but I'm not sure it's working quite properly. I can't flash my arduino and have the display plugged in at the same time, so that is making troubleshooting difficult. If I leave it plugged in and flash it, I get a synch error. If anyone can help me fix that I'd be hugely appreciative.

When the display reads an error, it replies with a 0x15 command, which is shown in the "serial_response" function. It looks like I had it commented out, because I suspected it was making my program behave improperly. I'm wondering now if I should be using software serial, or just don't use the Serial monitor I suppose, but then troubleshooting is somewhat difficult.

Anyway, what happens is that I run the program. Command 1 and 2 are executed. (not sure 2 is executed quite right. It seems kind of slow. Idk if that's normal or not). Command 3 will not execute until I open the serial monitor. I have no idea what is going on there. Unfortunately I don't have much output I can paste in here to show you. I'm just changing the color of the screen color and then putting a circle on the screen. I see the color change.....slowly, and then the circle instantly shows up some fixed duration of time after I open the serial monitor.


@Pauls I tried that. Didn't fix the problem. The datasheet says you should tie the transmit line high while the display is starting up, to help with the autobaud function the module has. Is there a better way to accomplish that through software? Yeah, these datasheets i am working with are a bit sketchy. I don't recall the SPI libraries I wrote for some TI chips taking this long...

Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 30
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, read an old thread. Going to try NewSoftwareSerial. Will post new code when I finish.
Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 108
Posts: 5144
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

NewSoftwareSerial is SoftwareSerial in the new 1.0 IDE and included.

Just be sure: your LED display is controlled by a UART interface? You don't mix up SPI and UART, do you?

If you wanna put the TX line high during startup of the display move Serial.begin() down after that startup happened.

Code:
  pinMode(1,OUTPUT);
  digitalWrite(1, HIGH); //set high as per the data sheet recommendation to mitigate spurious data
  delay(1000);
  Serial.begin(9600);

Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 30
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

No, I don't think I've mixed up SPI. There is no clock for the display to read, so I don't think it can be SPI.


Code:
#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3); // RX, TX
uint8_t arr[]={0x42, 0x10, 0xcc};
uint8_t new_arr[]={0x43,0x50,0x3F,0x22,0x00,0x1F};
uint8_t autobaud[]={0x55};
void setup()
{
  pinMode(2, INPUT);
  pinMode(3, OUTPUT);
  digitalWrite(3, HIGH); //set high as per the data sheet recommendation to mitigate spurious data
  delay(1000);
//  pinMode(8,OUTPUT);
//  digitalWrite(8,HIGH);
//  delay(20);
//  digitalWrite(8,LOW);

    Serial.begin(9600);
  mySerial.begin(9600);
//  UCSR0C = UCSR0C | B00000110;
      delay(1000);
      dostuff(autobaud, sizeof(autobaud));
      dostuff(arr, sizeof(arr)); // the name of an array points to the array & you are passing 10 values
      dostuff(new_arr, sizeof(new_arr)); // the name of an array points to the array & you are passing 10 values
}

void loop()
{

}


void dostuff(uint8_t *buffer, int len)
{
  mySerial.write(buffer, len);  // Serial.write wants the array pointer and the number of elements
  /*more stuff*/
  serial_response();
 // memset(&a, 0, BUF);
}

void  serial_response(){
 // delay(1);
//    int incomingByte=0;
//      if (Serial.available() > 0) {
//      // read the incoming byte:
//      incomingByte = Serial.read();
//
//      if (incomingByte = 0x15)
//      return;
//     
//    //   Serial.print("I received: ");
//      Serial.println(incomingByte, HEX);
//      }

  if (mySerial.available()>0){
  Serial.println(mySerial.read());
 }
 }

Implemented softwareserial, now none of my code works :/ on the bright side, I can flash the arduino now without having to unplug the display.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
SoftwareSerial mySerial(2, 3); // RX, TX
You told the SoftwareSerial instance that it owns pins 2 and 3.

Code:
  pinMode(2, INPUT);
  pinMode(3, OUTPUT);
  digitalWrite(3, HIGH); //set high as per the data sheet recommendation to mitigate spurious data
Then you go messing with them. Why? What data sheet are you talking about? The SoftwareSerial instance should be doing this, if it is necessary.

Code:
  mySerial.write(buffer, len);  // Serial.write wants the array pointer and the number of elements
Write a bunch of stuff to the serial port. OK.

Code:
  if (mySerial.available()>0){
  Serial.println(mySerial.read());
Immediately, expect a one character response. Why?

Well, I'm waiting?

C'mon. Hurry up.

I'm not a patient type, either.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 30
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  if (mySerial.available()>0){
  Serial.println(mySerial.read());
Immediately, expect a one character response. Why?

Well, I'm waiting?

C'mon. Hurry up.

I'm not a patient type, either.
I am guessing you are referring to status registers? I need to wait until TX/RX is complete? I found a website on that subject that seems informative. I'll have to refresh myself.

Can I write the status registers directly into the IDE?
Logged

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12549
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am guessing you are referring to status registers?

No, I believe PaulS is referring to the fact that in the real world there will be a significant delay between sending a message out, and having the response available to you. Your sketch makes no allowance for that and assumes that the response will be available instantly.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 30
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

oh ok. I think I understand now. So the code goes to that function, and can potentially immediately evaluate false (or true), even though it may not actually be false (or true), and the program will then continue on.

I think I understand, but I am having difficulty coming up with the logic.

basically I want to loop until I get a handshake of "0x06"=good or a "0x15"=bad.  so my response function needs to loop until it receives a response for each command transmitted. I have something written, but it's poor form. If the unit doesn't send a response, then the code goes into an infinite loop.....lol

Code:
#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3); // RX, TX
uint8_t arr[]={0x42, 0x10, 0xcc};
uint8_t new_arr[]={0x43,0x50,0x3F,0x22,0x00,0x1F};
uint8_t autobaud[]={0x55};

void setup()
{
  Serial.begin(9600);
    mySerial.begin(9600);
      delay(1000);
      dostuff(autobaud, sizeof(autobaud));
      dostuff(arr, sizeof(arr));
      dostuff(new_arr, sizeof(new_arr));
}

void loop()
{
}

void dostuff(uint8_t *buffer, int len)
{
  mySerial.write(buffer, len);  // Serial.write wants the array pointer and the number of elements
  Serial.print("Command: ");
  Serial.write(buffer, len);
  Serial.println("|");
  serial_response();
}

void  serial_response(){
  
int incomingByte=0;
    if (mySerial.available()>0){
       incomingByte = mySerial.read();
       Serial.print("Response: ");
       Serial.print(incomingByte, HEX);
       Serial.println("|");
     }
    else{
       serial_response();
    }
 }


the output is:

Command: U|
Response: 0|
Command: B Ì|
Response: 0|
Command: CP?"  |
Response: 0|

I'm not sure why the code is printing zeros, because....the only response should be 0x06 or 0x15 as far as I know. The display is doing nothing.....it's just black. So if the display is not responding at all, I'm not sure why the code prints any response at all.

I'll have to look at the datasheet again when I get back. I'm still not sure if I'm supposed to get a response for every byte, or every command. It's every command.
« Last Edit: May 15, 2012, 03:18:09 pm by PearlRock » Logged

Western New York, USA
Offline Offline
Faraday Member
**
Karma: 32
Posts: 4258
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So, you are sending magic numbers to a mystery device which may or may not be responding to one or more of those magic numbers.   You are expecting some sort of output from that mystery device but you are not getting that output and you can't figure out why.

You are the one who knows what the mystery device is, what information you are supposed to send it, and how it is supposed to respond but you can't determine why it isn't working the way you think it should.  We know nothing about your mystery device except that it is some sort of serial LED display.   How do you expect us to help?


Don
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 30
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So, you are sending magic numbers to a mystery device which may or may not be responding to one or more of those magic numbers.   You are expecting some sort of output from that mystery device but you are not getting that output and you can't figure out why.

You are the one who knows what the mystery device is, what information you are supposed to send it, and how it is supposed to respond but you can't determine why it isn't working the way you think it should.  We know nothing about your mystery device except that it is some sort of serial LED display.   How do you expect us to help?


Don

Well, basic UART should be pretty universal amongst all devices. You send data, you wait for the response back. What my mystery device is really shouldn't matter too much, assuming the call and response information I have provided is correct.  People have made suggestions which made me re-examine the way I have my code structured. that is helpful to me, and what I am really looking for in terms of help. Other then that, there really isnt much else you can do unless you want want to start reading through the datasheet(s) themselves. I'm learning, so I want to handle the datasheet business myself. The device is a 4D uOLED-160-G1. The datasheet I am primarily referencing is here
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
void  serial_response()
{
   int incomingByte=0;
   if (mySerial.available()>0)
   {
      incomingByte = mySerial.read();
      Serial.print("Response: ");
      Serial.print(incomingByte, HEX);
      Serial.println("|");
   }
   else
   {
      serial_response();
   }
 }
If there is no data yet, make a recursive call to this function. Nope, not going to work.

You need to learn about while loops! While there is no serial data, sit on your hands and do NOTHING!
Logged

Western New York, USA
Offline Offline
Faraday Member
**
Karma: 32
Posts: 4258
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You have not mentioned anything about seeing, or not seeing, the splash screen.  Could you comment on this and also answer the following questions based on your original post?  (Some of these questions would not be necessary if you had properly commented your code, specifically if you had identified what the magic numbers are supposed to do.)

Quote
I am sending 2 commands to the slave device, but only the first command appears to be read.
What makes you think that the command 'appears' to be read?
-- What command are you sending?
-- What response do you expect to get?
-- What response do you actually get?


Quote
The second command executes, albeit appears to execute incorrectly ... It's kind of doing what I want.....but not quite.
What makes you think that the command 'appears' to execute and why do you think that it is incorrect?
-- What command are you sending?
-- What response do you expect to get?
-- What response do you actually get?


Quote
... only when I open the serial monitor....
--What happens, or does not happen, before you open the serial monitor?
--What  happens, or does not happen, after you open the serial monitor?


Quote
I'm not even sure the first command is quite right.
-- What command are you referring to?
-- What response do you expect to get?
-- What response do you actually get?


Quote
It goes kinda slow ... Maybe that command is supposed to be slower. It's hard to say.
What do you mean by slow?   Do you mean there is a delay before you start to see something, or do you mean it takes a long time between the time it starts to do what ever it is doing and the time it finishes?

Quote
but idk.
I am a native English speaker but many of the contributors to this forum, even in this English language section, are not.  Also many of us are more than 14 years old.  You are severely limiting the number of people who can help you what you include abbreviations such as this.

Don





Logged

Pages: [1] 2   Go Up
Jump to: