1 registered members (AbrahamR),
717
guests, and 4
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Re: Serial Communications Header
[Re: qoolelee]
#304976
01/12/10 12:10
01/12/10 12:10
|
Joined: Feb 2008
Posts: 3,232 Australia
EvilSOB
OP
Expert
|
OP
Expert
Joined: Feb 2008
Posts: 3,232
Australia
|
port_close waits for the port to be idle before it closes, and I think thats the problem... Try changing the port_close step to this...
...
}
port_close(hPort);
while(proc_status(port_close)) wait(1);
//
//
sys_exit("");
}
Let me know how it goes... any changes in behaviour. Then if its fixed I'll explain what was happening...
"There is no fate but what WE make." - CEO Cyberdyne Systems Corp. A8.30.5 Commercial
|
|
|
Re: Serial Communications Header
[Re: EvilSOB]
#305136
01/13/10 03:35
01/13/10 03:35
|
Joined: Dec 2007
Posts: 5
qoolelee
Newbie
|
Newbie
Joined: Dec 2007
Posts: 5
|
Thank you for your response. The situation is the same, the second re-run of the script is still not able to successfully open the port in SED. After changing my device to com1 instead the usb virtual com9 port, still the same problem happened. I've got a mcu device designed for my other project for stationary bike control, if it got the order control command "0xFD", it will return "0xFD R", where R stands for a single byte indicates the rpm of the bike. So I change the script to
#include <acknex.h>
#include <default.c>
#include "Port_IO.h"
TEXT* debug = { pos_x=20; pos_y=20; size_y=400; font="Arial#20b"; strings=20; flags=SHOW + WWRAP; }
var hPort;
var temp;
var temp2;
byte sent_data = 0xFD;
byte received_data[2] = { 0x00, 0x00};
function main()
{
wait(1); level_load(NULL); wait(1); diag("\n\n\n");
on_esc = NULL;
//
//
hPort = port_open_readwrite("COM1", "2400,n,8,1");
wait(-1);
if(port_read_status(hPort))
{
temp = port_write_bytes(hPort, &sent_data, 1, 500); // write to com1 1 byte 500ms time out
}
else
{
str_cpy((debug.pstring)[0], "Port is Busy");
port_close(hPort);
while(proc_status(port_close)) wait(1);
//
//
while(!key_any){wait(1); }
sys_exit("");
}
wait(-1);
if(port_read_status(hPort))
{
port_read_bytes(hPort, received_data, 2); // read data from com1 per 1 byte
}
//
while(1)
{
str_cpy((debug.pstring)[0], "Port Handle = ");
str_cat((debug.pstring)[0], _chr(str_for_int(NULL,hPort)));
str_cpy((debug.pstring)[1], "Data Sent = ");
str_cat((debug.pstring)[1], _chr(str_for_int(NULL,temp)));
str_cpy((debug.pstring)[2], "Data Read [0]= ");
str_cat((debug.pstring)[2], _chr(str_for_int(NULL,(long)received_data[0])));
str_cpy((debug.pstring)[3], "Data Read [1]= ");
str_cat((debug.pstring)[3], _chr(str_for_int(NULL,(long)received_data[1])));
if(key_esc) break;
wait(1);
}
port_close(hPort);
while(proc_status(port_close)) wait(1);
//
//
sys_exit("");
}
The result became more confusing, 1. the port sometimes is opened successfully, sometimes don't. 2. when press ecs key to end, sometimes the script crashes, sometimes don't. result 1 result 2 Any advise will be much appreciated.
Last edited by qoolelee; 01/13/10 05:14.
|
|
|
Re: Serial Communications Header
[Re: qoolelee]
#305143
01/13/10 05:37
01/13/10 05:37
|
Joined: Dec 2007
Posts: 5
qoolelee
Newbie
|
Newbie
Joined: Dec 2007
Posts: 5
|
Another test with my little mcu device, if send "0xFF T1 T2" 3 byte command, the device will return "0xFF T1 T2" immediantly. Though the "Data send =" item on PC end still show the huge number but the LED digits on the mcu device show me it received the command and the T1 T2 bytes and number correctly. But on pc end it seems there's no return from it for me to show it on the computer window. If I comment out the newly added "while(proc_status(port_close)) wait(1);" line it stop crashed.
#include <acknex.h>
#include <default.c>
#include "Port_IO.h"
TEXT* debug = { pos_x=20; pos_y=20; size_y=400; font="Arial#20b"; strings=20; flags=SHOW + WWRAP; }
var hPort;
var temp;
var temp2;
byte sent_data[3] = {0xFF, 0x10, 0x00};
byte received_data[3] = {0x00, 0x00, 0x00};
function main()
{
wait(1); level_load(NULL); wait(1); diag("\n\n\n");
on_esc = NULL;
//
//
hPort = port_open_readwrite("COM1", "2400,n,8,1");
wait(-1);
if(port_read_status(hPort))
{
temp = port_write_bytes(hPort, sent_data, 3, 500); // write to com1 1 byte 500ms time out
}
else
{
str_cpy((debug.pstring)[0], "Port is Busy");
port_close(hPort);
while(proc_status(port_close)) wait(1);
//
//
while(!key_any){wait(1); }
sys_exit("");
}
wait(-1);
while(!port_read_status(hPort)){ wait(1);}
if(port_read_status(hPort))
{
port_read_bytes(hPort, received_data, 3); // read data from com1 3 byte
}
//
while(1)
{
str_cpy((debug.pstring)[0], "Port Handle = ");
str_cat((debug.pstring)[0], _chr(str_for_int(NULL,hPort)));
str_cpy((debug.pstring)[1], "Data Sent = ");
str_cat((debug.pstring)[1], _chr(str_for_int(NULL,temp)));
str_cpy((debug.pstring)[2], "Data Read [0]= ");
str_cat((debug.pstring)[2], _chr(str_for_int(NULL,(long)received_data[0])));
str_cpy((debug.pstring)[3], "Data Read [1]= ");
str_cat((debug.pstring)[3], _chr(str_for_int(NULL,(long)received_data[1])));
str_cpy((debug.pstring)[4], "Data Read [2]= ");
str_cat((debug.pstring)[4], _chr(str_for_int(NULL,(long)received_data[2])));
if(key_esc) break;
wait(1);
}
port_close(hPort);
// while(proc_status(port_close)) wait(1);
//
//
sys_exit("");
}
For your reference. Thanks.
Last edited by qoolelee; 01/13/10 05:55.
|
|
|
Re: Serial Communications Header
[Re: qoolelee]
#305144
01/13/10 06:15
01/13/10 06:15
|
Joined: Feb 2008
Posts: 3,232 Australia
EvilSOB
OP
Expert
|
OP
Expert
Joined: Feb 2008
Posts: 3,232
Australia
|
OK, Im having a think on this, but I need to refresh myself with how my code works first, so give me time... maybe a couple of days...
Firstly, to try and remove the random "crash on exit issue", try putting "on_esc=NULL;" at the beginning of main, and re-enable my while(proc_status... line. default.c has an on_esc function that almost-instant-closes. I think it is making the code skip some (if not all) of the port_close stages, thereby leaving the port open and crashing.
From memory, I have not (intentionally) limited the COM port numbers to 8, but if I find something during my memory-refreshing process, I'll take it out and let you know...
"There is no fate but what WE make." - CEO Cyberdyne Systems Corp. A8.30.5 Commercial
|
|
|
Re: Serial Communications Header
[Re: EvilSOB]
#305147
01/13/10 07:20
01/13/10 07:20
|
Joined: Dec 2007
Posts: 5
qoolelee
Newbie
|
Newbie
Joined: Dec 2007
Posts: 5
|
Really appreciate your quick response. I must apologize to you, it's my fault, after checking my rusty mcu device. I found some glitch in my firmware. After re-write and upload to the chip, it works in both reading and writing with your header. Except that on the PC end the port_write_bytes still returns a huge number. Thank you for your great contribution and your continuous attention on this thread, I think I own you a beer. Wish you a best day.
|
|
|
Re: Serial Communications Header
[Re: qoolelee]
#305308
01/14/10 05:05
01/14/10 05:05
|
Joined: Feb 2008
Posts: 3,232 Australia
EvilSOB
OP
Expert
|
OP
Expert
Joined: Feb 2008
Posts: 3,232
Australia
|
Hehe... been there, done that, myself. Pulling the wrong MCU from the shelf sucks, thats why I toss or erase all out-of-date mcu's now.
I'll have a look at the port-write-bytes again because I can vaguely remember that being a bug I had for a while. I thought I had fixed it but I may be wrong... I dont remember if I tested it with an MCU, only with hyperterminal.
"There is no fate but what WE make." - CEO Cyberdyne Systems Corp. A8.30.5 Commercial
|
|
|
Re: Serial Communications Header
[Re: EvilSOB]
#305824
01/17/10 15:22
01/17/10 15:22
|
Joined: Oct 2008
Posts: 15 Montevideo, Uruguay
charrua
Newbie
|
Newbie
Joined: Oct 2008
Posts: 15
Montevideo, Uruguay
|
nice to see that i were not the only one interested in serial communications, my first post was on this subject about 2 years, in that time i posted code to do serial communications with liteC but on these days seems that few people were working on this topic (seen by many but only few posts). is good to see that serial communications are still alive!
i mantain my offer so if some needs some help, here i'm. (edit: sorry, i should say: i'm back, i been disconected from this forum for a while, best regards) (sorry my bad english)
Juan
Last edited by charrua; 01/17/10 19:43.
|
|
|
Re: Serial Communications Header
[Re: druid]
#324532
05/21/10 14:40
05/21/10 14:40
|
Joined: Feb 2008
Posts: 3,232 Australia
EvilSOB
OP
Expert
|
OP
Expert
Joined: Feb 2008
Posts: 3,232
Australia
|
Ive just done a bit of research, and it SHOULD work for COM1 right through to COM256
The core API functions should allow this, according to several on-line sources.
But I dont have a USB device that will let me 'set' it above COM8 to test with ATM. Im gonna try and beg/borrow/steal one in the next few days and I'll post my results...
FYI :: be certain you are trying to access the ports with COM in capital letters, and be sure there is no trailling "." ie "COM25" is correct, but NOT "com25" or "COM25."
"There is no fate but what WE make." - CEO Cyberdyne Systems Corp. A8.30.5 Commercial
|
|
|
Re: Serial Communications Header
[Re: EvilSOB]
#324757
05/23/10 03:13
05/23/10 03:13
|
Joined: Feb 2008
Posts: 3,232 Australia
EvilSOB
OP
Expert
|
OP
Expert
Joined: Feb 2008
Posts: 3,232
Australia
|
Bug Found and fixed. Thanks for that druid. The top post of this thread has been corrected. Read the dark-green [EDIT] up there for explaination.
[WARNING :: TECHNICAL CONTENT] Serial ports COM1 -> COM9 'appear' to be dos-legacy names, whereas COM10 -> COM256 are not. So when accessing the COM10 and above you NEED to lead the port name with the 'root-path' accessor "\\.\" (eg \\.\COM25 ), but when accessing ports 1->9 it is assumed by the OS. My code now adds this accessor automatically, so you dont NEED to know this, Im just giving this infomation out for the curious...
"There is no fate but what WE make." - CEO Cyberdyne Systems Corp. A8.30.5 Commercial
|
|
|
|