Now I've a modified crosslink (attached) which take argument and write
to rtser0.
I know it's ugly. But it's working and that's always good ;)

Anyhow. From what I've heard it will be inefficient if my java code
would call "/usr/src/xenomai/example/crosslink string" even time a
string should be written to serial.

And as it looks now I can't even run the binary as user else than root
(since it starts a xenmai thread?).

How can I as a 'user' open a device, write to it and close it without
keeping starting processes or threads every time?

I would like to be able to do something like:
file =  open("/dev/myTestSerialDevice");
while (...)
file.write(...)
and finally close
file.close() when there's no more to write.

/Bachman

On 10/09/2007, Jan Kiszka <[EMAIL PROTECTED]> wrote:
> Bachman Kharazmi wrote:
> > A guy recommended me to try to get things going with the working
> > mobo-serialport first before playing with linux drivers and serial
> > controllers.. probabily a good point to start at.
>
> For sure. You may also test against a different PC running Linux or a
> Windoz box with terminal programs installed, BTW.
>
> >
> > Anyhow I tried to used your cross-link.c to write ASCII to serialport
> > rtser0. I connected the cable to the dc motor drive which accepts
> > ASCII commands with CR(\r\n) at the end of every command line.
> > The light version of your code that I was testing with looks like:
> > http://pastebin.ca/690561
>
> <Quick glance mode> You write strings of different length in lines 152
> and 153, but you pass the same size (sz = sizeof(RTIME), ie. 8).
>
> >
> > I'm trying to write two simple commands in the example, but I couldn't
> > get it working. Don't know why :/
> >
> > First when I got that working it would be even nicer to be able to
> > give a argument to the binary which is a ASCII command(string) written
> > to rtser0.
>
> Go wild and write such a trivial, command line driver tool to test
> serial ports! Would be highly appreciated, and quickly accepted into the
> tree (if it's clean).
>
> Jan
>
>
>
/*
 * cross-link.c
 *
 * Userspace test program (Xenomai native skin) for RTDM-based UART drivers
 * Copyright 2005 by Joerg Langenberg <[EMAIL PROTECTED]>
 *
 * Updates by Jan Kiszka <[EMAIL PROTECTED]>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <sys/mman.h>
#include <string.h>
#include <stdlib.h>
#include <native/task.h>
#include <native/timer.h>

#include <rtdm/rtserial.h>

#define MAIN_PREFIX   "main : "
#define WTASK_PREFIX  "write_task: "
//#define RTASK_PREFIX  "read_task: "

#define WRITE_FILE    "rtser0"

int write_fd = -1;

#define STATE_FILE_OPENED         1
#define STATE_TASK_CREATED        2

unsigned int write_state = 0;

/*                           --s-ms-us-ns */
RTIME write_task_period_ns =    100000000llu;
RT_TASK write_task;

static const struct rtser_config read_config = {
	.config_mask       = 0xFFFF,
	.baud_rate         = 115200,
	.parity            = RTSER_DEF_PARITY,
	.data_bits         = RTSER_DEF_BITS,
	.stop_bits         = RTSER_DEF_STOPB,
	.handshake         = RTSER_DEF_HAND,
	.fifo_depth        = RTSER_DEF_FIFO_DEPTH,
	.rx_timeout        = RTSER_DEF_TIMEOUT,
	.tx_timeout        = RTSER_DEF_TIMEOUT,
	.event_timeout     = 1000000000, /* 1 s */
	.timestamp_history = RTSER_RX_TIMESTAMP_HISTORY,
	.event_mask        = RTSER_EVENT_RXPEND,
};

static const struct rtser_config write_config = {
	.config_mask       = RTSER_SET_BAUD | RTSER_SET_TIMESTAMP_HISTORY,
	.baud_rate         = 115200,
	.timestamp_history = RTSER_DEF_TIMESTAMP_HISTORY,
	/* the rest implicitely remains default */
};

static int close_file( int fd, char *name)
{
	int err, i=0;
	
	do {
		i++;
		err = rt_dev_close(fd);
		switch (err) {
		case -EAGAIN:
			printf(MAIN_PREFIX "%s -> EAGAIN (%d times)\n",
			       name, i);
			rt_task_sleep(50000); /* wait 50us */
			break;
		case 0:
			printf(MAIN_PREFIX "%s -> closed\n", name);
			break;
		default:
			printf(MAIN_PREFIX "%s -> %s\n", name,
			       strerror(-err));
			break;
		}
	} while (err == -EAGAIN && i < 10);

	return err;
}

void cleanup_all(void)
{
	if (write_state & STATE_FILE_OPENED) {
		close_file(write_fd, WRITE_FILE " (write)");
		write_state &= ~STATE_FILE_OPENED;
	}
	
	if (write_state & STATE_TASK_CREATED) {
		printf(MAIN_PREFIX "delete write_task\n");
		rt_task_delete(&write_task);
		write_state &= ~STATE_TASK_CREATED;
	}
	
}

void catch_signal(int sig)
{
	cleanup_all();
	printf(MAIN_PREFIX "exit\n");
	return;
}

//void write_task_proc(void *arg){
void write_task_proc(void *arg){
char* myString1=arg;
//printf("%s\n",myString1);
	int err;
//	RTIME write_time;
	int write_time;
	ssize_t sz = sizeof(RTIME);
	ssize_t written = 0;
	int counter=0;

	err = rt_task_set_periodic(NULL, TM_NOW,
				   rt_timer_ns2ticks(write_task_period_ns));
	if (err) {
		printf(WTASK_PREFIX "error on set periodic, %s\n",
		       strerror(-err));
		goto exit_write_task;
	}

	while (counter==0) {
		err = rt_task_wait_period(NULL);
		if (err) {
			printf(WTASK_PREFIX
			       "error on rt_task_wait_period, %s\n",
			       strerror(-err));
			break;
		}
	
		//write_time = rt_timer_read();
		//write_time = 1;
		
	//	char *mystringa = "EN\r\n";
	//	char *mystringb = "V0\r\n";
		//int test=3;
		written = rt_dev_write(write_fd, myString1, strlen(myString1));
//		written = rt_dev_write(write_fd, mystringb, strlen(mystringb));
		if (written < 0 ) {
			printf(WTASK_PREFIX "error on rt_dev_write, %s\n",
			       strerror(-err));
			break;
		} else if (written != sz) {
//			printf(WTASK_PREFIX "only %d / %d byte transmitted\n",
//			       written, sz);
			break;
		}
	counter++;
	}
	exit(0);
 exit_write_task:
	if ((write_state & STATE_FILE_OPENED) &&
	    close_file(write_fd, WRITE_FILE " (write)") == 0)
		write_state &= ~STATE_FILE_OPENED;

//	printf(WTASK_PREFIX "exit\n");
}


int main(int argc , char *argv[]){
	int err = 0;
        //printf("%s\n",argv[1]);
	char *myString = argv[1];
	//printf("%s\n",myString);

//printf("%s\n",myString);
//char finalString = strcat( char *myString, char *cr );
char *finalString = (char*) malloc(sizeof(char)*(strlen(myString)+3));
sprintf(finalString, "%s\r\n", myString);
//
	signal(SIGTERM, catch_signal);
	signal(SIGINT, catch_signal);

	/* no memory-swapping for this programm */
	mlockall(MCL_CURRENT | MCL_FUTURE);

	/* open rtser0 */
	write_fd = rt_dev_open( WRITE_FILE, 0);
	if (write_fd < 0) {
		printf(MAIN_PREFIX "can't open %s (write), %s\n", WRITE_FILE,
		       strerror(-write_fd));
		goto error;
	}
	write_state |= STATE_FILE_OPENED;
//	printf(MAIN_PREFIX "write-file opened\n");

	/* writing write-config */
	err = rt_dev_ioctl(write_fd, RTSER_RTIOC_SET_CONFIG, &write_config);
	if (err) {
		printf(MAIN_PREFIX "error while RTSER_RTIOC_SET_CONFIG, %s\n",
		       strerror(-err));
		goto error;
	}
//	printf(MAIN_PREFIX "write-config written\n");

	/* create write_task */
	err = rt_task_create(&write_task, "write_task", 0, 50, 0);
	if (err) {
		printf(MAIN_PREFIX "failed to create write_task, %s\n",
		       strerror(-err));
		goto error;
	}
	write_state |= STATE_TASK_CREATED;
//	printf(MAIN_PREFIX "write-task created\n");

	/* start write_task */
//	printf(MAIN_PREFIX "starting write-task\n");
	//err = rt_task_start(&write_task, &write_task_proc, NULL);
	err = rt_task_start(&write_task, &write_task_proc, finalString);	

	if (err) {
		printf(MAIN_PREFIX "failed to start write_task, %s\n",
		       strerror(-err));
		goto error;
	}

	pause();
	return 0;

 error:
	cleanup_all();
	return err;
}
_______________________________________________
Xenomai-help mailing list
[email protected]
https://mail.gna.org/listinfo/xenomai-help

Reply via email to