Accessing serial port while player is running?

Posted by: rexkp

Accessing serial port while player is running? - 20/03/2002 20:15

I having trouble hitting the serial port while the player is running. I want to change the port speed and other attributes.

The code works fine if the player isn't running but I get weird results (not consistent) when the player is running.

Is there a simple way to stop the player from messing with the port or for a program to lock out the player from the port? I have tried the -s- option but it didn't help.

I need to hit the port for gps, obdii and accelerometer apps.

Cheers,

Rex.
Posted by: TheAmigo

Re: Accessing serial port while player is running? - 21/03/2002 21:02

Check out this thread which has a link to TINI which sounds like it would do what you want.
Posted by: kimbotha

Re: Accessing serial port while player is running? - 21/03/2002 22:44

I think the idea is to be able to use the built in serial port first... why have to buy a new device when there is a built in serial port waiting to be used...?

Cheers

Kim
Posted by: TheAmigo

Re: Accessing serial port while player is running? - 22/03/2002 10:38

The original poster mentioned three things that he wanted to plug in to the serial port. With the existing hardware, that's not possible. Some sort of terminal server is needed provide ports to plug in multiple devices. As long as you're adding extra serial ports, why not just the leave the existing one as is... less work that way.
Posted by: Shonky

Re: Accessing serial port while player is running? - 27/04/2002 21:35

Hi Rex,

Did you end up figuring this out?
Posted by: rexkp

Re: Accessing serial port while player is running? - 28/04/2002 06:25

Partly. I did end up getting the -s- option to work. In order to use the stock init I renamed the player and have a tiny executable that launches it. Causes a Hijack error but other wise works.

Cheers,

Rex.
Posted by: Shonky

Re: Accessing serial port while player is running? - 28/04/2002 06:47

My problem is more that everything is echoed back by the terminal. I have kludged it a bit by using stty -echo but at the moment I'm only able to do it via the actual serial port console - which means I can't automate on boot up. What exactly does the -s- option do? I was under the impression it stopped messages from the player software but mine still seems to spit them out.
Posted by: rexkp

Re: Accessing serial port while player is running? - 28/04/2002 08:14


I think the -s- option only stops the player from reading the serial port, there may still be some output.

It hasn't always worked for me though, may be it was broken in one of the recent betas or perhaps I was doing something wrong. 2.0-b11 works for me.

Perhaps this could be addressed better with a kernel hack?

Cheers,

Rex.
Posted by: Shonky

Re: Accessing serial port while player is running? - 28/04/2002 17:58

Rex,

Would you mind showing me your source for your GPS code reader since it seems to be working? Alternatively could you give my code below a quick scan and see if there is anything glaringly obvious? I basically ripped most of this code from my original x86 based MP3 player which read an IR remote ontroller so I am fairly confident it's OK.

Basically my code is either receiving nothing (I know data is definitely going in to the port) or receiving zeroes. I have tried t at a standard baud rate to rule out baud rate problems with my custom kernel.

Also I see how the -s- switch is working now and it would appear that I need it. However it made no difference.

I also needed to turn of terminal echo and that seems to be working fine for the moment. My code is working in the sending direction but not in the recieving. It appears that something else is swallowing my received data.

I do plan on a kernel hack (basically using a lot of Hijack for screen functions) eventually but I am trying to run the code in userspace initially for testing.

Thanks,

My code:


int main(int argc, char *argv[])
{
unsigned char c, queue[32];
struct termios options;
struct timeval timeout;
fd_set fds;

printf("Opening serial port...\n");
if ((changer_handle = open("/dev/ttyS1", O_RDWR | O_NOCTTY | O_NDELAY)) < 0) {
fprintf(stderr, "Couldn't get serial port.\n");
exit(1);
}
printf("Serial port open.\n");


printf("Getting Serial options\n");
/* Get the current options for the port... */
tcgetattr(changer_handle, &options);

/* Set the baud rates to 2400... (actually 1500) */
cfsetispeed(&options, B2400);
cfsetospeed(&options, B2400);

/* Enable the receiver and set local mode... */
options.c_cflag |= (CLOCAL | CREAD);

options.c_cflag &= ~CSIZE; /* Mask the character size bits */
options.c_cflag |= CS8; /* Select 8 data bits */

/* No parity (8N1): */
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;

/* No flow control */
options.c_cflag &= ~CRTSCTS;

/* Set the new options for the port... */
tcsetattr(changer_handle, TCSAFLUSH, &options);
printf("Setting Serial options\n");

/* Send a hello command - expect 9 8 2 back */
queue[0] = 0x1;
queue[1] = 0x8;
queue[2] = 0xA;
send_mbus(queue, 3);

usleep(1000000);

/* Make the set */
FD_ZERO(&fds);
FD_SET(changer_handle, &fds);

/* Make the timeout */
timeout.tv_sec=3;
timeout.tv_usec=0;

while(1) {
/* Do a select on the changer input */
if (select(changer_handle + 1, &fds, NULL, NULL, &timeout) > 0) {

if (read(changer_handle, &c, 1)) {
printf("%02x", c);
}
} else {
break;
}
}

cfsetispeed(&options, B115200);
cfsetospeed(&options, B115200);
tcsetattr(changer_handle, TCSAFLUSH, &options);

close(changer_handle);
}
Posted by: rexkp

Re: Accessing serial port while player is running? - 01/05/2002 07:24

At first glance we are doing pretty much the same thing. My code assumes the port has already been set to 8N1 and just tweaks the settings needed:

(The BBS messes with the alignment here but you get the idea.)

//Opens the serial port. Returns true on success.
bool open_serial()
{
struct termios attribs;

handle_serial = open(EMPEG_GPS_SERIAL_PORT, O_RDWR | O_NOCTTY ); //| O_EXCL

if(handle_serial == -1)
{
printf("open() of port %s failed.", EMPEG_GPS_SERIAL_PORT);
return(false);
}

tcdrain(handle_serial);

if(tcgetattr(handle_serial,&attribs)<0)
{
printf("tcsetattr failed.");
return(false);
}


/* Save off the original port settings.
*/
serial_orig_c_lflag = attribs.c_lflag;
serial_orig_speed = cfgetospeed(&attribs);

/* Set new mode and speed.
*/
attribs.c_lflag &= ~ICANON;
cfsetospeed(&attribs, B4800);

tcdrain(handle_serial);
if(tcsetattr(handle_serial, TCSAFLUSH, &attribs)<0)
{
printf("tcsetattr failed.");
return(false);
}
return(true);
}

Clearing ICANON isn't needed for the GPS code but is for the OBDII code which is related.

Reading is done with a select() and read(), much like you do.

Cheers,

Rex.
Posted by: Shonky

Re: Accessing serial port while player is running? - 01/05/2002 07:37

Hugo pointed me towards cfmakeraw() (here) which simply clears a lot of special flags and sets a few. I haven't worked out which actual flag I need to change yet, but it works now.

It does clear ICANON though which was one I hadn't cleared so perhaps that is it. I might have a go based on your code and see if that makes much difference.

Thanks,