?
Solved

Serial ports > 9

Posted on 1998-09-02
8
Medium Priority
?
685 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 300 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

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

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

Web hosting control panels were first developed to make it faster and easier for most users to set up and operate websites. The graphical user interface (GUI) allows users to perform tasks by pointing and clicking rather than typing highly specific…
While Plesk offers many potential benefits to website administrators, including compatibility with Windows Server and other leading technologies, the company has also been working to differentiate it from other control panels for content management…
Video by: Tony
This video teaches viewers how to export a project from Adobe Premiere Pro and the various file types involved.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

830 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