I'm writing the serial port interface for Empire for use with a BT-Serial port adapter, and I'm running into a problem receiving the data on the Empeg side. I've got the Palm part running, the Palm sends data in sets of 8 characters, it looks like this:

Code:
BEG__ENQ
000001B4
00000E92
00000E93
00000E94
END__ENQ


I send the data using sockets (over Bluetooth) but it is straightforward. The data is copied into a buffer using StrCopy:

Code:
        if(btBeamPosition == -1) {
switch(palantirSave->uploadMode) {
case UPLOAD_MODE_INSERT: StrNCopy(btData, "BEG__INS", 8); break;
case UPLOAD_MODE_APPEND: StrNCopy(btData, "BEG__APP", 8); break;
case UPLOAD_MODE_REPLACE: StrNCopy(btData, "BEG__REP", 8); break;
case UPLOAD_MODE_ENQUEUE: StrNCopy(btData, "BEG__ENQ", 8); break;
}
} else if(btBeamPosition == playlistCount) {
switch(palantirSave->uploadMode) {
case UPLOAD_MODE_INSERT: StrNCopy(btData, "END__INS", 8); break;
case UPLOAD_MODE_APPEND: StrNCopy(btData, "END__APP", 8); break;
case UPLOAD_MODE_REPLACE: StrNCopy(btData, "END__REP", 8); break;
case UPLOAD_MODE_ENQUEUE: StrNCopy(btData, "END__ENQ", 8); break;
}
} else {
tItem = DmQueryRecord(palantirDB, playlist[btBeamPosition]);
tRecPacked = (TrackRecordPacked *) MemHandleLock(tItem);
MemHandleUnlock(tItem);
StrIToH(btData, tRecPacked->fid);
}

btData[8] = chrLineFeed; //0x0A
//btData[8] = chrCarriageReturn; //0x0C
btData[9] = chrNull; //0x00

if((err = BtLibSocketSend(btLibRefNum, btSocket, (UInt8*)btData, 10)) != btLibErrPending)
FrmCustomAlert(BtErrorAlert, "BtBeamPlaylist()\n", "Error on data send\n", StrIToA(charIntDebug, err));


The code that I am using to receive (serialtest.c) is attached, important is the open:

Code:
	if((serial_fd = open("/dev/ttyS1", O_NOCTTY | O_RDWR | O_NONBLOCK)) > -1) {
//install the signal handler before making the device asynchronous
saio.sa_flags = 0;
saio.sa_restorer = NULL;
saio.sa_handler = signal_handler_IO;
sigemptyset(&saio.sa_mask);
sigaction(SIGIO, &saio, NULL);

//allow the process to receive SIGIO
fcntl(serial_fd, F_SETOWN, getpid());
fcntl(serial_fd, F_SETFL, FASYNC);

bzero(&termios, sizeof(termios));
termios.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
termios.c_iflag = ICRNL;
termios.c_oflag = 0;
termios.c_lflag = ICANON;
termios.c_cc[VMIN] = 1;
termios.c_cc[VTIME] = 0;

tcflush(serial_fd, TCIFLUSH);
if(tcsetattr(serial_fd, TCSANOW, &termios) > -1)
ret = 1;
}


And the code to read:

Code:
void BT_HandleInput(void) {
int len;
char buffer[255];

if(wait_flag == 0) {
len = read(serial_fd, buffer, 255);
buffer[len] = 0x00;
fprintf(stderr, "line[%d]: %s", len, buffer);
wait_flag = 1; /* wait for new input */
}
}


Now, everything works fine if I run serialtest on the Empeg, connect to the Bluetooth adapter via my laptop and use Hyperterminal to send lines over. The output comes out fine. The output is also coming across ok if I cat /dev/ttyS1 on the Empeg and use my Palm to beam over a playlist. I get the entire list printed out on the screen. What isn't working correctly is if I use my Palm to beam a playlist and read it with serialtest. I get the following:

Code:
sig:sig:line[9]: BEG__ENQ


Which makes me wonder.. I am getting two signals for input on the serial port? Why don't I get the next line? If I send slowly (by pausing a bit between sends on the Palm side) I get something different:

Code:
sig:line[9]: BEG__ENQ
sig:line[10]: sig:line[10]: sig:line[10]: sig:line[10]: sig:line[10]:


Help?


Attachments
271304-serialtest.c (403 downloads)

_________________________
Mark Cushman