简体   繁体   中英

Easiest way to implement a Com Port Terminal. (Windows, C)

I need to write a Terminal to communicate with a COM - port and I need to be able to send commands from the COM-Port, as well as from the Console at the same time. (I want to get access to a computer via two sensor nodes, that are communicating with each other wirelessly, so I still need a way to send something from the node to the Computer)

Now, I have implemented a Non Overlapped Serial Communication, but I am not sure, how to implement the "Send and Receive at the same time"-Part and I only have around 4 days to solve the problem. There is not really that much information out there, so I would welcome any pointers on how to implement that fastest or easiest way.

Overlapped I/O-Communication is not exactly very time friendly as far as I can see. Is it possible to do this with multi-threading (only overlapped)? I am guessing in that case I would have to read the buffer every few ms and make an own thread for the input?

Whether to use overlapped I/O or not isn't really the issue: overlapping just frees up some time for your program. I have written many programs like this and the conclusion is always to use a thread to handle all COM routines. Whether this thread calls overlapped or synchronous methods is less relevant, as long as the thread lies idle doing WaitForMultipleObjects().

The way I have written my most recent COM terminal is this (pseudo):

thread()
{
  while not kill the thread event
  {
    WaitForMultipleObjects (open port, close port, kill the thread event)

    if (open port)
    {
      send();
      receive();
      wait_for_send_and_receive();
    }
  }  
}

send()
{
  take COM_port mutex
  if(there is something to send)
  {
    copy send_data to local_data, protect this with mutex
    WriteFileEx(COM_port,
                local_data,
                size,
                some_overlapped_struct_stuff);

    handle errors

  }
  release COM_port mutex
}

receive()
{
  take COM_port mutex

  ReadFileEx(COM_port, ...);

  handle errors

  release COM_port mutex
}

wait_for_send_and_receive()
{
  WaitForMultipleObjects (open port, 
                          close port, 
                          kill the thread event,
                          send done event from send callback routine (overlapped I/O),
                          receive done event from receive callback routine (overlapped I/O)
                         );

}

Naturally this is quite an over-simplification since you need various functions for open/close COM port, data shuffling etc. Several mutices are likely needed.

I'd share the real, working production code if it wasn't corporate property :( 4 days seems a bit optimistic, judging from my project log, it took me several months to develop a working COM port terminal to production quality level. The COM port driver alone is around 1k loc, with a lot of Win API calls all over.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM