0 registered members (),
1,459
guests, and 9
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Re: Serial Communications Header
[Re: EvilSOB]
#349185
12/04/10 19:23
12/04/10 19:23
|
Joined: Jun 2006
Posts: 2,640 Earth
Germanunkol
Expert
|
Expert
Joined: Jun 2006
Posts: 2,640
Earth
|
Hi EvilSOB! Thanks for this library. We've been using it for a couple of days to try and communicate with a Micro Controller. We got the communication to work now, except for one problem: If the connection is not constantly busy (constantly meaning sending stuff about once a second from MC to the COM port, it seems to "time out"). We're using a USB-Adapter, and here is the code:
void collectInput(var hPort)
{
(inputText.pstring)[0] = inputStr;
inputText.pos_y = screen_size.y - 25;
while(1)
{
result = inkey(inputStr);
// while(inkey_active == ON) wait(1);
if(result == 13)
{
port_write_bytes(hPort,_chr(inputStr), str_len(inputStr)+1,1);
appendLine(sendTxt, inputStr);
}
str_cpy(inputStr, " ");
wait(1);
}
}
byte singleByte = -1;
byte inputBytes[10] = {0,0,0,0,0,0,0,0,0,0};
double inputDouble = -1;
var resetCharArray()
{
var count = 0;
while(count < 10)
{
inputBytes[count] = 0;
count += 1;
}
}
void receiveData(var hPort)
{
STRING* inputStr = str_create("");
var count = 0;
while(1)
{
if(key_esc) break;
result = port_read_bytes(hPort,singleByte,1);
if(result > 0)
{
if(singleByte == 0 || count == 9)
{
count = 0;
appendLine(receiveTxt, _str(inputBytes));
resetCharArray();
}
else
{
inputBytes[count] = singleByte;
count += 1;
}
}
wait(1);
}
}
function main()
{
wait(1);
appendLine(sendTxt, "Started.");
on_close = exit_safely;
initLevel();
on_esc = NULL;
byte last_recieved = 0;
hPort = port_open_readwrite("COM2", "9600,n,8,1");
if(hPort)
{
collectInput(hPort);
receiveData(hPort);
wait_for(receiveData);
port_close(hPort);
}
else
{
error("Error connecting to Port");
while(key_esc == OFF) wait(1);
}
sys_exit("");
}
We can't figure this one out. If we don't send a "NULL" from the MC to the PC every second, then nothing seems to be received any more at all.
~"I never let school interfere with my education"~ -Mark Twain
|
|
|
Re: Serial Communications Header
[Re: Germanunkol]
#349319
12/06/10 06:14
12/06/10 06:14
|
Joined: Feb 2008
Posts: 3,232 Australia
EvilSOB
OP
Expert
|
OP
Expert
Joined: Feb 2008
Posts: 3,232
Australia
|
OK, I'll take a look in the next day or so.
Im going to have to refresh my memory of this library, AND dig up some external hardware to test with if I cant see any obvious code-bugs in my or your coding... (I have had un-resolved intermittant problems in the past with port_read_bytes losing sync with its buffer at times so it may be something there)
FYI:: 90% of my testing was done through a USB-serial adaptor, so its unlikely to have anything to do with that...
I'll let you know back here...
"There is no fate but what WE make." - CEO Cyberdyne Systems Corp. A8.30.5 Commercial
|
|
|
Re: Serial Communications Header
[Re: EvilSOB]
#349353
12/06/10 19:35
12/06/10 19:35
|
Joined: Jun 2006
Posts: 2,640 Earth
Germanunkol
Expert
|
Expert
Joined: Jun 2006
Posts: 2,640
Earth
|
Thank you a lot! I've spent quite a few hours now with your code. Apart from initial problems where I didn't understand how it worked and then a few troubles with strings, it's been working great. We're building a prototype 3d-scanner with 2 motors and a distancescanner which displays its data in acknex, plotted using draw_line3d. It's a really fun project, and even though it's very simple and non-professional, we've already had some neat tests that we did. Just to let you know what your code made possible
~"I never let school interfere with my education"~ -Mark Twain
|
|
|
Re: Serial Communications Header
[Re: IngoS.]
#377327
07/12/11 09:35
07/12/11 09:35
|
Joined: May 2005
Posts: 29 Jiangsu, China PR
eaglelab
Newbie
|
Newbie
Joined: May 2005
Posts: 29
Jiangsu, China PR
|
I using Port_IO.h no problem when each time a byte is sent。 but when sending multiple consecutive bytes, are sent the wrong data. code: function func_write() { byte last_test[8] ={ 0x01, 0x02,0x03, 0x04,0x05, 0x06,0x07, 0x08};
port_write_bytes(hPortwrite, &last_test,8,50); }
Thank you!
|
|
|
Re: Serial Communications Header
[Re: eaglelab]
#383755
09/25/11 01:47
09/25/11 01:47
|
Joined: Feb 2008
Posts: 3,232 Australia
EvilSOB
OP
Expert
|
OP
Expert
Joined: Feb 2008
Posts: 3,232
Australia
|
Hiya guys, long time no see. Its been 6 months since Ive even touched Gamestudio, so Im a bit rusty... But Im trying to get back into it again, just to validate the money I just spent upgrading to A8...
So I will be looking at all your problems, once Ive re-familiarised myself with gamestudio in general, and POINTERS in particular, because port_io.h was evil with them...
"There is no fate but what WE make." - CEO Cyberdyne Systems Corp. A8.30.5 Commercial
|
|
|
Re: Serial Communications Header
[Re: EvilSOB]
#391468
01/12/12 20:38
01/12/12 20:38
|
Joined: Jan 2012
Posts: 1 Canada
ed_h
Guest
|
Guest
Joined: Jan 2012
Posts: 1
Canada
|
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!
|
|
|
|