Go Down

Topic: Arduino as RS232 converter? (Read 622 times) previous topic - next topic

Hi.

Pardon the noobish questions.

I had a RS232 transmitter/receiver and an arduino laying around. this is the MAX3222 variety (http://datasheets.maxim-ic.com/en/ds/MAX3222-MAX3241.pdf).  I don't have a rs232 port on the computer so I can't test this out, unfortunately.  My main goal here is to program another microcontroller with a rs232 input (http://www.ladyada.net/make/minipov3/software.html)

The main questions I have are:

-Once arduino has booted up (2 seconds or so?) can it receive standard serial outputs from other programs via com port? Is there anything special I have to initialize in the sketch for this to work? I've only ever used it to communicate with the arduino serial monitor.  No experience using it with other programs.
-As I don't have a serial input/output on my computer I can't really test to make sure my sketch works... here's the code (mostly lifted from the software RS232 guide on arduino.cc)  Anyone see any massive errors that would cause it to not work?  I'm a little nervous about the SWread function as the default code waits for a "read" every time there is a "write", where the "read" function *waits* for an input.  I am not certain whether the circuit I am doing RS232 through will give me an input.  So perhaps I shouldn't be waiting for an input.  Maybe I just need outputs? I am not sure.

Code: [Select]
//Created August 23 2006
//Heather Dewey-Hagborg
//http://www.arduino.cc

#include <ctype.h>

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

byte rx = 8;
byte tx = 9;
byte SWval;
byte serial_out;

void setup() {
  Serial.begin(9600);
  pinMode(rx,INPUT);
  pinMode(tx,OUTPUT);
  digitalWrite(tx,HIGH);
  digitalWrite(13,HIGH); //turn on debugging LED
  Serial.print("hi");
  SWprint('h');  //debugging hello
  SWprint('i');
  SWprint(10); //carriage return
}

void SWprint(int data)
{
  byte mask;
  //startbit
  digitalWrite(tx,LOW);
  delayMicroseconds(bit9600Delay);
  for (mask = 0x01; mask>0; mask <<= 1) {
    if (data & 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);
    for (int offset = 0; offset < 8; offset++) {
     delayMicroseconds(bit9600Delay);
     val |= digitalRead(rx) << offset;
    }
    //wait for stop bit + extra
    delayMicroseconds(bit9600Delay);
    delayMicroseconds(bit9600Delay);
    return val;
  }
}

void loop()
{
    if(Serial.available() > 0)
    {
      serial_out = Serial.read();
      SWprint(serial_out);
      SWval = SWread();
      Serial.print(SWval);
    }



}

johnwasser

If all you want is USB to TTL Serial you don't even need the ATmega chip in the Arduino.   On most models there is a set of 6 holes that give you access to the USB-to-TTL Serial outputs.  Use your MAX3222 for level shifting and you will have a USB-to-RS232 cable.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e


If all you want is USB to TTL Serial you don't even need the ATmega chip in the Arduino.   On most models there is a set of 6 holes that give you access to the USB-to-TTL Serial outputs.  Use your MAX3222 for level shifting and you will have a USB-to-RS232 cable.



Hey, thanks for the reply.  Would those 6 holes be 'sockets' where the ATMega goes? Would I still have to provide separate +5V VDD to the chip?

johnwasser

What Arduino model do you have?
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Go Up