【问题标题】:How to access non-standard COM ports (USB->Serial, COM5+) in DOS/C?如何在 DOS/C 中访问非标准 COM 端口(USB->Serial、COM5+)?
【发布时间】:2012-07-13 16:06:33
【问题描述】:

我正在使用一种内部软件工具,该工具可以显示和记录从我为其开发嵌入式软件的产品的串行调试端口收集的格式化诊断数据。它是 C 语言并且非常古老。它是使用 Borland Turbo-C v1.01(版权所有 1990!)构建的。如果可能的话,我更愿意为现代环境修改而不是重写该工具。

我想一次从多个设备收集调试数据。我设想通过 USB-> 串行适配器连接到集线器的几个设备,连接到 PC(运行 Windows XP)。每个设备运行一个诊断工具实例(同样在 Windows 中),指向相应的 COM 端口。简单吧?

不完全是。观察我正在使用的串口初始化函数:

void serinit(int baudrate, char paristat, char adaptnum) {
  int hibcon, lobcon, paricon;
  if(adaptnum == '3') {
    sioreg = lowbaud = 0x3E8;     // SIO (Serial I/O Reg.)
    intenreg = highbaud = 0x3E9;  // IER (Interrupt Enable Reg.)
    intidreg = 0x3EA;             // IIR (Interrupt Ident. Reg.)
    linecon = 0x3EB;              // LCR (Line Control Reg.)
    modemcon = 0x3EC;             // MCR (Modem Control Reg.)
    linestat = 0x3ED;             // LSR (Line Status Reg.)
    modemstat = 0x3EE;            // MSR (Modem Status Reg.)
    sintvect = 0x0C;
    sintmask = 0x10;
  } else if(adaptnum == '2') {
    //omitted for brevity, similar to above w/ different magic numbers
  } else {
    //ditto
  }

  outportb(linecon, 0x80);        // LCR - set up to set baud rate

  switch(baudrate) {
    case 9600:  hibcon = 0x00;  lobcon = 0x0C; break;
    //more magic numbers for other baud rates
  }

  outportb(lowbaud, lobcon);            // Baud Rate Divisor LSB
  outportb(highbaud, hibcon);           // Baud Rate Divisor MSB

  switch(paristat) {
    case 'o': //odd parity, 2 stop, 7 data
    case 'O': paricon = 0x0E; break;
    //more magic numbers for other parity settings
  }

  outportb(linecon, paricon);  //Line Control Register
  outportb(intenreg, 0x01);    //IER - receive enabled
  outportb(modemcon, 0x09);    //x x x x +out2 x -rts +dtr

  imodemcon = 0x09;     //update image
  inportb(sioreg);      //Just in case there's anything lurking in the register
  intvsave = getvect(sintvect);
  setvect(sintvect, serint);   //Set up interrupt vector.
  outportb(0x21, inportb(0x21) & !sintmask); //OCW 1 - enable serial interrupts
}

当 USB->Serial 适配器将显示为时,我有哪些选项可以为 COM 端口 5+ 调整这种配置?我可以使用 DOS mode 命令(以及在 Windows 设备管理器中像普通人一样)按预期看到它们,但我不确定如何从诊断程序中访问它们。

【问题讨论】:

标签: c usb serial-port dos turbo-c


【解决方案1】:

直接寻址 I/O 寄存器需要模拟传统 COM 端口行为的设备驱动程序。标准的 Microsoft 设备驱动程序执行此操作。但是你没有使用那个驱动程序,你有一个供应商特定的 USB 驱动程序。

这些驱动程序通过将自己连接到串行端口的标准 winapi 函数来模拟串行端口。像 CreateFile()、SetCommConfig() 等。这需要编写 32 位代码才能使用这些功能。他们没有做的是模拟寄存器,所以 DOS 应用程序仍然可以工作,这已经结束了。并且不能正常工作,DOS 只支持 4 个 COM 端口,所以只使用了 4 组寄存器。 COM5 及更高版本没有标准寄存器地址。

也许您可以找到一个带有驱动程序的 USB 仿真器,它仍然可以执行此操作。但我认为几率非常低。相反,将 90 年代的软件与 90 年代的硬件结合起来。购买您拧入总线的老式 PCI 卡。这样标准的 Microsoft 驱动程序才能工作。上次我看时(胖一年前)这些卡仍然可用,尽管选择越来越少。或者从旧机器中挖出一个。

【讨论】:

  • 另外,很多USB串口适配器没有实现所有的状态控制线(或正确实现)
【解决方案2】:

如果您运行的是纯 DOS,您将受限于系统上可用的 COM 端口。查看this串口扩展器的用户手册。它允许您选择最多 7 个 com 端口。

如果您在 Windows 中运行此 DOS 应用程序,请查看设备管理器中的设备资源。它告诉您的 I/O 范围将是您程序的寄存器地址范围。此网页demonstrates 演示查找信息。

【讨论】:

    【解决方案3】:

    HyperTerminal Hilgraeve/Microsoft 虚拟化 COM1。腻子紧随其后。 DOSBox 下一个带有 serial1/realport。示例:http://www.prosefights.org/irp2014/ganssle.htm#14510USB :)

    【讨论】:

    • 欢迎来到 Stack Overflow!虽然这在理论上可以回答问题,it would be preferable 在此处包含答案的基本部分,并提供链接以供参考。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-08
    • 2013-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多