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

Code:
#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. laugh



Last edited by qoolelee; 01/13/10 05:14.