【问题标题】:Default settings for IrDA discovery (baud, bits, stop, parity, flow control, etc)IrDA 发现的默认设置(波特、位、停止、奇偶校验、流控制等)
【发布时间】:2024-01-15 03:11:01
【问题描述】:

我正在尝试编写一个使用 IrDA 与 Uwatec 潜水电脑通信的工具……在 Mac 上。我正在使用的 USB IrDA 设备提供了一个可用于发送和接收数据的串行设备(/dev/cu.IrDA-IrCOMM0/dev/tty.IrDA-IrCOMM0)。不幸的是,Mac 不提供 IrDA 套接字层。

我已使用设备驱动程序附带的命令行工具确认它可以侦听和接收来自其他设备的 IrDA 通信。然而,虽然命令行工具告诉我它正在以 9600 波特率进行通信,但其余设置(位、停止位、奇偶校验、流控制等)并未返回给我。

我尝试编写自己的程序来监听数据,但它无法接收任何数据,我认为原因是这些设置不正确。那么,假设我只是想收听正在发送的 9600 波特 IrDA 发现数据包,我还需要使用哪些其他设置?

如果有帮助,这是我目前用来设置通信参数的代码的 sn-p —— 这不起作用:

#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;
}

【问题讨论】:

    标签: c macos serial-port infrared


    【解决方案1】:

    下面是我在 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;
    }
    

    【讨论】: