Just in case anyone was hoping to find a working C example in this topic after a search, this code has been a great starting point for me in the past:
http://www.teuniz.net/RS-232/
If you're using the UART gpio pins on the raspberry pi, make sure to add '/dev/ttyAMA0' to the list of comports, or change an existing one (rs232.c, line 47). For arduino (or other ftdi-like solutions), there's already a '/dev/ttyUSB0' in the list on position 16.
Then in your main.c, it's basically something like:
Code: Select all
#include "rs232.h"
#define COMPORT 16 // this is '/dev/ttyUSB0' (for arduino) or choose wherever you added '/dev/ttyAMA0' (for raspberry pi UART pins) to the list
#define BAUDRATE 9600 // or whatever baudrate you want
#define RECEIVE_CHARS 8 // or whatever size of buffer you want to receive
#define SEND_CHARS 4 // or whatever size of buffer you want to send
int main (int argc, char **argv) {
unsigned char receive_buffer[RECEIVE_CHARS];
unsigned char send_byte = 42;
unsigned char send_buffer[SEND_CHARS] = {'a','b','c','d'};
OpenComport(COMPORT, BAUDRATE);
while(1) {
// change send_byte and/or send_buffer with what you want to send. Then:
SendByte(COMPORT, send_byte); // or:
SendBuf(COMPORT, send_buffer, SEND_CHARS);
// and/or:
PollComport(COMPORT, receive_buffer, RECEIVE_CHARS);
// do something with received data in buffer
// maybe sleep for a while
}
CloseComport(COMPORT);
return 0;
}
I haven't tested this code with an actual microprocessor/arduino, but it should get anyone started. I had something similar working before. At least it compiles: