Solved

Serial ports > 9

Posted on 1998-09-02
8
674 Views
Last Modified: 2013-11-15
I have a routine using fprintf to output to a serial port and this works fine however when I go above Com9 ie Com10 no information is outputed, I have 16 correctly setup serial ports on my system and have run into simillar problems in HTBasic (but I was expecting that since it is documented that HTbasic can only handle 9 serial ports), any help would be greatly welcomed, thanks.
0
Comment
Question by:pdcn01
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 8

Expert Comment

by:Answers2000
ID: 1252473
Are you in Windows ?  If yes this is the reason

There is a bug in the Windows CreateFile API (called by fopen, fcreat etc.).  This is documented in the knowledge base

(begin quote)
Specifying Serial Ports Larger than COM9
Article ID: Q115831
Creation Date: 05-JUN-1994
Revision Date: 25-SEP-1995
The information in this article applies to:

Microsoft Win32 Application Programming Interface (API) included with:

    - Microsoft Windows NT versions 3.1, 3.5, and 3.51
    - Microsoft Windows 95 version 4.0

CreateFile() can be used to get a handle to a serial port. The "Win32 Programmer's Reference" entry for "CreateFile()" mentions that the share mode must be 0, the create parameter must be OPEN_EXISTING, and the template must be NULL.

CreateFile() is successful when you use "COM1" through "COM9" for the name of the file; however, the message "INVALID_HANDLE_VALUE" is returned if you use "COM10" or greater.

If the name of the port is \\.\COM10, the correct way to specify the serial port in a call to CreateFile() is as follows:


   CreateFile(
      "\\\\.\\COM10",     // address of name of the communications device
      fdwAccess,          // access (read-write) mode
      0,                  // share mode
      NULL,               // address of security descriptor
      OPEN_EXISTING,      // how to create
      0,                  // file attributes
      NULL                // handle of file with attributes to copy
   );

NOTES: This syntax also works for ports COM1 through COM9. Certain boards will let you choose the port names yourself. This syntax works for those names as well.
(end quote)

1. The sure solution then is to switch to the API functions (CreateFile, WriteFile, etc) and use the names as shown above.

2. You can try using the modified names in your call to fopen/fcreat etc. but this may or may not work (depends on how you C compiler's runtime library handles this)

3. Some C compilers have a runtime library function (this is compiler specific and non portable)  to convert an already open o/s FILE HANDLE into a file *.  Check you're compiler help.  If yours has one, you can open the file with CreateFile as shown above, and then convert the handle into a FILE *, and finally use the normal fprintf etc. functions.
0
 

Author Comment

by:pdcn01
ID: 1252474
Can't seem to get this to work prehaps you could supply a complete function to send a message to com10, my knowledge of MDS is limited.
Cheers.
0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1252475
I can't try this at the moment, but this should be enough.

Suppose szBuffer is an array of characters you want to write

char szBuffer[] = "SomeInfo" ; // data to output
HANDLE hh ; /* handle to file */
DWORD dwWritten ; /* bytes written */
BOOL bWritten ;  /* written okay */

hh =  CreateFile(
               "\\\\.\\COM10",    
               GENERIC_WRITE,
               0,                  
               NULL,            
               OPEN_EXISTING,
               0,                  
               NULL
            );

if ( hh != INVALID_HANDLE_VALUE )
{
   bWritten = WriteFile( hh, szBuffer, sizeof szBuffer, &dwWritten, NULL ) ;
   CloseHandle( hh ) ;

   if ( ( !bWritten ) || ( dwWritten != (sizeof szBuffer)) )
   {
     /* Write failed - put error handling code here */
   }
} else
{
 /* Port couldn't be opened - put error handling code here*/
}

0
Get Actionable Data from Your Monitoring Solution

Your communication platform is only as good as the relevance of the information you send. Ensure your alerts get to the right people every time with actionable responses. Create escalation rules that ensure everyone follows the process and nothing is left to chance.

 
LVL 8

Expert Comment

by:Answers2000
ID: 1252476
Sorry
 // data to output

is a C++ style comment, replace with

/*  data to output  */


0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1252477
One other thing the way I wrote it, I write the terminating NUL in the string, if you don't want that, replace all sizeof(szBuffer)'s with strlen(szBuffer)
0
 

Author Comment

by:pdcn01
ID: 1252478
I still seem to be having grief with some of these commands I'm using version 5 Visual C++ however it is saying HANDLE is an undeclared identifier along with CreateFile, GENERIC_WRITE etc,
Now is this some sort of header file problem or what, I tried including all the header files and still no luck, the noose is approaching.
Thanks for your help so far Answers2000, hopefully you know what the problem is!.

0
 

Author Comment

by:pdcn01
ID: 1252479
I have gotten it to work now, please submit an answer so I can grade it,
Thanks again answers2000

0
 
LVL 8

Accepted Solution

by:
Answers2000 earned 100 total points
ID: 1252480
Thanks pdcn01, sorry been on holiday for a couple of weeks so I didn't see the Sept 8 comment till now.

Glad it's working now
0

Featured Post

Turn Insights Into Action

You’ve already invested in ITSM tools, chat applications, automation utilities, and more. Fortify these solutions with intelligent communications so you can drive business processes forward.

With xMatters, you'll never miss a beat.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If your app took Google’s lash recently, here are the 5 most likely reasons.
All of the resources available today make learning a new digital media easier than ever-- if you know where to begin. This is a clear, simple guide to a few of the basic digital art mediums and how to begin learning them on your own.
The viewer will learn how to successfully download and install the SARDU utility on Windows 7, without downloading adware.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question