Serial port communication in VC++

Can any one help me?
I am trying to access serial port using vc++. i cnnot read the port data. how to do it? i am using the standard active-X control MSComm control and reading the data in a variant.

my mail id is tamalbp(AT)rediffmail.com
 
I also have the same trouble (Close Handle not freeing the comm port. I use the comm port at around 10kbyte/sec (115200 baud).. Seems to work ok at 500 byte/sec.. I am using windows2000.. Windows98 did not seem to have this problem, but corrupted the recieved data occasionally instead(lost bytes).. Got any answers? ([email protected])
 
Hi

I can not read and print from serial port. I am reading the data in a buffer char RX[10], using readfile function; but while printing it after formatting it like

CString l;
l.Format("%c", RX);
CClienDC dc(this);
dc.TextOut(100,100,l);

it is printing some funny character and not the one that i sent

please mail me at [email protected].

thanks

tamal
 
F

Friedrich Haase

Moin tamal,

when formatting the string l, you passed RX, which is a pointer to the character array. I is not the first character.
l.Format("%c", RX);

Try
l.Format("%c", RX[0]);
instead.

hope this helps

regards
Friedrich Haase
 
i included windows.h,tty.h yet the code is not being compiled ...........
it gives error regarding tty.h
what shall i do ?
generally which files are essential to include for compiling serial port comm. prog.
thanks
 
Hello, i am currently doing a project using serial communication between two PCs. I am new to the visual c++ and i really confuse how to make program to set one Pc and transmitter and another one as receiver by the mean time the receiver can convert the string to the value it required. Can you give me some help please. I really appreciate if any one can help me to solve this problem.
 
In general if I'm missing an include file from sample code, and I've verified

by a file search it's not on my system somewhere, I'll modify the include line to slightly modify the include file name, such as tty.h to mytty.h and create the file mytty.h, putting put in "stub" code, definitions, etc. that match in type to what the sample code is looking for to gradually eliminate the compilation errors in the sample code. Then you'll get a picture of what was expected in the tty.h file.

Often include files in code snippets only provide one or two definitions or calls, sometimes they're just vestigial information from the incomplete snippet extraction.

The worst case is when the author says, "oh, yeah, here's the header file you're missing" and when you get it, IT has a couple more missing header/code files and so on and so on....

Rufus
 
hi madhura

will you plz send me the vc++ project of this code bcaz i am not findind tty.h file

its urgent!!!!!!!11

bye
mashy
 
D
Hi Mate,
Your progrma really nice i have some doubts.
could you explain following pls.

First Occurence of substring how related with consequences,

(strstr(mdmData, "ENTER USERNAME"))
...

Thanks
Regards
D.Saravanan







> I wrote the following program for logging into my ISP, but for some reason the ReadFile does not returns. However, if I use HyperTerminal for doing the same this, and then disconnect and run my program, it works. Similarly, if I once get the connection using Dial-up service that comes with Windows, and then disconnect and try to connect again with my program, then it works (that is, the ReadFile returns).
>
> Please advise.
> Thanks.
>
>
> #include <stdio.h>
> #include <windows.h>
>
> int
> main(int argc, char *argv[])
> {
> HANDLE hCom;
> COMMPROP commProp = {0};
> COMMCONFIG commConfig = {0};
> DCB dcb = {0};
> BOOL fSuccess;
> DWORD bytesRW;
> char *pcCommPort[] = {"COM1","COM2","COM3","COM4"};
> char mdmData[512];
> char tempData[20];
> int i;
>
> while(1)
> {
> //////////////////////////
> // Find and open modem //
> //////////////////////////
> for(i=1; i<=4; i++)
> {
> // Open a COM port
> hCom = CreateFile(
> pcCommPort[i-1],
> GENERIC_READ | GENERIC_WRITE,
> 0, // comm devices must be opened w/exclusive-access
> NULL, // no security attributes
> OPEN_EXISTING, // comm devices must use OPEN_EXISTING
> 0, // Non-overlapped I/O
> NULL // hTemplate must be NULL for comm devices
> );
>
> if (hCom == INVALID_HANDLE_VALUE)
> {
> // Handle the error.
> printf ("\n\nCreateFile failed at %s with error %d.", pcCommPort[i-1], GetLastError());
> continue;
> }
> printf ("\n\nCreateFile succeeded at %s!.", pcCommPort[i-1]);
>
> // Get modem capabilities
> fSuccess = GetCommProperties(
> hCom, // handle to comm device
> &commProp // communications properties
> );
>
> if (!fSuccess)
> {
> // Handle the error.
> printf ("\nGetCommProperties failed at %s with error %d.", pcCommPort[i-1], GetLastError());
> return (0);
> }
> printf ("\nGetCommProperties succeeded at %s!.", pcCommPort[i-1]);
>
> if(commProp.dwProvSubType != PST_MODEM)
> {
> printf("\nNo modem found at %s!", pcCommPort[i-1]);
> if(i==4)
> {
> printf("\nNo modem found in this computer!");
> return(0);
> }
> CloseHandle(hCom);
> continue;
> }
> else
> {
> printf("\nModem found at %s!", pcCommPort[i-1]);
> break;
> }
> }
> /////////////////////
> // Configure modem //
> /////////////////////
>
> // Get current configuration of the modem
> fSuccess = GetCommState(
> hCom, // handle to comm device
> &dcb // device-control block
> );
>
> if (!fSuccess)
> {
> printf ("\nGetCommState failed at %s with error %d.", pcCommPort[i-1], GetLastError());
> break;
> }
> printf ("\nGetCommState succeeded at %s!.", pcCommPort[i-1]);
>
> dcb.BaudRate = 57600; // set the baud rate
> dcb.ByteSize = 8; // data size, xmit, and rcv
> dcb.Parity = NOPARITY; // no parity bit
> dcb.StopBits = ONESTOPBIT; // one stop bit
>
> fSuccess = SetCommState(
> hCom, // handle to comm device
> &dcb // device-control block
> );
>
> if (!fSuccess)
> {
> printf ("\nSetCommState failed with error %d.", GetLastError());
> break;
> }
> printf ("\nSetCommState succeeded at %s!.", pcCommPort[i-1]);
>
>
> //////////////////////
> // Write/Read Modem //
> //////////////////////
>
> strcpy(mdmData, "ATDT3891004\r\n");
>
> fSuccess = WriteFile(
> hCom,
> mdmData,
> (DWORD)strlen(mdmData),
> &bytesRW,
> 0
> );
>
> if (!fSuccess)
> {
> // Handle the error.
> printf ("\nWriteFile failed with error %d.", GetLastError());
> break;
> }
> printf ("\nWriteFile succeeded at %s.", pcCommPort[i-1]);
> printf("\nDialing...");
> Sleep(20000);
>
> while(1)
> {
> memset(mdmData, '\0', sizeof(mdmData));
> fSuccess = ReadFile(
> hCom,
> mdmData,
> sizeof(mdmData),
> &bytesRW,
> 0
> );
>
> if (!fSuccess)
> {
> // Handle the error.
> printf ("\nReadFile failed with error %d.", GetLastError());
> }
> else
> {
> printf ("\nReadFile succeeded at %s.", pcCommPort[i-1]);
> printf("%s", mdmData);
> strupr(mdmData);
>
> if(strstr(mdmData, "ENTER USERNAME"))
> {
> memset(mdmData, '\0', sizeof(mdmData));
> scanf("%s",mdmData);
> strcat(mdmData, "\r\n");
> fSuccess = WriteFile(
> hCom,
> mdmData,
> (DWORD)strlen(mdmData),
> &bytesRW,
> 0
> );
>
> }
> else if(strstr(mdmData, "ENTER YOUR PASSWORD"))
> {
> memset(mdmData, '\0', sizeof(mdmData));
> scanf("%s",mdmData);
> strcat(mdmData, "\r\n");
> fSuccess = WriteFile(
> hCom,
> mdmData,
> (DWORD)strlen(mdmData),
> &bytesRW,
> 0
> );
> }
> }
> }
> }
>
> if (hCom != INVALID_HANDLE_VALUE)
> {
> CloseHandle(hCom);
> }
>
> return(0);
> }
 
whats wrong with my code?

#include "conio.h"
#include "stdio.h"
unsigned short a;
main()
{
a=int _inp(0x3f8);
printf("%d",a);
}

no syntax error or compilation error.
but when it executes the a=int _inp(0x3f8); windows is giving the usual message that sorry for inconvenience with the option buttons send error report, debug,dont send.Is it a problem with vc++ or my system. But my system is working good.it is irrespective of the port number.can anyone help me out.
 
V

Vladimir E. Zyubin

Hello automation,

AFAIK, your code will work only for Win 9x... NT/2000/XP do not allow direct access to hardware... you need a driver, or a more user-friendly OS.

--
Best regards.
= Vladimir E. Zyubin
 
H

Heinz-Juergen Oertel

Depending of the maturity of the operating system you are using, a call to read or write to an I/O port, like _inp(0x3f8), should not be possible for a standard user. I'm not a Windoze expert, but as far as I know on Operating Systems better than WIN95 ( That is Win98, Win XP, Win NT, Linux - in that order), this call is not possible without using a device driver. Which is an perfect behaviour.

Regards
Heinz
 
1. Create a dialog based mfc exe.
2. Go to Project >> Add To Project >> Components and Controls command from the menu
3. Select Registered ActiveX Controls and then choose 'Microsoft Communications Control, version 6.0.'
4. Click insert and close
5. You will see a telephone icon in the toolbar.
6. Drag it inside ur dialog.
7. Set its properties like com1,baud,bits...
8. Add a member variable to this object (mComm).
9. In the header file of ur dialog
#include "atlbase.h"
10. In the c++ implementation file of ur dialog,
in the code, such as onOkButton() try this following code:

CString packet;
CComVariant input;

mComm.SetPortOpen(true);
input.Clear();
//receive 100 characters
for(int i=0;i<100;i++)
{
input = mComm.GetInput();
CString s = input.bstrVal;
packet+= s;
}
MessageBox(packet);
 
Dear Friend,

After I create the Microsoft Communications Control and create the instance for MSCOMM its giving error like "error C2143: syntax error : missing ';' before 'constant'"

Please help me for the Above.

Thanks and Regards
Ajith
 
This is a simple syntax error in C++, copy the line referred to and the line or two above it and the problem will likely be clear.

The first thing to do is try putting a ";" at the end of the line preceding the line referenced in the error message.

Rufus
 
Top