PowenKo > Arduino > Serial read and write

March 23, 2012   ·   0 Comments

Screen Shot 2012-03-23 at 12.15.23 AM

write

// Sends "A"s through the serial port every second
 
// 'Setup' function


#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3); // RX, TX

void setup()
{
  // Start the UART at 9600 bits-per-second (baud)
  Serial.begin(115200);
  
    mySerial.begin(115200);
}
 
// Main loop
void loop()
{
 
  int t1=10;

  Serial.write(t1);

  FunWrite(0x55);
  FunWrite(0x00);
  
  FunWrite(0x00);
  FunWrite(0x00);
  
  FunWrite(0x00);
  FunWrite(0x00);
  FunWrite(0xFF);
  FunWrite(0x02);
  
  FunWrite(0xFE);
  FunWrite(0xD4);
  FunWrite(0x02);
  FunWrite(0x2A);
  FunWrite(0x00);
  

  // Pause 1 second
  delay(5000);
}
void FunWrite(int t1){
  
  Serial.write(t1);
}

read

// www.powenko.com


#include <SoftwareSerial.h>
int val = 0;

SoftwareSerial mySerial(2, 3); // RX, TX
// 'Setup' function
void setup()
{
  // Start the UART at 115200 baud
  Serial.begin(115200);
    mySerial.begin(115200);

}

// Main loop
void loop()
{

   if (Serial.available())
  {
    // ...Set the value of our variable to whatever the data is
    val = Serial.read();


    fun_print(val);
    
    
    
  }
  
}
void fun_print(int analogValue){
 // print it out in many formats:
  Serial.print(analogValue);         // print as an ASCII-encoded decimal
  Serial.print("\t");                // print a tab character
  Serial.print(analogValue, DEC);    // print as an ASCII-encoded decimal
  Serial.print("\t");                // print a tab character
  Serial.print(analogValue, HEX);    // print as an ASCII-encoded hexadecimal
  Serial.print("\t");                // print a tab character
  Serial.print(analogValue, OCT);    // print as an ASCII-encoded octal
  Serial.print("\t");                // print a tab character
  Serial.print(analogValue, BIN);    // print as an ASCII-encoded binary
  Serial.print("\t");                // print a tab character
//  Serial.print(analogValue/4, BYTE); // print as a raw byte value (divide the
                                     // value by 4 because analogRead() returns numbers
                                     // from 0 to 1023, but a byte can only hold values
                                     // up to 255)
  Serial.print("\t");                // print a tab character    
  Serial.println();                  // print a linefeed character 
}
shareShare on FacebookShare on Google+Share on LinkedInEmail this to someonePrint this pageBuffer this pageDigg thisFlattr the authorShare on RedditPin on PinterestShare on StumbleUponShare on TumblrTweet about this on Twitter

By


Readers Comments (0)


You must be logged in to post a comment.

PowenKo’s Online Store

powenko_arduinoBook
Hardware22

Hardware

Amazon

adsbygoogle

Recent Posts

Categories

Amazon