I am using BAFO bf-810 USB to Serial adapter with pl2303 drivers on Mac OS X. I am a beginner in the field and so have got the high level APIs from here which is a cross-platform API.
I have two programs, one to write and one to read. This API does not have a inbuilt samples in the code. So I had to look at other's APIs to get a picture of what had to be done (like, comOpen, comWrite etc.). With the limited knowledge, I wrote the following little programs using the linked APIs.
sendSerial.c
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include "rs232.h"
extern int errno;
int main(){
comEnumerate();
if(!comOpen(1, 9600)) printf("Cannot open the port\n");
while (1){
char data[1024] = "Hello World";
int ret = comWrite(1, data, 512);
printf("%d\n", ret);
if(ret>0){
printf("Bytes transferred!\n");
} else{
printf("%s\n", strerror(errno));
}
sleep(1);
}
return 0;
}
.
recSerial.c
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include "rs232.h"
extern int errno;
int main(){
comEnumerate();
if(!comOpen(1, 9600)) printf("Cannot open the port\n");
char read[1024] = "EMPTY";
printf("\nNow reading data at %s...\n",comGetPortName(1));
while (1){
printf(".\n");
if(comRead(1, read, 4)){
printf("Data: %s\n**\n", read);
}
sleep(1);
}
return 0;
}
The source has a single C program and might help on what I might be missing.
Question
When sending data, the write returns the Resource temporarily unavailable
error after the third iteration. Why is this happening consistently? When I restart the sender program when the receiver is running, the error starts off with the first iteration itself.
In any case, I am not able to read any data on the read side of the program. I understand that the RS232 is an async protocol, but I've read somewhere that the system has a buffer of around 4 Mb before the data gets overwritten. So the second question is, do I have to manually handle the clock synchronisation?
P.S.: For the loopback test, I have connected, Send-Recieve, RTS-CTS and DTR-DSR on the DB9 connector.