Solved

Serial ports > 9

Posted on 1998-09-02
8
670 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Microsoft  Partnership 1 95
How does this program upload video to Youtube? 10 67
Unable to print PDF document on Mac laptop 6 49
Setting variables in a stored procedure 5 49
Today companies are subjected to more-and-more data, and it won't stop any time soon.  But there are obvious opportunities for reducing data, particularly data duplicated among companies.
Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
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.

749 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