【问题标题】:Windows Error 87 when raising DTR line (serial)提升 DTR 行时出现 Windows 错误 87(串行)
【发布时间】:2016-05-18 18:32:22
【问题描述】:

我正在尝试通过 TTL->RS-232 转换器与串行设备通信。此转换器需要在 DTR 和 RTS 线路上供电。

// imports
extern crate serial; //see: https://dcuddeback.github.io/serial-rs/serial/index.html
use std::env;
use std::io;
use std::time::Duration;
use std::process::exit;
use std::io::prelude::*;
use serial::prelude::*;
use serial::{BaudRate, CharSize, Parity, StopBits, FlowControl, SystemPort, PortSettings, Error};
use std::mem;

#[inline]
fn display_error_message(err: &Error, msg: &str) -> ! {
    println!("{}", msg);
    println!("Error Code: {:?}", err.raw_os_error());
    println!("Error:  {:?}", err);
    exit(0);
}

fn main() {
    // open port
    let mut port = match serial::open("COM3") {
        Err(ref e) => display_error_message(e, "Error opening serial port"),
        Ok(x) => x,
    };
    // push settings to stack
    let settings = PortSettings {
        baud_rate: BaudRate::Baud115200,
        char_size: CharSize::Bits8,
        parity: Parity::ParityNone,
        stop_bits: StopBits::Stop1,
        flow_control: FlowControl::FlowNone,
    };
    // configure port
    match port.configure(&settings) {
        Ok(_) => {}
        Err(ref e) => display_error_message(e, "Error configuring serial port."),
    };
    // set rts
    match port.set_rts(true) {
        Ok(_) => {}
        Err(ref e) => display_error_message(e, "Error setting RTS line"),
    };
    // set DTR
    match port.set_dtr(true) {
        Ok(_) => {}
        Err(ref e) => display_error_message(e, "Error setting DTR line"),
    };
    // allocate readbuffer on stack
    let mut rb: [u8; 1] = [0u8; 1];
    // allocate buffer to hold output
    let mut out: String = String::with_capacity(1024);
    // loop while reading
    loop {
        match port.read(&mut rb) {
            Ok(_) => {}
            Err(ref e) => {
                println!("Error reading serial port.");
                println!("Error:  {:?}", e);
                exit(0);
            }
        };
        match rb[0] {
            // Linefeed
            10 => {
                println!("{}<LF>", &out);
                out = String::with_capacity(1024);
            }
            // carriage return
            13 => {
                println!("{}<CR>", &out);
                out = String::with_capacity(1024);
            }
            // normal chars
            32...126 => {
                let temp: u32 = rb[0].clone() as u32;
                let ch: char = unsafe { mem::transmute(temp) };
                out.push(ch);
            }
            // everything else
            x => {
                println!("Non standard character encountered");
                println!("Value:  {:?}", x);
                exit(0);
            }
        };
    }
}

源代码也在Rust PlaygroundGithub上。

我正在使用 serial-rs 库和 Rust 1.8(GNU ABI Windows 7 x64 Professional)。

我的错误发生在第 46 行:

 match port.set_dtr( true ) {
     Ok(_) => { },
     Err(ref e) => display_error_message(e,"Error setting DTR line")
 };

这将返回错误代码 87(在 Windows 7 上)。标准的serial-rs 不保存错误代码,所以我使用my own fork(等待 PR)。

此错误来自格式错误的系统调用。深入serial-rs 会看到set_dtr(bool) 只是对EscapeCommFunction(2) first callsecond callthird call 的调用。参数代码出现correct

使用 MSVC 工具链编译会导致同样的错误,将 DTR 调用放在 RTS 调用之前会导致同样的错误。

同时提升 DTR 和 RTS 在 py-serial 中有效,但这样做时我无法写入端口。 See other issue

【问题讨论】:

    标签: windows serial-port rust


    【解决方案1】:

    USB->RS232转换器的串口仿真驱动不支持DTR线导致的错误。

    【讨论】:

      猜你喜欢
      • 2017-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-27
      • 2018-12-13
      • 2019-09-30
      • 2017-03-23
      相关资源
      最近更新 更多