Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,402 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,108 people online right now. Registration is fast and FREE... Join Now!




Serial Programming in C++-Checking output

 
Reply to this topicStart new topic

Serial Programming in C++-Checking output

totally_lost
21 Sep, 2006 - 05:24 PM
Post #1

New D.I.C Head
*

Joined: 13 Sep, 2006
Posts: 9



Thanked: 1 times
My Contributions
hey...........

I managed to compile the below code in Dev C++.Its a program to intialize a serial port and write to it.

The program is compiling. The problem i am facing now is......... how do I check weather it is running properly. I m simply initializing the serial port and writing the letter 'a' to it.

Acctually ...can I use a digital oscilloscope and check the waveform pattern in it???????????

11 01000001 0 = stop bits / data bits / start bit

would I be able to see a waveform pattern in the oscilloscope???????

For example......I am using a Dell Laptop. I connect the serial port of the Dell laptop to a digital oscilloscope.Then observe the digital waveform pattern........Do you think it can be done. If so how long will the waveform pattern last.

Are there any other ways to verify weather the program is writing to the serial port?

I would greatly appreciate any help u guys can give....

the code is below (compiles without errors on Dev C++ )



Serial.h
CODE
// Flow control flags

#define FC_DTRDSR 0x01
#define FC_RTSCTS 0x02
#define FC_XONXOFF 0x04

// ascii definitions
#include <stdio.h>
#include <time.h>

//#define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers

//#include <afxwin.h> // serial.cpp : Defines the entry point for the console application.

//#include "stdafx.h"
#include <string.h>

#define ASCII_BEL 0x07
#define ASCII_BS 0x08
#define ASCII_LF 0x0A
#define ASCII_CR 0x0D
#define ASCII_XON 0x11
#define ASCII_XOFF 0x13


HANDLE SerialInit(char*, int);

char SerialGetc(HANDLE*);

void SerialPutc(HANDLE*, char);

--------------------------------------------------------------------------


Serial.cpp
CODE
#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <time.h>
#include <windows.h>

//#define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers
//#include <bios.h>
//#include <afxwin.h> // serial.cpp : Defines the entry point for the console application.

//#include "stdafx.h"
#include <string.h>
#include "serial.h"

// Flow control flags

#define FC_DTRDSR 0x01
#define FC_RTSCTS 0x02
#define FC_XONXOFF 0x04

// ascii definitions

#define ASCII_BEL 0x07
#define ASCII_BS 0x08
#define ASCII_LF 0x0A
#define ASCII_CR 0x0D
#define ASCII_XON 0x11
#define ASCII_XOFF 0x13
using namespace std;
// variables used with the com port
BOOL bPortReady;
DCB dcb;
COMMTIMEOUTS CommTimeouts;
BOOL bWriteRC;
BOOL bReadRC;
DWORD iBytesWritten;
DWORD iBytesRead;

HANDLE SerialInit(char *ComPortName, int BaudRate)
{
HANDLE hCom;

hCom = CreateFile(ComPortName,
GENERIC_READ | GENERIC_WRITE,
0, // exclusive access
NULL, // no security
OPEN_EXISTING,
0, // no overlapped I/O
NULL); // null template

bPortReady = SetupComm(hCom, 2, 128); // set buffer sizes


bPortReady = GetCommState(hCom, &dcb);
dcb.BaudRate = BaudRate;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
// dcb.Parity = EVENPARITY;
dcb.StopBits = ONESTOPBIT;
dcb.fAbortOnError = TRUE;

// set XON/XOFF
dcb.fOutX = FALSE; // XON/XOFF off for transmit
dcb.fInX = FALSE; // XON/XOFF off for receive
// set RTSCTS
dcb.fOutxCtsFlow = TRUE; // turn on CTS flow control
dcb.fRtsControl = RTS_CONTROL_HANDSHAKE; //
// set DSRDTR
dcb.fOutxDsrFlow = FALSE; // turn on DSR flow control
dcb.fDtrControl = DTR_CONTROL_ENABLE; //
// dcb.fDtrControl = DTR_CONTROL_DISABLE; //
// dcb.fDtrControl = DTR_CONTROL_HANDSHAKE; //

bPortReady = SetCommState(hCom, &dcb);

// Communication timeouts are optional

bPortReady = GetCommTimeouts (hCom, &CommTimeouts);

CommTimeouts.ReadIntervalTimeout = 5000;
CommTimeouts.ReadTotalTimeoutConstant = 5000;
CommTimeouts.ReadTotalTimeoutMultiplier = 1000;
CommTimeouts.WriteTotalTimeoutConstant = 5000;
CommTimeouts.WriteTotalTimeoutMultiplier = 1000;

bPortReady = SetCommTimeouts (hCom, &CommTimeouts);

return hCom;
}

