Not reading in ReadFile function through COM port

Hello,
   I try to transfer file(in .txt format) from my test PC to my
working PC through serial port(COM port).
   One pc I open hyper terminal and set a connection in
9600(baud)-8(data bit)-none(parity)-1(stop bit)-none(flow control).In
another PC I write the code below in vc++ 6.0.
   I select Transfer->Send Text File and open a text file in my test
PC. Then I run this code in my working PC. Altough ReadFile function
doesn't get error message. dwBytesRead shows 0 during reading and text
in text file does not transfer to MyBuffer.
   My code is :

///////////////////////////////////////////////////
      DWORD m_dwError;
      HANDLE hPort = CreateFile("COM2", GENERIC_READ, 0, 0,
OPEN_EXISTING, 0, 0);
      if(hPort == INVALID_HANDLE_VALUE) {
	m_dwError = GetLastError();
	AfxMessageBox("CreateFile error occured");
      }
      DCB m_DCB;
      if(GetCommState(hPort, &m_DCB) == FALSE){
	m_dwError = GetLastError();
	AfxMessageBox("GetCommState error occured");
      }
      m_DCB.BaudRate = CBR_9600;
      m_DCB.ByteSize = 8;
      m_DCB.StopBits = ONESTOPBIT;
      m_DCB.Parity = NOPARITY;
      m_DCB.fRtsControl = RTS_CONTROL_DISABLE;
      m_DCB.fDtrControl = DTR_CONTROL_DISABLE;
      m_DCB.fOutxCtsFlow = FALSE;    
      m_DCB.fBinary = TRUE;
      m_DCB.fDsrSensitivity = FALSE;
      m_DCB.fInX = FALSE;
      m_DCB.fOutX = FALSE;
      m_DCB.fNull = FALSE;
      m_DCB.fAbortOnError = TRUE;
      m_DCB.fOutxDsrFlow = FALSE;
      m_DCB.fParity = 0;
      if(SetCommState(hPort, &m_DCB) == FALSE){	
	m_dwError = GetLastError();
	AfxMessageBox("SetCommState error occured");
      }
      COMMTIMEOUTS cto;
      cto.ReadIntervalTimeout = 5;   
      cto.ReadTotalTimeoutConstant = 300; 
      cto.ReadTotalTimeoutMultiplier = 1;
      if(SetCommTimeouts(hPort, &cto) == FALSE) {
	m_dwError = GetLastError();
	AfxMessageBox("SetCommTimeouts error occured");
      }
      
      DWORD dwBytesRead = 0;	
      char myBuffer[60];
	
      SetFilePointer(hPort, 0, NULL, FILE_BEGIN);
      for(int i =0; i < 10; i++) 
      {
	if(ReadFile(hPort, &myBuffer, 1, &dwBytesRead, 
                    NULL) == FALSE) 
	    AfxMessageBox("Error occured while reading to 
                           the serial port!!!"); 
      }
////////////////////////////////////   

how can I see the text in myBuffer? What is my error?

best regards
0
pera323 (3)
12/12/2003 1:37:37 PM
vc.mfc 33608 articles. 0 followers. Follow

3 Replies
545 Views

Similar Articles

[PageSpeed] 7

"sezo" <pera323@yahoo.com> wrote in message
news:5423eee3.0312120537.225a7e3d@posting.google.com...
> Hello,
>    I try to transfer file(in .txt format) from my test PC to my
> working PC through serial port(COM port).
>    One pc I open hyper terminal and set a connection in
> 9600(baud)-8(data bit)-none(parity)-1(stop bit)-none(flow control).In
> another PC I write the code below in vc++ 6.0.
>    I select Transfer->Send Text File and open a text file in my test
> PC. Then I run this code in my working PC. Altough ReadFile function
> doesn't get error message. dwBytesRead shows 0 during reading and text
> in text file does not transfer to MyBuffer.
>    My code is :
>
> ///////////////////////////////////////////////////
>       DWORD m_dwError;
>       HANDLE hPort = CreateFile("COM2", GENERIC_READ, 0, 0,
> OPEN_EXISTING, 0, 0);
>       if(hPort == INVALID_HANDLE_VALUE) {
> m_dwError = GetLastError();
> AfxMessageBox("CreateFile error occured");
>       }
>       DCB m_DCB;
>       if(GetCommState(hPort, &m_DCB) == FALSE){
> m_dwError = GetLastError();
> AfxMessageBox("GetCommState error occured");
>       }
>       m_DCB.BaudRate = CBR_9600;
>       m_DCB.ByteSize = 8;
>       m_DCB.StopBits = ONESTOPBIT;
>       m_DCB.Parity = NOPARITY;
>       m_DCB.fRtsControl = RTS_CONTROL_DISABLE;
>       m_DCB.fDtrControl = DTR_CONTROL_DISABLE;
>       m_DCB.fOutxCtsFlow = FALSE;
>       m_DCB.fBinary = TRUE;
>       m_DCB.fDsrSensitivity = FALSE;
>       m_DCB.fInX = FALSE;
>       m_DCB.fOutX = FALSE;
>       m_DCB.fNull = FALSE;
>       m_DCB.fAbortOnError = TRUE;
>       m_DCB.fOutxDsrFlow = FALSE;
>       m_DCB.fParity = 0;
>       if(SetCommState(hPort, &m_DCB) == FALSE){
> m_dwError = GetLastError();
> AfxMessageBox("SetCommState error occured");
>       }
>       COMMTIMEOUTS cto;
>       cto.ReadIntervalTimeout = 5;
>       cto.ReadTotalTimeoutConstant = 300;
>       cto.ReadTotalTimeoutMultiplier = 1;
>       if(SetCommTimeouts(hPort, &cto) == FALSE) {
> m_dwError = GetLastError();
> AfxMessageBox("SetCommTimeouts error occured");
>       }
>
>       DWORD dwBytesRead = 0;
>       char myBuffer[60];
>
>       SetFilePointer(hPort, 0, NULL, FILE_BEGIN);
>       for(int i =0; i < 10; i++)
>       {
> if(ReadFile(hPort, &myBuffer, 1, &dwBytesRead,
>                     NULL) == FALSE)
>     AfxMessageBox("Error occured while reading to
>                            the serial port!!!");
>       }
> ////////////////////////////////////
>
> how can I see the text in myBuffer? What is my error?
>
> best regards

This may or may not be the answer to your problem, but when I open the COM
port, I always open "COM1:".  Notice the ':' at the end.  I would think that
without the ':', CreateFile is creating a FILE called "COM2" in your root or
current directory.


0
trevor8021 (184)
12/12/2003 2:45:02 PM
You don't need the SetFilePointer call.

You are only trying to read 1 character at a time, and there may be no 
characters there. Change that to the size of your buffer and check the number of 
bytes read (which may be 0)

Put your ReadFile in a while loop or something as it might take some time for 
characters to arrive at your program. You are only trying 10 times.

It is normal to use overlapped IO for this sort of thing. When reading from 
commport, characters can turn up at any old time, depending on what your 
computer or the remote computer is doing at the time.

sezo wrote:
> Hello,
>    I try to transfer file(in .txt format) from my test PC to my
> working PC through serial port(COM port).
>    One pc I open hyper terminal and set a connection in
> 9600(baud)-8(data bit)-none(parity)-1(stop bit)-none(flow control).In
> another PC I write the code below in vc++ 6.0.
>    I select Transfer->Send Text File and open a text file in my test
> PC. Then I run this code in my working PC. Altough ReadFile function
> doesn't get error message. dwBytesRead shows 0 during reading and text
> in text file does not transfer to MyBuffer.
>    My code is :
> 
> ///////////////////////////////////////////////////
>       DWORD m_dwError;
>       HANDLE hPort = CreateFile("COM2", GENERIC_READ, 0, 0,
> OPEN_EXISTING, 0, 0);
>       if(hPort == INVALID_HANDLE_VALUE) {
> 	m_dwError = GetLastError();
> 	AfxMessageBox("CreateFile error occured");
>       }
>       DCB m_DCB;
>       if(GetCommState(hPort, &m_DCB) == FALSE){
> 	m_dwError = GetLastError();
> 	AfxMessageBox("GetCommState error occured");
>       }
>       m_DCB.BaudRate = CBR_9600;
>       m_DCB.ByteSize = 8;
>       m_DCB.StopBits = ONESTOPBIT;
>       m_DCB.Parity = NOPARITY;
>       m_DCB.fRtsControl = RTS_CONTROL_DISABLE;
>       m_DCB.fDtrControl = DTR_CONTROL_DISABLE;
>       m_DCB.fOutxCtsFlow = FALSE;    
>       m_DCB.fBinary = TRUE;
>       m_DCB.fDsrSensitivity = FALSE;
>       m_DCB.fInX = FALSE;
>       m_DCB.fOutX = FALSE;
>       m_DCB.fNull = FALSE;
>       m_DCB.fAbortOnError = TRUE;
>       m_DCB.fOutxDsrFlow = FALSE;
>       m_DCB.fParity = 0;
>       if(SetCommState(hPort, &m_DCB) == FALSE){	
> 	m_dwError = GetLastError();
> 	AfxMessageBox("SetCommState error occured");
>       }
>       COMMTIMEOUTS cto;
>       cto.ReadIntervalTimeout = 5;   
>       cto.ReadTotalTimeoutConstant = 300; 
>       cto.ReadTotalTimeoutMultiplier = 1;
>       if(SetCommTimeouts(hPort, &cto) == FALSE) {
> 	m_dwError = GetLastError();
> 	AfxMessageBox("SetCommTimeouts error occured");
>       }
>       
>       DWORD dwBytesRead = 0;	
>       char myBuffer[60];
> 	
>       SetFilePointer(hPort, 0, NULL, FILE_BEGIN);
>       for(int i =0; i < 10; i++) 
>       {
> 	if(ReadFile(hPort, &myBuffer, 1, &dwBytesRead, 
>                     NULL) == FALSE) 
> 	    AfxMessageBox("Error occured while reading to 
>                            the serial port!!!"); 
>       }
> ////////////////////////////////////   
> 
> how can I see the text in myBuffer? What is my error?
> 
> best regards

0
isemmel (236)
12/12/2003 7:18:17 PM
Are you using a serial cable or a null-modem cable? A serial cable won't
work.

Jase

"sezo" <pera323@yahoo.com> wrote in message
news:5423eee3.0312120537.225a7e3d@posting.google.com...
> Hello,
>    I try to transfer file(in .txt format) from my test PC to my
> working PC through serial port(COM port).
>    One pc I open hyper terminal and set a connection in
> 9600(baud)-8(data bit)-none(parity)-1(stop bit)-none(flow control).In
> another PC I write the code below in vc++ 6.0.
>    I select Transfer->Send Text File and open a text file in my test
> PC. Then I run this code in my working PC. Altough ReadFile function
> doesn't get error message. dwBytesRead shows 0 during reading and text
> in text file does not transfer to MyBuffer.
>    My code is :
>
> ///////////////////////////////////////////////////
>       DWORD m_dwError;
>       HANDLE hPort = CreateFile("COM2", GENERIC_READ, 0, 0,
> OPEN_EXISTING, 0, 0);
>       if(hPort == INVALID_HANDLE_VALUE) {
> m_dwError = GetLastError();
> AfxMessageBox("CreateFile error occured");
>       }
>       DCB m_DCB;
>       if(GetCommState(hPort, &m_DCB) == FALSE){
> m_dwError = GetLastError();
> AfxMessageBox("GetCommState error occured");
>       }
>       m_DCB.BaudRate = CBR_9600;
>       m_DCB.ByteSize = 8;
>       m_DCB.StopBits = ONESTOPBIT;
>       m_DCB.Parity = NOPARITY;
>       m_DCB.fRtsControl = RTS_CONTROL_DISABLE;
>       m_DCB.fDtrControl = DTR_CONTROL_DISABLE;
>       m_DCB.fOutxCtsFlow = FALSE;
>       m_DCB.fBinary = TRUE;
>       m_DCB.fDsrSensitivity = FALSE;
>       m_DCB.fInX = FALSE;
>       m_DCB.fOutX = FALSE;
>       m_DCB.fNull = FALSE;
>       m_DCB.fAbortOnError = TRUE;
>       m_DCB.fOutxDsrFlow = FALSE;
>       m_DCB.fParity = 0;
>       if(SetCommState(hPort, &m_DCB) == FALSE){
> m_dwError = GetLastError();
> AfxMessageBox("SetCommState error occured");
>       }
>       COMMTIMEOUTS cto;
>       cto.ReadIntervalTimeout = 5;
>       cto.ReadTotalTimeoutConstant = 300;
>       cto.ReadTotalTimeoutMultiplier = 1;
>       if(SetCommTimeouts(hPort, &cto) == FALSE) {
> m_dwError = GetLastError();
> AfxMessageBox("SetCommTimeouts error occured");
>       }
>
>       DWORD dwBytesRead = 0;
>       char myBuffer[60];
>
>       SetFilePointer(hPort, 0, NULL, FILE_BEGIN);
>       for(int i =0; i < 10; i++)
>       {
> if(ReadFile(hPort, &myBuffer, 1, &dwBytesRead,
>                     NULL) == FALSE)
>     AfxMessageBox("Error occured while reading to
>                            the serial port!!!");
>       }
> ////////////////////////////////////
>
> how can I see the text in myBuffer? What is my error?
>
> best regards


0
jshelley (197)
12/15/2003 9:29:59 PM
Reply:

Similar Artilces:

Cannot open port 25 in Exhange 2007
I keep on getting 'stealth' when I scan port 25 from the internet. My router forwards 25 correctly to the exchange server, and no other firewall is present. The receive connector of Exchange is on port 25, and I tried different kinds of authentication but without success, port keeps invisible. I have been struggling a day with this, please help! Thanks Can you telnet to the server on port 25 from the lan? Telnet localservername 25? who is your ISP? Do you have a business account with them? Josh "Prem" <premmetje@zonnet.nl> wrote in message news:1166068608.579454...

help with datedif function please
I have names and dates in columns , every time the name changes I need the difference from that date untill to day's date. With the same name I need the difference between the dates eg: A b c 1 vit 05/12/2009 datedif untill TODAY() 2 vit 20/11/2009 datedif between A1-A2 3 vit 04/07/2009 " " A2-A3 4 vit 24/06/2009 " " A3-A4 5 vit 15/06/2009 " " A4-A5 6 tom 21/11/2009 d...

Reading .wks file
Greetings...according to the Excel "help" file, as well as the file extension listing, I *should* be able to read an *.wks file, but Excel insists that it cannot. I am pretty sure the file was created in Microsoft Works. Is there a converter somewhere that I can download/install? Cheers - S2 Excel can read Works 2.0, not later. You have to save them in Works as excel files or Works 2.0 or get a commercial converter. -- Regards, Peo Sjoblom "Skip Stocks" <anonymous@discussions.microsoft.com> wrote in message news:AFC110E0-641D-4D87-9464-B930CC41CF02@microsoft....

How expensive are gdi drawing functions?
I am trying to improve the performance of a chart control.. lets say I am graphing data roughly 500 points per second and I have a 10 second window. So thats about 5000 points being represented in a control thats about 500 pixels wide. As of now, the chart tries to draw every single point. I am thinking that an algorithm that turns the 5000 points to 500 points might improve performance. lets say the chart is 100 pixels across, and the min(first window value) and max(last window value) of the xaxis is 0,100, so each pixel represents a unit. data is coming in at 10 points per unit(assu...

File won't open as read only
I have a file that is in use, but another person opens it and it doeasn't display the "file is in use" message. Is there a setting or fix? Hi have you shared this file? -- Regards Frank Kabel Frankfurt, Germany John wrote: > I have a file that is in use, but another person opens it > and it doeasn't display the "file is in use" message. Is > there a setting or fix? The file is on a network share. The share has all the appropriate permissions. >-----Original Message----- >Hi >have you shared this file? > >-- >Regards >Frank Ka...

Move system attendant mailbox <1174912412.216002.244450@y80g2000hsf.googlegroups.com>
The solution is here http://www.mcse.ms/archive/index.php/t-828566.html EggHeadCafe.com - .NET Developer Portal of Choice http://www.eggheadcafe.com ...

Read mail arn't marked as read anymore
After an SP upgrade of my Office 2000 the priviewed mail doesnt get marked as read anymore. I have tried to change the time (Tools->Options->Priview pane) from 2 -> 3 -> 4 seconds but nothing works. The only way to mark a mail as read is either to open it or right click it and chose Mark as read. Since I only use the priview pane this is very anoying for me. Is this a bug or has some setting changed with the SP? ...

Enter/Tab not functioning
We have a customer who recently upgraded to GP v 10.0. They are having random issues with the User Preferences for the Tab/Enter key. On certain instances when the User logs into GP, he is unable to use either Tab or Enter to move through fields - he has to go into User Preferences and click OK - it lasts as long as he is logged in but on the next day, he has to repeat the process. Has anyone else experienced this type of issue? He reports that it is random and has happened to several users in GP. Thank you! ...

How to give other users read-only access to Calendar
I want to allow the group Everyone to have read-only access to a calendar in a certain mailbox. I can do this by granting the permission 'Full mailbox access' (under 'Mailbox rights', under 'Exchange Advanced', for the particular user). However this also allows people to to do everything (ie: they become read-write users). I notice that every mailbox in the system has 'Read permissions' granted to group Everyone. This does not allow other people to open items in the mailbox, but as I understand it, permits Exchange Server and Outlook to do shared meetin...

reading confirmation
Good day, I have a problem with outlook. When they send a message to me that demands the shipment of a reading confirmation, even if I accept, the reading confirmation does not come received from the sender. Someone knows from what depends and in which way I can resolve the problem? Thanks for the eventual answers. Niki In news:eht7fo$251$1@fata.cs.interbusiness.it, Niki <nicola.pantaleo@yahoo.it> typed: > Good day, > > I have a problem with outlook. When they send a message to me that > demands the shipment of a reading confirmation, even if I accept, the > read...

Multiple IF function 03-23-10
Any assistance will be greatly appreciated. I have a worksheet that contains employee information, another worksheet that contains current team structure. I need the formula to look at the month and sine on the 'employee information' against the month and sine on the 'current team structure' and return the Product. If false, then return 'Other'. The logic is to lookup the sine on the employee information and return the applicable Product from the current team structure based their sine and month If they are not on the current team structure then refl...

"Unblock" feature should be optional when reading e-mail in CRM
When viewing e-mail messages in CRM, a line appears saying "Unblock" to allow the full message content to be read. Can this be made a configurable server or security setting? We are trying to reduce "clicks" as much as possible. ---------------- This post is a suggestion for Microsoft, and Microsoft responds to the suggestions with the most votes. To vote for this suggestion, click the "I Agree" button in the message pane. If you do not see the button, follow this link to open the suggestion in the Microsoft Web-based Newsreader and then click "I Agre...

Port is Closed for VPN Connection
I Receveied the following message when trying to connect VPN Connection: http://img444.imageshack.us/img444/6861/hqclienterror2ge6.jpg Can any1 tell me how to open back the port or anyway to prevent this to happen again? ...

The memory could not be "read".
I'm at work yesterday afternoon responding to e-mail on my desktop, and Adobe pops up from the toolbar with a notice that there is an update available for Acrobat. I accept the download, keep plugging away at my e-mail, and install it when it's done. It runs through the install, tells me that I should restart, but I ignore it and keep working. It's near the end of the day and I'm going to be going home soon anyways. I forgot to turn it off when I left. Came in this morning, restarted it through the Start menu, and rebooted. Upon reaching the "Ctrl+Alt+Del" ...

Restricting access to Export to Excel functionality.
Hi all I am aware that the answer is likely to be 'no', but is anybody aware of a way in which I can disable the Export to Excel functionality on the various screens (accounts, contacts, etc.) - either on a user by user basis or globally. A customer of mine believes this functionality makes it too easy for a user to do a complete dump of the central CRM data to Excel. (Obviously we have given them access to the data and we want them to be able to view it, but we do not want them to easily be able to do a complete dump). Any help appreciated. Cheers Andrew Andrew Volz Senior Consu...

AutoFilter and Frequency Function #2
I am trying to see if I can create a frequency function that will change as I autofilter different criteria. I have a file with over 15,000 rows returning data on sales opportunities including key fields I want to filter on: organization, product line, geography and status (Won, Lost, Open). In each case I want to see the change in distribution of "days in funnel" whether I am looking at all the data or just one geography or just one product line. It appears that the Frequency function does not recognize the filtered out data and continues to return values for the entire data set....

Valentine Gift
Valentine Gift - www.shopndiscount.com ...

how can i copy a document to a CD without making it read only?
HOW CAN I COPY A DOCUMENT TO A CD WITHOUT MAKING IN READ ONLY? You can't. It is not the file, but the media, that is read only. Even CD-RW media does not allow editing a file on the CD. Copy te file from CD to HD, mak edits and if a CD-RW you should be able to burn the edited file back to the CD. hth "DON" wrote: > HOW CAN I COPY A DOCUMENT TO A CD WITHOUT MAKING IN READ ONLY? ...

Unable to Read Japanese Email
I correspond with several Japanese users and can read emails from some of them without a problem, but emails from others are nothing but a series of ?????. In one case, I can read one email but not another from the same sender. Changing the Encoding doesn't help. Sending emails in Japanese to them without a problem...they can read it fine. Would appreciate any suggestions/solutions as I'm stumped. I'm using Outlook 2003 on a Windows XP Home operating system. Japanese language support is installed. ...

Transferring read e-mail to another folder
Hello I was wondering whether there was a way in which I could automatically transfer my e-mail that has been read into another folder, such as "Old e-mail" or something like that? Thanks ...

write a program which tests a specific port to see wether is listening mode?
hi all, I want to write a program which tests a specific port to see wether is in listening mode or not. do you know any api / code to get tat?? Network port? Or I/O port? I'm assuming network port information here... Are you looking for the sort of state that netstat reports? Take a look at GetTcpTable, which will lead you to the rest of the functions for the IP Helper APIs. joe On 30 Apr 2007 04:53:13 -0700, "vishal.g.shah@gmail.com" <vishal.g.shah@gmail.com> wrote: >hi all, > I want to write a program which tests a specific por...

Function question
Hi Is there a function that I can total the best x of y cells? Example: I am totalling scores over 6 rounds of which only the best 4 will count Neil Sounds like Golf so maybe SMALL instead. PC "Don Guillett" <donaldb@281.com> wrote in message news:ObNTo#aRDHA.3768@tk2msftngp13.phx.gbl... > take a look at LARGE > > -- > Don Guillett > SalesAid Software > Granite Shoals, TX > donaldb@281.com > "Neil Grantham" <neil40@btinternet.com> wrote in message > news:c66a9b10.0307081553.7ca2be6f@posting.google.com... > > Hi > > &...

Outlook not marking read emails as read
Hi - We have 1 computer that is doing the oddest thing, anytime the customer reads the last email in his box, exit's out of email and then comes back in, that email is now marked as unread - it's the weirdest thing I have seen in a long time. I uninstalled office XP, rebooted, then reinstalled and applied the 2 service packs, hoping that would fix it, but it didn't. So now I'm stuck and was wondering if anybody out there has any thoughts on what to do.... thanks! Gerri Urban gurban@ci.broomfield.co.us ...

Array functions and cell formatting problem
Dear all! I kindly request your help with the following problem i've run into: I'll try to be as explanatory as possible, so please bear with me :-). I'm working on Excel XP. I'm using a Main Excel file (MAIN) that uses several array functions t perform some computations. These array functions refer to Ranges in a Database (DB) Excel file. There is also an Input Excel file (INPUT), that feeds MAIN with data. After MAIN is fed with data from INPUT, it tries to calculate its arra functions, which refer to ranges in DB. So we have the following 3 Excel files: INPUT (Data)...

Suggested reading
Any suggested reading for Access 2003 VBA? I have both "Step by Step Access 2003" And "Microsoft Access 2003 VBA for Dummies" (how Ironic) and niether have been very helpful. Try this book. It's a winner: Access 2003 VBA Programmer's Reference by Patricia Cardoza, Teresa Hennig, Graham Seach, and Armen Stein http://www.amazon.com/Access-2007-Programmers-Reference-Programmer/dp/0470047038/ref=sr_1_1/104-1181757-2327103?ie=UTF8&s=books&qid=1185824619&sr=8-1 -- Arvin Meyer, MCP, MVP http://www.datastrat.com http://www.mvps.org/access http://www.access...