Hello,
I'm using Arduino BT (ATmega328) as "gateway" between a PC and another board (with ATmega164P). The serial connection between the two boards is @ 9600 baud (tried also lower rates). The arduino code is very simple:
#include <NewSoftSerial.h>
int rxPin = 2;
int txPin = 3;
NewSoftSerial mySerial(rxPin, txPin);
void setup() {
Serial.begin(115200);
mySerial.begin(9600);
}
void loop() {
if (Serial.available()) {
mySerial.print((char) Serial.read());
}
if (mySerial.available()) {
Serial.print((char) mySerial.read());
}
}
First of all, one question: the serial between atmega328 and the BT chip (WT11) is @ 115200. But it seems I can send data over the virtual COM on the pc at any rate. Is it correct?
Now, let's see the problems. When my own board transmits data to arduino, it repeats correctly each byte to the pc.
When is the PC that sends data to arduino, it repeats only first 3 bytes to my own board. Example: if I send abcdefghi, it will repeats only abc. Of course if I send char-by-char there is no problem.
I was thinking about some buffer underrun (with so small strings?) and implemented a circular buffer:
#include <NewSoftSerial.h>
#define BUFFER_SIZE 32
#define BUFFER_MASK (BUFFER_SIZE - 1)
int rxPin = 2;
int txPin = 3;
int head = 0;
int tail = 0;
char buffer[BUFFER_SIZE];
NewSoftSerial mySerial(rxPin, txPin);
void setup() {
Serial.begin(115200);
mySerial.begin(9600);
}
void loop() {
if (Serial.available()) {
buffer[head] = Serial.read();
head = (head + 1) & BUFFER_MASK;
}
if (head != tail) {
mySerial.print(buffer[tail]);
tail = (tail + 1) & BUFFER_MASK;
}
if (mySerial.available()) {
Serial.print((char) mySerial.read());
}
}
But the behavior is the same.
I checked with the oscilloscope: just the first three bytes are transmitted over.
Any hints?
Thanks!
Marco