Pages: [1]   Go Down
Author Topic: read message from serial port  (Read 690 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 8
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi

i have the following coding which receives a messagr serialy. Now at the begning of every message im gonna have 3 integers to determine what type of respons i need.

Now i need to store that message in a variable so that i can display it on a led matrix displa later on. the problem is i cant get a good convertion of the char to char* because an error alwyas pops up....can someone help pls??

thanks

Code:
#define BAUD_RATE 9600
#define TERM_CHAR '\n'
#define BUF_LEN   128

void setup()
  // setting baud rate at 9600
  {
  Serial.begin(BAUD_RATE);
  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
  }
  
int i, a = 0;
char incomingChar, buf[BUF_LEN];
char* msg;

void loop()
  {
   //clean buffer
  memset(buf,'\0',BUF_LEN); i = 0;
  
    while(Serial.available())
    {
    incomingChar = (char) Serial.read();
    if(incomingChar != TERM_CHAR && i != BUF_LEN)
     {
      
       buf[i++] = incomingChar;
          
    
  if(buf[0] == '1' && buf[1] == '0' && buf [2] == '1')
    {
      digitalWrite(13, HIGH);
     for (a = 3; a < i; a++)
    {
      
      msg = incomingChar;
    
    }
    
    
    }
    
  
  }
    }
  }
Logged

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

On every pass through loop, you reset i to 0. Then, you read whatever serial data has arrived since the last pass though loop, looking for a full buffer or a terminating character. Any data read is stuffed in a buffer.

You check the first three values in the buffer, even if you have entered only one, and do something if, by magic, the array contains '1', '0', and '1'.

Apparently, this does not happen very often. I'm not surprised.

The first thing you need to tell us is where the data being read is coming from, and whether you have any control over that data being sent by that source.
Logged

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

Hi

i managed the convertion part so thanks anyway....the last probelm that i have is that i need to clear the variabel msg when ever a new message is being received. i tried the memset function to clear the msg variable but there is no scrolling on the display just the world only. if i dont clear the array if i type amessage..it will appear on the display scrolling...if i type another message, this will be added to the previous message. i need it to clear


Code:
#include <SureLEDMatrix.h>

#define BAUD_RATE 9600
#define TERM_CHAR '\n'
#define BUF_LEN   128

int PIN_CHIPSELECT_1 = 9;  // CS for LED matrix 1
int PIN_WRITE = 7;
int PIN_DATA = 8;

// Instance of LED matrices
SureLEDMatrix matrix1(PIN_CHIPSELECT_1, PIN_WRITE, PIN_DATA, kMatrixType_32x8);  
//char msg[] = "JP";
int i, a, b = 0;
char incomingChar, buf[BUF_LEN];
char msg[BUF_LEN];

int scroll = 0;




void setup()
  // setting baud rate at 9600
  {
  Serial.begin(BAUD_RATE);
  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
  
    // Initialize I/O pins
  pinMode(PIN_CHIPSELECT_1, OUTPUT);
  pinMode(PIN_WRITE, OUTPUT);
  pinMode(PIN_DATA, OUTPUT);

  // Initialize the LED matrices
  matrix1.Initialize();  
  matrix1.SetBrightness(7);

   }
  


void loop()
  {
  
   writedisp();
   int maxscroll = strlen(msg) * 6;
  
   matrix1.DrawText(msg, 7, scroll, true);

    scroll = scroll-1;

    if (scroll < -maxscroll)
    {
      scroll = 30;
      
    }
    delay(100);

  }
  
void writedisp()
{
  //clean buffer
  memset(buf,'\0',BUF_LEN); i = 0;
  memset(msg,'\0',strlen(msg));
  
    while(Serial.available())
  {
    incomingChar = (char) Serial.read();
    if(incomingChar != TERM_CHAR && i != BUF_LEN)
      buf[i++] = incomingChar;
    else
      break;
      
    delay(1); // wait for another byte
  }  
    
  if(buf[0] == '1' && buf[1] == '0' && buf [2] == '1')
    {
      digitalWrite(13, HIGH);
     for (a = 3; a < strlen(buf); a++)
    {
      
      msg[b] = buf[a];
      b++;
      
 
  
   }
  
    }
  

    
  
  
}
Logged

Pages: [1]   Go Up
Jump to: