Trouble getting Serial out

Hi,

I am trying to interface my Arduino with a Aerocomm ac4790 transceiver (8N1 serial device) by sending a firmware version request, then read the response from the transcevier.

The transmit code works (I debugged it with LED/delay, and there are four 1's in the command and it flashed 4 times), but the read code is not working... Whenever I implement the read code in loop(), the program transmits the request only once (it should loop), so I gather that the read code is getting stuck for some reason.

//March 2, 2009
//Richard Beare

#include <ctype.h>

#define bit9600Delay 84  
#define halfBit9600Delay 42
#define bit4800Delay 188 
#define halfBit4800Delay 94 

byte rx = 6;
byte tx = 7;
byte SWval;
int versionRequest[2];

void setup() {
  pinMode(rx,INPUT);
  pinMode(tx,OUTPUT);
  pinMode(8, OUTPUT);
  digitalWrite(tx,HIGH);
  Serial.begin(9600);
  // Set up command -> version request asks the transceiver for firmware version 0xCC 0x00 0x00
  versionRequest[0] = 204; versionRequest[1] = 0; versionRequest[2] = 0; 
}

void SWprint(int totalBytes, int command[])
{
  byte mask;
  int currentByte;
 
  //Iterate through each byte
  for (int i=0; i<totalBytes; i++) {  
  currentByte = command[i];
  //startbit
  digitalWrite(tx,LOW);
  delayMicroseconds(bit9600Delay);
  
  for (mask = 0x01; mask>0; mask <<= 1) { 
    if (currentByte & mask){ // choose bit
     digitalWrite(tx,HIGH); // send 1
    }
    else{
     digitalWrite(tx,LOW); // send 0
    }
    delayMicroseconds(bit9600Delay);
  }
  
  //stop bit
  digitalWrite(tx, HIGH);
  delayMicroseconds(bit9600Delay);
  
  }
}

int SWread()
{
  
  byte val = 0;
  while (digitalRead(rx));
  
  //wait for start bit
  if (digitalRead(rx) == LOW) {   
    delayMicroseconds(halfBit9600Delay);
    //response is four bytes
    for (int offset = 0; offset < 32; offset++) {
     delayMicroseconds(bit9600Delay);
     val |= digitalRead(rx) << offset;
    }
    //wait for stop bit + extra
    delayMicroseconds(bit9600Delay); 
    delayMicroseconds(bit9600Delay);
    
    return val;
  }
}


void loop()
{
    SWprint(3, versionRequest);
    int value = SWread(); // Serial.print doesnt seem to work for simple strings/ints either....
}

As well, Serial.print doesnt seem to work at all...

Any help would be greatly appreciated :slight_smile:

Richard Beare

Hi Richard, is there a reason you are not using the SoftwareSerial library distributed with Arduino?

But even better is the very nice library here: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1233298805