Hi EvilSOB,

Thank you for posting the serial communications header. It's a very useful utility and a great way to start learning about API calls using Lite C.

Sending characters works flawlessly for me. There seems to be something odd happening in trying to receive them though. This has happened on 2 different computers using both hardware and USB adapter serial ports.

The first character received comes in fine. After that, when receiving individual characters, nothing happens. Receiving a continuous stream of characters does work though. This is consistent with Germanunkol's post. I'm receiving one byte at a time using the approach you recommended in another post:

"If you are trying to read a single BYTE, then use
Code:

// var tmp; //assumed...
BYTE tmp_byte=0;
port_read_bytes(portSerial, &tmp_byte, 1);
tmp = tmp_byte;"

I think what's happening is when a receive buffer is initially empty, the first character in the buffer is received fine. Once the buffer has received that initial character though, it's actually the second character that gets retrieved from then on. So, if you send a single character and the buffer never fills beyond that one character, you always retrieve a null character (the second character in the buffer). If characters come in faster than you retrieve them and the buffer fills a little, you do read valid data because the second buffer position then has something in it. The following test code demonstrates the effect:

#include <acknex.h> // Include header file required by the acknex engine.
#include <default.c> // Include default engine keys and functions.
#include <serial_io.h> // Include header file containing the serial routines.

TEXT* debug = { pos_x=20; pos_y=20; size_y=400; font="Arial#20b"; strings=20; flags=SHOW + WWRAP; }

var port_status;
var port_handle;
var char_counter = 0;

var temp;
byte tmp_byte;

function main()
{
wait(1); level_load(NULL); wait(1); diag("\n\n\n");
on_esc = NULL;

port_handle = port_open_readwrite("COM1", "9600,n,8,1");

wait(-1);

str_cpy((debug.pstring)[0], "Port Handle = ");
str_cat((debug.pstring)[0], _chr(str_for_int(NULL,port_handle)));

while(1)
{
if(port_read_buffer_size(port_handle) != 0)
{
++char_counter;

tmp_byte=0;
port_status = port_read_bytes(port_handle, &tmp_byte, 1);
temp = tmp_byte;
str_cpy((debug.pstring)[0], "Byte received char code = ");
str_cat((debug.pstring)[0], _chr(str_for_int(NULL,temp)));
str_cat((debug.pstring)[0], " Total characters = ");
str_cat((debug.pstring)[0], _chr(str_for_int(NULL,char_counter)));
}
else
{
str_cpy((debug.pstring)[0], "No byte received.");
str_cat((debug.pstring)[0], " Total characters = ");
str_cat((debug.pstring)[0], _chr(str_for_int(NULL,char_counter)));
}

if(key_esc) break;

wait(-1);
}

// data_byte[0] = 't';
// port_status = port_write_bytes(port_handle, &data_byte[0], 1, 500);

wait(-0.5);

port_close(port_handle);

sys_exit("");
}

With this program doing the receiving, when you transmit an initial character from another computer, its character code number will display fine. If you then send just a single character again, character code "0" will get displayed. You can send as many single characters as you like with the same resulting character code, as long as you wait until "No byte received." is displayed again. If you send two characters in quick succession though, you'll get a "0" code displayed for the first character received but a correct character code for the second character.

It seems to be something to do with buffer pointers that I'd really like to solve, but unfortunately, I don't have enough API programming or Lite C experience to figure out. In the mean time, I've hard coded a workaround in the header to only receive one byte at a time. It misses the initial character received when the program is first started but otherwise, it's good enough for now. A general solution would sure be great though and hopefully you might be able to find time to look at this.

Thanks again!