c - 如何在 DOS/C 中访问非标准 COM 端口(USB->Serial,COM5+)?

标签 c usb serial-port dos turbo-c

我正在使用一个内部软件工具来显示和记录从我为其开发嵌入式软件的产品的串行调试端口收集的格式化诊断数据。它在 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-> 串行适配器显示为 COM 端口 5+ 时,我有什么选择可以调整这种配置?我可以使用 DOS mode 命令(以及像正常人一样在 Windows 设备管理器中)按预期看到它们,但我不确定如何从诊断程序访问它们。

最佳答案

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

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

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

关于c - 如何在 DOS/C 中访问非标准 COM 端口(USB->Serial,COM5+)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11474180/

相关文章:

c - 测试迭代指针之间的关系是否安全?

c# - WMI:在插入时获取 USB 设备描述

c++ - 串口传输错误如何解决?

C# Raspberry pi Linux 串行端口权限即使作为 root 也被拒绝

c - 在下面的场景中我是服务器还是客户端?

c - strtof() 在 C 中产生奇怪的结果

c - d引用二维数组

python - 如何在python中使用struct/class的 vector

python-2.7 - 为什么来自 Arduino 的串行通信会在一段时间后停止

python :Read from a USB HID device