Serial programming woes

Posted by: tonyc

Serial programming woes - 14/07/2010 21:53

I've got an XM receiver installed in my car now, and I'm trying to control it via the serial port. Unfortunately, though I can see serial data coming from the receiver on my laptop, and can control the receiver using software on my laptop, I can't get the XM receiver to communicate successfully with my empeg via the serial port, even using the same code that works on my laptop.

The receiver is 9600 baud, N81, no flow control. I've set car_rate to 9600 in config.ini, and set Hijack (v500) to "Apps use the serial port." Still, no luck.

Here is a stripped-down test program that works on my Mac laptop (sees and prints data coming from the XM receiver) but doesn't on the empeg:

Code:
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>

#define BAUDRATE B9600
#ifdef MAC
#define SERIALDEV "/dev/tty.usbserial"
#else
#define SERIALDEV "/dev/ttS1"
#endif
 
int main(int argc,char** argv)
{
    struct termios termios;
    struct termios stdio;
    int serial_fd;
    fd_set rdset;
 
    unsigned char c='\0';

    fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);

    memset(&termios,0,sizeof(termios));
    termios.c_iflag=0;
    termios.c_oflag=0;
    termios.c_cflag=CS8|CREAD|CLOCAL;
    termios.c_lflag=0;
    termios.c_cc[VMIN]=1;
    termios.c_cc[VTIME]=5;

    serial_fd = open(SERIALDEV, O_NOCTTY | O_RDWR | O_NONBLOCK);
    if (serial_fd < 0)
    {
        fprintf(stderr, "can't open fd");
    }
    cfsetospeed(&termios,BAUDRATE);
    cfsetispeed(&termios,BAUDRATE);
 
    tcsetattr(serial_fd,TCSANOW,&termios);

    while (c!='q')
    {
        if (read(serial_fd,&c,1)>0)        write(STDOUT_FILENO,&c,1);
        if (read(STDIN_FILENO,&c,1)>0)  write(serial_fd,&c,1);
    }
 
    close(serial_fd);
}


Now, I've found some threads indicating there are some termios flags you have to mess with in some cases, and I've tried some of those, but still can't get anything to work.

Here are the combinations that work:
serialtest program on laptop can read data from XM receiver
serialtest program on laptop can talk to serialtest program on empeg (and vice versa)

The one combination that isn't working is serialtest program on empeg being able to see anything from the XM receiver when it's plugged in.

Any ideas what to try?
Posted by: mlord

Re: Serial programming woes - 14/07/2010 23:30

Triple check rx/tx wires on the cable -- possibly reversed.
Posted by: tonyc

Re: Serial programming woes - 14/07/2010 23:44

I'll check, but if it were a case of a bad cable, wouldn't one of the combinations above not work? My laptop can see data from the receiver, and my laptop can communicate over serial with my empeg. That means the cable attached to the empeg and the cable coming from the XM receiver both work, except when connected to each other. Wouldn't that seem to rule out one of the cables being wired wrong?
Posted by: mlord

Re: Serial programming woes - 15/07/2010 00:14

I just suspect confusion somewhere in there about the need or lack of need for a crossover in the cable.
Posted by: mlord

Re: Serial programming woes - 15/07/2010 00:15

Eg. if the exact same serial cable is used, and works, between PC and radio, and between PC and empeg, then a _different_ cable will definitely be needed when connecting empeg and radio.
Posted by: tonyc

Re: Serial programming woes - 15/07/2010 00:17

Well, I verified that the M-M DB-9 adapter I was using to connect my empeg cable to my XM receiver is straight-through. I'm pretty sure the cable going to the empeg is straight-through as well, but testing it would require about half an hour of dash reconstruction to reach the back of the sled. To double-check, I guess I could use a null-modem cable to "re-cross" the pins back again and see if that works, but I don't have enough gender-changing adaptors to make that adventure possible right now.
Posted by: Shonky

Re: Serial programming woes - 15/07/2010 00:26

Maybe, maybe not.

Are you connecting to the empeg with the laptop using the same cable?
Are you connecting to the empeg with the laptop via the sled connector?
Not enough info there to be sure.

The empeg sled connector is (in my opinion at least) wired incorrectly. TX and RX are set up to act as a DCE (like a modem) rather than a DTE (like a PC) but the rest of the serial port is wired as a DTE. BUT the serial port on the unit itself is wired like a DTE which means you need the null cable.

Soooo if you're using the same cables for both sled connection and direct empeg connection it won't work.

See also my post here:
http://empegbbs.com/ubbthreads.php/ubb/showthreaded/Number/116302/site_id/1#import

So effectively you need to use a null modem in all likely situations (i.e. PC direct to empeg unit and empeg sled direct to a device like your XM radio).

So as Mark says, check RX/TX smile

Always handy to have something like the picture for serial debugging. It's immediately obvious which signals are being driven. It's something I knocked up at my older employer since we did a lot of serial port interfacing. Can't seem to find anything similar online. For a once off it's not worth it but always good to have around.
Posted by: tonyc

Re: Serial programming woes - 15/07/2010 00:53

Yeah, you guys are probably right -- I'll pick up a null-modem adapter and see if that solves the problem.
Posted by: tonyc

Re: Serial programming woes - 15/07/2010 16:47

Eeeeeyup. Adding a null-modem adapter works.

Than's guys -- Occam's razor strikes again.
Posted by: tfabris

Re: Serial programming woes - 15/07/2010 21:18

To make it even more confusing: The port on the empeg's casing is opposite of the port on the Empeg's sled. smile

Glad you got it working. If anyone can get us a successful XM controller with user interface, it'd be you or Mark. I wish you the best of luck with that project!

Gonna build it into Emphatic, or make it a separate app?
Posted by: tonyc

Re: Serial programming woes - 16/07/2010 14:46

Quote:
Gonna build it into Emphatic, or make it a separate app?

Well, I started off thinking I'd add it to emphatic, but that code is basically unrecognizable to me at this point, and it always had some weird timing bugs and performance problems that annoyed me. So, I started this XM thing off from scratch.

Right now I've got it turning on the receiver and changing channels, but I don't have it displaying artist/title info yet. Once the XM stuff is working, I might try to make it more of a comprehensive player front-end and release it as emphatic. But, we'll see how this goes first.
Posted by: wfaulk

Re: Serial programming woes - 19/07/2010 19:07

I still have a commercial RS232 adapter hanging around that pulled out the pertinent pins to LEDs and provided a few switches for you to make the proper changes while it was all hooked up. That thing was a godsend back in the day. I even got to the point where I could usually tell what the data rate was based on the blinking, or, failing that, what the line noise looked like.
Posted by: Shonky

Re: Serial programming woes - 19/07/2010 21:36

Yep. I could often work out in seconds what would take ages to sort out with oscilloscopes etc. I know what you mean about baud rates too - you could at least get a basic idea if it was right.

It also had a snoop function (TX, RX or both if half duplex), null modem, RS232D (RJ45) all in one as well as the pass through. About the only thing it couldn't do was RS232D to RS232D but simply two of them joined did the trick then.

Otherwise called a "Christmas tree"...