Pages: [1]   Go Down
Author Topic: SoftwareSerial  (Read 428 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am using the SoftwareSerial library with a 4d systems 4.3" screen

I have tried this on both the Arduino UNO and the Arduino Mega 2560 with the same result.

I have used the Visi Genie to create all of my interfaces.

The problem lies in the fact that I am getting 0 data transfer from the screen to arduino

Code:
mySerial.available()
is giving me 0 which will not allow any of the other code to run.

I have also used the following code.

Code:
///
/// @mainpage Arduino_RGB_Genie
/// @details Set colour from panel with red-green-blue sliders
/// @n
/// @n @a Developed with [embedXcode](http://embedXcode.weebly.com)
///
/// @author Rei Vilo
/// @date Nov 13, 2012
/// @version 308
///

// Core library
#include "Arduino.h" // for Arduino 1.0

// Include application, user and local libraries
#include "SoftwareSerial.h"

// Define variables and constants
SoftwareSerial mySerial(2, 3);

#define RED_PIN 9
#define GREEN_PIN 10
#define BLUE_PIN 11

#define READ_OBJ 0x00
#define WRITE_OBJ 0x01
#define REPORT_OBJ 0x05
#define REPORT_EVENT 0x07

const uint16_t buttonReset = 0x0600;
const uint16_t sliderRed = 0x0400;
const uint16_t sliderGreen = 0x0401;
const uint16_t sliderBlue = 0x0402;

uint8_t red, green, blue;
uint8_t command;
uint16_t object, value;
boolean flag;

uint8_t nacAck() {
delay(10);
while (!mySerial.available());
return mySerial.read(); // 0x06 or 0xd5
}

bool readMessage(uint8_t &command0, uint16_t &object0, uint16_t &value0) {
bool flag0 = false;
uint8_t buffer[6];
uint8_t checksum = 0x00;
uint8_t i=0;

if (mySerial.available()>0) {
buffer[i] = mySerial.read();
command0 = buffer[0];
i++;
checksum = command0;

if (command0==REPORT_EVENT) {
if (mySerial.available()>3) {
buffer[i] = mySerial.read();
checksum ^= buffer[i];
i++;
buffer[i] = mySerial.read();
checksum ^= buffer[i];
i++;
object0 = buffer[1]<<8 | buffer[2];

buffer[i] = mySerial.read();
checksum ^= buffer[i];
i++;
object0 = buffer[1]<<8 | buffer[2];
buffer[i] = mySerial.read();
checksum ^= buffer[i];
i++;
value0 = buffer[3]<<8 | buffer[4];
}
}
flag0 = (checksum==mySerial.read());
}
return flag0;
}


bool sendMessage(uint8_t command0, uint16_t object0, uint16_t value0) {
bool flag0 = false;
uint8_t buffer[6];
uint8_t checksum = 0x00;
uint8_t i=0;

buffer[i] = command0;
checksum = command0;
i++;

buffer[i] = object0 >>8;
checksum ^= buffer[i];
i++;

buffer[i] = object0 & 0xff;
checksum ^= buffer[i];
i++;

buffer[i] = value0 >>8;
checksum ^= buffer[i];
i++;

buffer[i] = value0 & 0xff;
checksum ^= buffer[i];
i++;

buffer[i] = checksum;
i++;

mySerial.write(buffer, i);

return (nacAck()==0x06);
}

void update() {
analogWrite(RED_PIN, red);             // value for red channel
analogWrite(GREEN_PIN, green);             // value for blue channel
analogWrite(BLUE_PIN, blue);             // value for green channel
}


void reset() {
uint8_t a;

red   = 0x7f;
green = 0x7f;
blue  = 0x7f;

mySerial.flush();
a = sendMessage(WRITE_OBJ, sliderRed, red);
a = sendMessage(WRITE_OBJ, sliderGreen, green);
a = sendMessage(WRITE_OBJ, sliderBlue, blue);

update();
}


// Add setup code
void setup() {
delay(3000);
mySerial.begin(9600);

pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);

reset();
update();
}

// Add loop code
void loop() {
if (readMessage(command, object, value)) {
if (command==0x07) {
switch (object) {
case buttonReset:
reset();
break;

case sliderRed:
red = value & 0xff;
update();
break;

case sliderGreen:
green = value & 0xff;
update();
break;

case sliderBlue:
blue = value & 0xff;
update();
break;

default:
break;
}
}
}
delay(100);
}


Any ideas???
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have also used both the shield that came with it and bypassed the shield with hardwires so I could make sure TX and RX pins were correct with the same results
Logged

Offline Offline
Faraday Member
**
Karma: 62
Posts: 3018
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Are you saying,  mySerial.available() is always zero ?

That means, you are not receiving anything.   Most likely causes are
(a)  incorrect connection
(b)  the sending device is not sending anything !

Suggestion.   Connect to something else,   the PC serial monitor, some other serial device,   and see if you
receive anything from this mySerial connection.

Suggestion.  Try some different pins for the mySerial connection.

Suggestion.  Swap the TX and RX connections.      It is supposed to be TX -> RX  and   RX <- TX   but some devices are
incorrectly labelled.


Your other problem is likely to be,   even when you get something there on Serial.available(),   your logic
in that reading function looks bad,   it is unlikely to work reliably,   it does not adequately account for
the asynchronicity of the serial communications.
Logged

DELHI
Offline Offline
God Member
*****
Karma: 8
Posts: 821
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I used the liberary and it is working fine.

* SoftwareSerial.rar (8.16 KB - downloaded 13 times.)
Logged

AMPS

Pages: [1]   Go Up
Jump to: