Solved

Serial ports > 9

Posted on 1998-09-02
8
668 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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Let’s list some of the technologies that enable smooth teleworking. 
The viewer will learn common shortcuts with easy ways to remember them. The viewer will then learn where to find all of the keyboard shortcuts, how to create/change them, and how to speed up their workflow.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

831 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