char SerialGetc(HANDLE *hCom)
{
char rxchar;
BOOL bReadRC;
static DWORD iBytesRead;

bReadRC = ReadFile(*hCom, &rxchar, 1, &iBytesRead, NULL);

return rxchar;
}

void SerialPutc(HANDLE *hCom, char txchar)
{
BOOL bWriteRC;
static DWORD iBytesWritten;

bWriteRC = WriteFile(*hCom, &txchar, 1, &iBytesWritten,NULL);

return;
}

int main()
{
HANDLE my=SerialInit("com1",1200);
char letter;

HANDLE *ptr;
*ptr=my;
SerialPutc(ptr,'a');
//letter=SerialGetc(ptr);
getch();

return 0;

}
[s]
User is offlineProfile CardPM
+Quote Post

Xing
RE: Serial Programming In C++-Checking Output
21 Sep, 2006 - 05:29 PM
Post #2

D.I.C Addict
Group Icon

Joined: 22 Jul, 2006
Posts: 723



Thanked: 2 times
Dream Kudos: 1575
My Contributions
Yes, you can use oscilloscopes to check the output. You can also use led's. Add some delays and see the variations. Before interfacing your ports with any hardware device make sure you protect your port from back currents.

PS: Use code tags from next time
User is offlineProfile CardPM
+Quote Post

horace
RE: Serial Programming In C++-Checking Output
25 Oct, 2006 - 06:20 AM
Post #3

D.I.C Addict
Group Icon

Joined: 25 Oct, 2006
Posts: 573



Thanked: 4 times
Dream Kudos: 50
My Contributions
The simplest way to is connect your PC back to back with another PC using a null modem cable (see http://www.nullmodem.com/NullModem.htm) – in fact if you switch off all handshaking you can just cross pins 2 and 3. run Hyperterm or TerraTerm on the other PC to check your output.

You program works OK sending and receiving characters – the version below acts as a simple terminal – characters typed on keyboard are transmitted over serial line and characters received are displayed

CODE

#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <time.h>
#include <windows.h>

//#define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers
//#include <bios.h>
//#include <afxwin.h> // serial.cpp : Defines the entry point for the console application.

//#include "stdafx.h"
#include <string.h>
#include "serial.h"

// Flow control flags

#define FC_DTRDSR 0x01
#define FC_RTSCTS 0x02
#define FC_XONXOFF 0x04

// ascii definitions

#define ASCII_BEL 0x07
#define ASCII_BS 0x08
#define ASCII_LF 0x0A
#define ASCII_CR 0x0D
#define ASCII_XON 0x11
#define ASCII_XOFF 0x13

using namespace std;
// variables used with the com port
BOOL bPortReady;
DCB dcb;
COMMTIMEOUTS CommTimeouts;
BOOL bWriteRC;
BOOL bReadRC;
DWORD iBytesWritten;
DWORD iBytesRead;

HANDLE SerialInit(char *ComPortName, int BaudRate)
{
HANDLE hCom;

hCom = CreateFile(ComPortName,
GENERIC_READ | GENERIC_WRITE,
0, // exclusive access
NULL, // no security
OPEN_EXISTING,
0, // no overlapped I/O
NULL); // null template

bPortReady = SetupComm(hCom, 2, 128); // set buffer sizes


bPortReady = GetCommState(hCom, &dcb);
dcb.BaudRate = BaudRate;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
// dcb.Parity = EVENPARITY;
dcb.StopBits = ONESTOPBIT;
dcb.fAbortOnError = TRUE;

// set XON/XOFF
dcb.fOutX = FALSE; // XON/XOFF off for transmit
dcb.fInX = FALSE; // XON/XOFF off for receive
// set RTSCTS
dcb.fOutxCtsFlow = FALSE;//TRUE; // turn on CTS flow control
dcb.fRtsControl = FALSE;// RTS_CONTROL_HANDSHAKE; //
// set DSRDTR
dcb.fOutxDsrFlow = FALSE; // turn on DSR flow control
//dcb.fDtrControl = DTR_CONTROL_ENABLE; //
dcb.fDtrControl = DTR_CONTROL_DISABLE; //
// dcb.fDtrControl = DTR_CONTROL_HANDSHAKE; //

bPortReady = SetCommState(hCom, &dcb);

// Communication timeouts are optional

bPortReady = GetCommTimeouts (hCom, &CommTimeouts);

CommTimeouts.ReadIntervalTimeout = 5;
CommTimeouts.ReadTotalTimeoutConstant = 5;
CommTimeouts.ReadTotalTimeoutMultiplier = 1;
CommTimeouts.WriteTotalTimeoutConstant = 5;
CommTimeouts.WriteTotalTimeoutMultiplier = 1;

bPortReady = SetCommTimeouts (hCom, &CommTimeouts);

return hCom;
}

char SerialGetc(HANDLE *hCom)
{
char rxchar;
BOOL bReadRC;
static DWORD iBytesRead;

bReadRC = ReadFile(*hCom, &rxchar, 1, &iBytesRead, NULL);

if (iBytesRead) return rxchar; else return 0;         // return 0 if no character read
}

void SerialPutc(HANDLE *hCom, char txchar)
{
BOOL bWriteRC;
static DWORD iBytesWritten;

bWriteRC = WriteFile(*hCom, &txchar, 1, &iBytesWritten,NULL);

return;
}

#include <conio.h>

int main()
{
HANDLE my=SerialInit("com1",1200);
char letter;
cout << "serial " << endl;
HANDLE *ptr;
*ptr=my;
while(1)
  {
   if (kbhit())  SerialPutc(ptr, getche());                   // if keyboard hit read character and transmit it  
   if((letter=SerialGetc(ptr))>0) putchar(letter);          // if character received display it
}
getch();

return 0;
}

User is offlineProfile CardPM
+Quote Post

gregoryH
RE: Serial Programming In C++-Checking Output
26 Oct, 2006 - 02:57 AM
Post #4

D.I.C Regular
Group Icon

Joined: 4 Oct, 2006
Posts: 417


Dream Kudos: 50
My Contributions
QUOTE(totally_lost @ 21 Sep, 2006 - 06:24 PM) *

hey...........

I managed to compile the below code in Dev C++.Its a program to intialize a serial port and write to it.

The program is compiling. The problem i am facing now is......... how do I check weather it is running properly. I m simply initializing the serial port and writing the letter 'a' to it.

Acctually ...can I use a digital oscilloscope and check the waveform pattern in it???????????

11 01000001 0 = stop bits / data bits / start bit

would I be able to see a waveform pattern in the oscilloscope???????

For example......I am using a Dell Laptop. I connect the serial port of the Dell laptop to a digital oscilloscope.Then observe the digital waveform pattern........Do you think it can be done. If so how long will the waveform pattern last.

Are there any other ways to verify weather the program is writing to the serial port?

I would greatly appreciate any help u guys can give....

the code is below (compiles without errors on Dev C++ )


I believe the solution yuou seek is to use a "loop back" connector... they are used when testing the serial port and would be the quickest way to test.

If you really must use a CRO, make sure you can store the waveform.. simple way to determine the time is to:
1/baud * (databits + stopbits )

to give you the approximate time the data will be present on the TX pin.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 03:11AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month