Solved

Serial ports > 9

Posted on 1998-09-02
8
659 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
  • 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
 
LVL 8

Expert Comment

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

is a C++ style comment, replace with

/*  data to output  */


0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

If your app took Google’s lash recently, here are the 5 most likely reasons.
Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
The viewer will learn how to create multiple layers to apply various filters and how to delete areas from each layer’s filter.

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now