PLEASE.
I have read the entire forum looking for an example for writing and reading on serial port.
I refuse to use winAPI32 because I am not a real programmer, I just have made an electronic aplication and I need it to comunicate to PC!
I wrote this code just for testing:
include <cstdlib>
include <iostream>
include <stdio.h> //I DONT KNOW IF ALL of THIS IS NECESARY
Well, as you can see, I just can read and not to write, but.
If I set fopen(...,"wb");
I can write but not to read.. Thus, I have tryed r+,w+,rb+,wb+,etc... but nothing.
I would need just the follow.
1º Send a 'P' on serial until receive an 'I' and break out.
2º Send another letter and receive amount of data from my electronic aplication.
PLEASE, I dont know where to start.
Might anybody hand me a piece of code? THANKS, THANKS A LOT!!!!
Ivan Perino
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Well with a post like that and an attitude like that you may be hard pressed to get a response....SOOOOOOO what I suggest is you open your browser and type in the search box "C++ serial port programming tutorial"
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
You don't have to be a real programmer to use the API. I'm just a hardware moron and I can handle it. All you really need is this:
HANDLE hComm;
hComm = CreateFile(com_port_text, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0); // open port
You can then write with:
DWORD iBytes;
WriteFile(hComm,text,length,&iBytes,NULL);
FlushFileBuffers(hComm); // force immediate write
Reading is a little trickier, but you can read one char at a time with something like this:
DWORD dwErrors;
COMSTAT comStat;
DWORD iBytes;
char sBuffer[2];
ClearCommError(hComm, &dwErrors, &comStat); // fills in status including number of bytes waiting to be read
bytes_waiting = comStat.cbInQue;
ReadFile(hComm, &sBuffer, 1, &iBytes, NULL); // read one byte
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2007-08-29
rb+ and wb+ should work. How are you testing this, is your cable sound? Use a port monitor application to see is the serial driver is getting the data (try http://www.microsoft.com/technet/sysinternals/utilities/portmon.mspx for example). The advantage of this is that you can determine the port activity without having to worry about correct cabling or matching baud rates.
Note that traditionally line ends are "\r\n" not "\n\r" (i.e. CRLF not LFCR)- this may make a difference to some applications.
I have some example serial I/O code using this technique somewhere, I'll dig it out - standby.
Clifford
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2007-08-29
... Sorry, seems I dumped it.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
PLEASE.
I have read the entire forum looking for an example for writing and reading on serial port.
I refuse to use winAPI32 because I am not a real programmer, I just have made an electronic aplication and I need it to comunicate to PC!
I wrote this code just for testing:
include <cstdlib>
include <iostream>
include <stdio.h> //I DONT KNOW IF ALL of THIS IS NECESARY
include <stdlib.h>
using namespace std;
int main(int argc, char argv[])
{
char C='m';
FILE portfp ;
}
Well, as you can see, I just can read and not to write, but.
If I set fopen(...,"wb");
I can write but not to read.. Thus, I have tryed r+,w+,rb+,wb+,etc... but nothing.
I would need just the follow.
1º Send a 'P' on serial until receive an 'I' and break out.
2º Send another letter and receive amount of data from my electronic aplication.
PLEASE, I dont know where to start.
Might anybody hand me a piece of code? THANKS, THANKS A LOT!!!!
Ivan Perino
Well with a post like that and an attitude like that you may be hard pressed to get a response....SOOOOOOO what I suggest is you open your browser and type in the search box "C++ serial port programming tutorial"
You don't have to be a real programmer to use the API. I'm just a hardware moron and I can handle it. All you really need is this:
HANDLE hComm;
hComm = CreateFile(com_port_text, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0); // open port
You can then write with:
DWORD iBytes;
WriteFile(hComm,text,length,&iBytes,NULL);
FlushFileBuffers(hComm); // force immediate write
Reading is a little trickier, but you can read one char at a time with something like this:
DWORD dwErrors;
COMSTAT comStat;
DWORD iBytes;
char sBuffer[2];
ClearCommError(hComm, &dwErrors, &comStat); // fills in status including number of bytes waiting to be read
bytes_waiting = comStat.cbInQue;
ReadFile(hComm, &sBuffer, 1, &iBytes, NULL); // read one byte
rb+ and wb+ should work. How are you testing this, is your cable sound? Use a port monitor application to see is the serial driver is getting the data (try http://www.microsoft.com/technet/sysinternals/utilities/portmon.mspx for example). The advantage of this is that you can determine the port activity without having to worry about correct cabling or matching baud rates.
Note that traditionally line ends are "\r\n" not "\n\r" (i.e. CRLF not LFCR)- this may make a difference to some applications.
I have some example serial I/O code using this technique somewhere, I'll dig it out - standby.
Clifford
... Sorry, seems I dumped it.