我正在尝试编写一个工具,使用IrDA在Mac上与Uwatec潜水计算机通信。我使用的USB IrDA设备提供了一个串行设备(/dev/cu.IrDA-IrCOMM0/dev/tty.IrDA-IrCOMM0),可用于发送和接收数据。不幸的是,Mac不提供IrDA套接字层。
我已经确认使用设备驱动程序附带的命令行工具,它可以监听和接收来自其他设备的IrDA通信。然而,尽管命令行工具告诉我它正在以9600波特的速度通信,但其余的设置(位、停止位、奇偶校验、流量控制等)并没有返回给我。
我试过编写自己的程序来监听数据,但它无法接收任何数据,我相信原因是这些设置不正确。所以,假设我只是想听听正在发送的9600波特的IrDA发现包,我还需要使用哪些设置?
如果有帮助的话,下面是我目前用来设置通信参数的代码片段——它不起作用:

#define DEVICE "/dev/cu.IrDA-IrCOMM0"

int main(void) {
    FILE *device;
    struct termios ttystate;

    device = fopen(DEVICE, "rw");

    //get the terminal state
    tcgetattr(fileno(device), &ttystate);
    //turn off canonical mode and echo
    ttystate.c_lflag &= ~(ICANON | ECHO);
    //minimum of number input read.
    ttystate.c_cc[VMIN] = 1;

    cfsetspeed(&ttystate, B9600); // Set 9600 baud····
    ttystate.c_cflag |= (CS8 | // Use 8 bit words
                         PARENB   | // parity enable
                         PARODD   | // odd parity
                         CCTS_OFLOW | // CTS flow control of output
                         CRTS_IFLOW);// RTS flow control of input

    //set the terminal attributes.
    tcsetattr(fileno(device), TCSANOW, &ttystate);
    return EXIT_SUCCESS;
}

最佳答案

下面是我在Linux上用于EXAR XR17C15的IrDA初始化代码。
另外,您必须将波特率设置为本例中未设置的波特率。
我希望这能有帮助。

//
// Set up IrDA hardware interface through UART

bool CeIrDA::SetupIrDA()
{
    struct termios termios;
    tcgetattr(hComPort, &termios);
    cfmakeraw(&termios);

    termios.c_iflag = IGNPAR;
    termios.c_oflag = 0;
    termios.c_cc[VTIME] = 1;    // timeout in 0.1s between 2 characters
    termios.c_cc[VMIN] = 1;     // min # of characters

    tcsetattr(hComPort, TCSANOW, &termios);

    xrioctl_rw_reg input;
    struct timespec delay = {0, 500};
    struct timespec delayrem;
    //EFR: Allowing Enhanced Functions and RTS/DTR flow control
    input.reg=0x09;
    input.regvalue=0x50;
    ioctl(hComPort,WRITE,&input);

    nanosleep(&delay, &delayrem);

    //MCR: Flow control RTS enabled
    input.reg=0x04;
    input.regvalue=0x40;
    ioctl(hComPort,WRITE,&input);

    nanosleep(&delay, &delayrem);

    //MCR: RTS pin high
    input.reg=0x04;
    input.regvalue=0x00;
    ioctl(hComPort,WRITE,&input);

    nanosleep(&delay, &delayrem);

    //MCR: RTS pin low
    input.reg=0x04;
    input.regvalue=0x02;
    ioctl(hComPort,WRITE,&input);

    nanosleep(&delay, &delayrem);

    //MCR: Infrared Mode
    input.reg=0x04;
    input.regvalue=0x42;
    ioctl(hComPort,WRITE,&input);

    nanosleep(&delay, &delayrem);

    //EFR: Allowing Enhanced Functions and RTS/DTR flow control
    input.reg=0x09;
    input.regvalue=0x40;
    ioctl(hComPort,WRITE,&input);

    nanosleep(&delay, &delayrem);

    //LCR: Allowing changes of DLL and DLM
    input.reg=0x03;
    input.regvalue=0x80;
    ioctl(hComPort,WRITE,&input);

    nanosleep(&delay, &delayrem);

    //DLL: Speed configuration
    input.reg=0x00;
    input.regvalue=0x02;
    ioctl(hComPort,WRITE,&input);

    nanosleep(&delay, &delayrem);

    //DLM: Speed configuration
    input.reg=0x01;
    input.regvalue=0x00;
    ioctl(hComPort,WRITE,&input);

    nanosleep(&delay, &delayrem);

    //LCR: configuration for parity, word length....
    input.reg=0x03;
    input.regvalue=0x03;
    ioctl(hComPort,WRITE,&input);

    nanosleep(&delay, &delayrem);
    return true;
}

07-24 12:58