Thread: Createfile function...I am so lost...

  1. #1
    Unregistered
    Guest

    Unhappy Createfile function...I am so lost...

    I need to open COM1 for reading data only. I thought I could use the function Createfile. I am using MS VC++ and want to do this inside of my C application. I am lost and need guidance. I'm not looking for anyone to write code for me...just some help. Thanks!
    Fred

  2. #2
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    Post your code.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,792
    Firstly I cant test any code I write on COM1 as my port has no devices.........

    I wrote the following using LPT1 (my paralell port that has a printer on it) so I guess they shouls work the same......Also of course I couldnt recieve data...only write.......but it should give you some idea..

    Code:
    #include <windows.h>
    #include <string.h>
    
    
    
    int WINAPI WinMain(HINSTANCE hThisInstance,
    				   HINSTANCE hPrevInstance, 
    				   LPSTR lpszArgument, 
    				   int nShow)
    {
    	HANDLE hFile;
    	DWORD dwWritten;
    	char Buff[] = "Hello World",
    		 Feed[] = "\f";
    
    	hFile = CreateFile("LPT1",GENERIC_READ | GENERIC_WRITE,
    		FILE_SHARE_READ | FILE_SHARE_WRITE,NULL,OPEN_EXISTING,
    		FILE_ATTRIBUTE_NORMAL,NULL);//Open up LPT1
    	
    	
    	if(hFile == INVALID_HANDLE_VALUE){
    		MessageBox(HWND_DESKTOP,"Could not open LPT1",
    			"Error",MB_OK);
    		return 1;
    	}
    
    	WriteFile(hFile,Buff,strlen(Buff),&dwWritten,NULL);
    		//Write my catchy phrase
    	
    	WriteFile(hFile,Feed,strlen(Feed),&dwWritten,NULL);
    		//Send the ASCII char to feed page
        
    	CloseHandle(hFile);//Close my handle
        return 0;
    }

  4. #4
    Unregistered
    Guest

    Unhappy

    GaPe,
    I had written code but unfortunately it was not relevant to using the functions that I (think???) I need such as (CreateFile, BuildCommDCB, ReadFile, etc.). I guess I need to know if I'm on the right track using these functions for opening and configuring a serial port and then reading continuous streaming data from the serial port. I am using an NT machine doing my development in C in developer studio. PS- I wouldn't mind seeing an example if anyone could provide one.
    Thanks!
    Fred

  5. #5
    Unregistered
    Guest

    Smile

    Fordy,
    This at least gives me a clue.
    Thanks!
    Fred

  6. #6
    Unregistered
    Guest

    Unhappy

    Well I thought it helped but I am still lost. I have outlined what I need to do. My outline is as follows:
    1. Open a serial port (COM1).
    2. Set up it's parameters (baud rate, etc).
    3. Read data from the serial port.
    4. Put this data into variable strings and intergers.
    5. Provide error handline for comunications.
    6. Closing the serial port.

    I think I know which functions to use ( CreateFile, BuildCommDCB, ReadFile, etc.). But I am having trouble with visualizing how to get the serial data from the serial port into my variables. I would appreciate some guidance. Thanks!
    Fred

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Try a few searches.

    And another
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Unregistered
    Guest

    Unhappy

    Hammer,
    Thanks for the links....still can't seem to put it together.
    Fred

  9. #9
    Unregistered
    Guest

    Unhappy

    Could somebody please help me with putting together the code or ideas of going from the "ReadFile" stage to putting the data from the ReadFile into variables. Thanks!
    Fred

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Unregistered
    Could somebody please help me with putting together the code or ideas of going from the "ReadFile" stage to putting the data from the ReadFile into variables. Thanks!
    Fred
    No. This is not a "here, let me do everything for you" board. This is a "post your code, and we'll help with problems" board.

    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Unregistered
    Guest

    Wink

    I am just looking for an example on how to take what I get from the ReadFile and put it into variables. An example is all. I am not looking for any more hardship. Thanks!
    Fred

  12. #12
    Registered User
    Join Date
    Dec 2001
    Posts
    27
    Pappy
    You learn something new everyday.

  13. #13
    Unregistered
    Guest

    Wink

    Thanks Pappy! However, I have all the functions working that configure, open, and read from the comm port. I just need to figure out how to go from what the ReadFile function reads in, to putting the bytes read in into variables. I wish someone with god-like ability would come through and help this pure mortal out on this. Thanks!
    Fred

  14. #14
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,792
    Originally posted by Unregistered
    Thanks Pappy! However, I have all the functions working that configure, open, and read from the comm port. I just need to figure out how to go from what the ReadFile function reads in, to putting the bytes read in into variables. I wish someone with god-like ability would come through and help this pure mortal out on this. Thanks!
    Fred
    The buffer you pass as parameter 2 to ReadFile holds the data read........ you use param 3 to say how much data you want read.....then param 4 tells you how much was actually read.......

  15. #15
    Unregistered
    Guest
    Fordy,
    I know that. I am missing something very basic here. That is what I have in param 2 needs to get into my variables. Can I just do a strcpy of what's in param 2 to each one of my variables or something like that?
    Fred

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Post your games here...
    By Hammer in forum Game Programming
    Replies: 132
    Last Post: 02-28-2013, 09:29 AM
  2. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  3. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM