Serial port communication using C++

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2006
Posts: 3
Reputation: prashw is an unknown quantity at this point 
Solved Threads: 0
prashw prashw is offline Offline
Newbie Poster

Serial port communication using C++

 
1
  #1
Sep 27th, 2006
hey all.........i m new here!!!!!!

I am at my wits end on this problem.....i would appreciate it if you could give any help.....................................................

i managed to compile and run a C++ program that intializes and reads/writes to a serial port . I was using the Dev C++ compiler to write the code.


Now I am trying to verify the output.What I did was to write the letter 'a' to the serial port. The serial port is connected to a digital oscilloscope and I intend to see the voltage patterns that correspond to the letter 'a' as below.

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


But this voltage pattern is not shown in the oscilloscope. Instead I only see the handshaking signal (a data bit '1') coming from the serial port.( I was able to see the handshaking control signal/initializing signal but not the data signal (the data signal was the letter 'a') ).
I saw a '1' from pin 3 and pin 6 of the R232 serial port when I ran the program. These pins correspond to Transmit data, Data set ready signals that are sent from the laptop to the oscilloscope.

1. I cannot see any data signals corresponding to the letter 'a'. What is the reason behind this????
Is it because there is no reply from the oscilloscope for the handshaking signal the laptop sends to it?? (i am using a laptop to run the program)
(Meaning the oscilloscope fails to send a handshking signal back to the laptop......this might indeed be the case because oscilloscopes are not built to send handshking signals)

2.If so how can I bypass sending handshking signals and simply send the letter 'a' to the oscilloscope and view its voltage pattern???

3.Can I do this by a simple adjustemnt to the code I have written below????



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

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


  1. Serial.h
  2. CODE
  3. CODE// Flow control flags
  4.  
  5. #define FC_DTRDSR 0x01
  6. #define FC_RTSCTS 0x02
  7. #define FC_XONXOFF 0x04
  8.  
  9. // ascii definitions
  10. #include <stdio.h>
  11. #include <time.h>
  12.  
  13. //#define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers
  14. #include <string.h>
  15.  
  16. #define ASCII_BEL 0x07
  17. #define ASCII_BS 0x08
  18. #define ASCII_LF 0x0A
  19. #define ASCII_CR 0x0D
  20. #define ASCII_XON 0x11
  21. #define ASCII_XOFF 0x13
  22.  
  23.  
  24. HANDLE SerialInit(char*, int);
  25.  
  26. char SerialGetc(HANDLE*);
  27.  
  28. void SerialPutc(HANDLE*, char);
  29.  
  30.  
  31. --------------------------------------------------------------------------
  32.  
  33.  
  34. Serial.cpp
  35. CODE
  36. CODE
  37. #include <iostream>
  38. #include <conio.h>
  39. #include <stdio.h>
  40. #include <time.h>
  41. #include <windows.h>
  42. #include <string.h>
  43. #include "serial.h"
  44.  
  45. // Flow control flags
  46.  
  47. #define FC_DTRDSR 0x01
  48. #define FC_RTSCTS 0x02
  49. #define FC_XONXOFF 0x04
  50.  
  51. // ascii definitions
  52.  
  53. #define ASCII_BEL 0x07
  54. #define ASCII_BS 0x08
  55. #define ASCII_LF 0x0A
  56. #define ASCII_CR 0x0D
  57. #define ASCII_XON 0x11
  58. #define ASCII_XOFF 0x13
  59. using namespace std;
  60. // variables used with the com port
  61. BOOL bPortReady;
  62. DCB dcb;
  63. COMMTIMEOUTS CommTimeouts;
  64. BOOL bWriteRC;
  65. BOOL bReadRC;
  66. DWORD iBytesWritten;
  67. DWORD iBytesRead;
  68.  
  69. HANDLE SerialInit(char *ComPortName, int BaudRate)
  70. {
  71. HANDLE hCom;
  72.  
  73. hCom = CreateFile(ComPortName,
  74. GENERIC_READ | GENERIC_WRITE,
  75. 0, // exclusive access
  76. NULL, // no security
  77. OPEN_EXISTING,
  78. 0, // no overlapped I/O
  79. NULL); // null template
  80.  
  81. bPortReady = SetupComm(hCom, 2, 128); // set buffer sizes
  82.  
  83.  
  84. bPortReady = GetCommState(hCom, &dcb);
  85. dcb.BaudRate = BaudRate;
  86. dcb.ByteSize = 8;
  87. dcb.Parity = NOPARITY;
  88. // dcb.Parity = EVENPARITY;
  89. dcb.StopBits = ONESTOPBIT;
  90. dcb.fAbortOnError = TRUE;
  91.  
  92. // set XON/XOFF
  93. dcb.fOutX = FALSE; // XON/XOFF off for transmit
  94. dcb.fInX = FALSE; // XON/XOFF off for receive
  95. // set RTSCTS
  96. dcb.fOutxCtsFlow = TRUE; // turn on CTS flow control
  97. dcb.fRtsControl = RTS_CONTROL_HANDSHAKE; //
  98. // set DSRDTR
  99. dcb.fOutxDsrFlow = FALSE; // turn on DSR flow control
  100. dcb.fDtrControl = DTR_CONTROL_ENABLE; //
  101. // dcb.fDtrControl = DTR_CONTROL_DISABLE; //
  102. // dcb.fDtrControl = DTR_CONTROL_HANDSHAKE; //
  103.  
  104. bPortReady = SetCommState(hCom, &dcb);
  105.  
  106. // Communication timeouts are optional
  107.  
  108. bPortReady = GetCommTimeouts (hCom, &CommTimeouts);
  109.  
  110. CommTimeouts.ReadIntervalTimeout = 5000;
  111. CommTimeouts.ReadTotalTimeoutConstant = 5000;
  112. CommTimeouts.ReadTotalTimeoutMultiplier = 1000;
  113. CommTimeouts.WriteTotalTimeoutConstant = 5000;
  114. CommTimeouts.WriteTotalTimeoutMultiplier = 1000;
  115.  
  116. bPortReady = SetCommTimeouts (hCom, &CommTimeouts);
  117.  
  118. return hCom;
  119. }
  120.  
  121. char SerialGetc(HANDLE *hCom)
  122. {
  123. char rxchar;
  124. BOOL bReadRC;
  125. static DWORD iBytesRead;
  126.  
  127. bReadRC = ReadFile(*hCom, &rxchar, 1, &iBytesRead, NULL);
  128.  
  129. return rxchar;
  130. }
  131.  
  132. void SerialPutc(HANDLE *hCom, char txchar)
  133. {
  134. BOOL bWriteRC;
  135. static DWORD iBytesWritten;
  136.  
  137. bWriteRC = WriteFile(*hCom, &txchar, 1, &iBytesWritten,NULL);
  138.  
  139. return;
  140. }
  141.  
  142. int main()
  143. {
  144. HANDLE my=SerialInit("com1",1200);
  145. char letter;
  146.  
  147. HANDLE *ptr;
  148. *ptr=my;
  149. SerialPutc(ptr,'a');
  150. //letter=SerialGetc(ptr);
  151. getch();
  152.  
  153. return 0;
  154.  
  155. }
Last edited by Salem; Sep 27th, 2006 at 1:40 pm. Reason: Added code tags - learn to use them yourself
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Serial port communication using C++

 
0
  #2
Sep 27th, 2006
I always used another PC (or laptop) that ran a program to read the data from the serial port. Didn't have a problem, unless the ports were set up differently.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,609
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 464
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Serial port communication using C++

 
0
  #3
Sep 27th, 2006
Last edited by ~s.o.s~; Sep 27th, 2006 at 1:01 pm.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Serial port communication using C++

 
1
  #4
Sep 27th, 2006
> But this voltage pattern is not shown in the oscilloscope.
Did you have the right time-base on the scope, compatible with the baud rate of the serial line?

Is it a storage scope, set to capture on the first bit of data? You only get one shot at this, so if you miss it, you won't see it.

You probably have to ground the CTS (Clear To Send) line as well to make the transmitter (your laptop) send the data.

If possible, get two machines wired together (or two serial ports on your single machine) with a NULL-MODEM cable (see link), then watch the lines with your scope. Once you understand what all the signals are doing, then you can 'spoof' a single ended connection which will just absorb data you can see with the scope.

http://www.zytrax.com/tech/layer_1/c...tech_rs232.htm
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 3
Reputation: prashw is an unknown quantity at this point 
Solved Threads: 0
prashw prashw is offline Offline
Newbie Poster

Re: Serial port communication using C++

 
1
  #5
Sep 28th, 2006
Thanx evryone for ur help...

>Did you have the right time-base on the scope, compatible with the baud rate of the serial line?
Yeps ....adjusted the timeline and view the handshaking signal in various compressed forms

Is it a storage scope, set to capture on the first bit of data? You only get one shot at this, so if you miss it, you won't see it.

Hmmm normal oscilloscope....as u said the output lasts for a few minutes


You probably have to ground the CTS (Clear To Send) line as well to make the transmitter (your laptop) send the data.

This i will give a shot............maybe would work.....


I always used another PC (or laptop) that ran a program to read the data from the serial port. Didn't have a problem, unless the ports were set up differently.

This also I will try...........do i need to run the same program simultaneously on both the laptops??? for example one labtop would run
void SerialPutc(HANDLE*, char);

the other would run

char SerialGetc(HANDLE*);

Thanx a million for your help......


Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 148
Reputation: Micko is on a distinguished road 
Solved Threads: 6
Micko Micko is offline Offline
Junior Poster

Re: Serial port communication using C++

 
1
  #6
Sep 29th, 2006
Please, can you post you results and conclusion?
Did you manage to see voltage pattern for 'a'?
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 3
Reputation: prashw is an unknown quantity at this point 
Solved Threads: 0
prashw prashw is offline Offline
Newbie Poster

Re: Serial port communication using C++

 
0
  #7
Oct 3rd, 2006
Hi,
Sorry for replying late.........

Still couldnt get the pattern from the oscilloscope...........going to try with 2 laptops running the same programme....
will post my result if i am successful ...

this forum rocks!!!!!
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 1
Reputation: mikeranda11 is an unknown quantity at this point 
Solved Threads: 0
mikeranda11 mikeranda11 is offline Offline
Newbie Poster

Re: Serial port communication using C++

 
0
  #8
Oct 11th, 2006
Originally Posted by prashw View Post
hey all.........i m new here!!!!!!

I am at my wits end on this problem.....i would appreciate it if you could give any help.....................................................

i managed to compile and run a C++ program that intializes and reads/writes to a serial port . I was using the Dev C++ compiler to write the code.


Now I am trying to verify the output.What I did was to write the letter 'a' to the serial port. The serial port is connected to a digital oscilloscope and I intend to see the voltage patterns that correspond to the letter 'a' as below.

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


But this voltage pattern is not shown in the oscilloscope. Instead I only see the handshaking signal (a data bit '1') coming from the serial port.( I was able to see the handshaking control signal/initializing signal but not the data signal (the data signal was the letter 'a') ).
I saw a '1' from pin 3 and pin 6 of the R232 serial port when I ran the program. These pins correspond to Transmit data, Data set ready signals that are sent from the laptop to the oscilloscope.

1. I cannot see any data signals corresponding to the letter 'a'. What is the reason behind this????
Is it because there is no reply from the oscilloscope for the handshaking signal the laptop sends to it?? (i am using a laptop to run the program)
(Meaning the oscilloscope fails to send a handshking signal back to the laptop......this might indeed be the case because oscilloscopes are not built to send handshking signals)

2.If so how can I bypass sending handshking signals and simply send the letter 'a' to the oscilloscope and view its voltage pattern???

3.Can I do this by a simple adjustemnt to the code I have written below????



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

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


  1. Serial.h
  2. CODE
  3. CODE// Flow control flags
  4.  
  5. #define FC_DTRDSR 0x01
  6. #define FC_RTSCTS 0x02
  7. #define FC_XONXOFF 0x04
  8.  
  9. // ascii definitions
  10. #include <stdio.h>
  11. #include <time.h>
  12.  
  13. //#define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers
  14. #include <string.h>
  15.  
  16. #define ASCII_BEL 0x07
  17. #define ASCII_BS 0x08
  18. #define ASCII_LF 0x0A
  19. #define ASCII_CR 0x0D
  20. #define ASCII_XON 0x11
  21. #define ASCII_XOFF 0x13
  22.  
  23.  
  24. HANDLE SerialInit(char*, int);
  25.  
  26. char SerialGetc(HANDLE*);
  27.  
  28. void SerialPutc(HANDLE*, char);
  29.  
  30.  
  31. --------------------------------------------------------------------------
  32.  
  33.  
  34. Serial.cpp
  35. CODE
  36. CODE
  37. #include <iostream>
  38. #include <conio.h>
  39. #include <stdio.h>
  40. #include <time.h>
  41. #include <windows.h>
  42. #include <string.h>
  43. #include "serial.h"
  44.  
  45. // Flow control flags
  46.  
  47. #define FC_DTRDSR 0x01
  48. #define FC_RTSCTS 0x02
  49. #define FC_XONXOFF 0x04
  50.  
  51. // ascii definitions
  52.  
  53. #define ASCII_BEL 0x07
  54. #define ASCII_BS 0x08
  55. #define ASCII_LF 0x0A
  56. #define ASCII_CR 0x0D
  57. #define ASCII_XON 0x11
  58. #define ASCII_XOFF 0x13
  59. using namespace std;
  60. // variables used with the com port
  61. BOOL bPortReady;
  62. DCB dcb;
  63. COMMTIMEOUTS CommTimeouts;
  64. BOOL bWriteRC;
  65. BOOL bReadRC;
  66. DWORD iBytesWritten;
  67. DWORD iBytesRead;
  68.  
  69. HANDLE SerialInit(char *ComPortName, int BaudRate)
  70. {
  71. HANDLE hCom;
  72.  
  73. hCom = CreateFile(ComPortName,
  74. GENERIC_READ | GENERIC_WRITE,
  75. 0, // exclusive access
  76. NULL, // no security
  77. OPEN_EXISTING,
  78. 0, // no overlapped I/O
  79. NULL); // null template
  80.  
  81. bPortReady = SetupComm(hCom, 2, 128); // set buffer sizes
  82.  
  83.  
  84. bPortReady = GetCommState(hCom, &dcb);
  85. dcb.BaudRate = BaudRate;
  86. dcb.ByteSize = 8;
  87. dcb.Parity = NOPARITY;
  88. // dcb.Parity = EVENPARITY;
  89. dcb.StopBits = ONESTOPBIT;
  90. dcb.fAbortOnError = TRUE;
  91.  
  92. // set XON/XOFF
  93. dcb.fOutX = FALSE; // XON/XOFF off for transmit
  94. dcb.fInX = FALSE; // XON/XOFF off for receive
  95. // set RTSCTS
  96. dcb.fOutxCtsFlow = TRUE; // turn on CTS flow control
  97. dcb.fRtsControl = RTS_CONTROL_HANDSHAKE; //
  98. // set DSRDTR
  99. dcb.fOutxDsrFlow = FALSE; // turn on DSR flow control
  100. dcb.fDtrControl = DTR_CONTROL_ENABLE; //
  101. // dcb.fDtrControl = DTR_CONTROL_DISABLE; //
  102. // dcb.fDtrControl = DTR_CONTROL_HANDSHAKE; //
  103.  
  104. bPortReady = SetCommState(hCom, &dcb);
  105.  
  106. // Communication timeouts are optional
  107.  
  108. bPortReady = GetCommTimeouts (hCom, &CommTimeouts);
  109.  
  110. CommTimeouts.ReadIntervalTimeout = 5000;
  111. CommTimeouts.ReadTotalTimeoutConstant = 5000;
  112. CommTimeouts.ReadTotalTimeoutMultiplier = 1000;
  113. CommTimeouts.WriteTotalTimeoutConstant = 5000;
  114. CommTimeouts.WriteTotalTimeoutMultiplier = 1000;
  115.  
  116. bPortReady = SetCommTimeouts (hCom, &CommTimeouts);
  117.  
  118. return hCom;
  119. }
  120.  
  121. char SerialGetc(HANDLE *hCom)
  122. {
  123. char rxchar;
  124. BOOL bReadRC;
  125. static DWORD iBytesRead;
  126.  
  127. bReadRC = ReadFile(*hCom, &rxchar, 1, &iBytesRead, NULL);
  128.  
  129. return rxchar;
  130. }
  131.  
  132. void SerialPutc(HANDLE *hCom, char txchar)
  133. {
  134. BOOL bWriteRC;
  135. static DWORD iBytesWritten;
  136.  
  137. bWriteRC = WriteFile(*hCom, &txchar, 1, &iBytesWritten,NULL);
  138.  
  139. return;
  140. }
  141.  
  142. int main()
  143. {
  144. HANDLE my=SerialInit("com1",1200);
  145. char letter;
  146.  
  147. HANDLE *ptr;
  148. *ptr=my;
  149. SerialPutc(ptr,'a');
  150. //letter=SerialGetc(ptr);
  151. getch();
  152.  
  153. return 0;
  154.  
  155. }
This bit of code is the problem:
HANDLE *ptr;
*ptr=my;
SerialPutc(ptr,'a');
the middle line should be
ptr = &my;
*ptr has not been allocated memory. It is ptr you want to assign.

It seems to work with this change.
Cheers - Mike
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 4
Reputation: fhshockey05d is an unknown quantity at this point 
Solved Threads: 0
fhshockey05d fhshockey05d is offline Offline
Newbie Poster

Re: Serial port communication using C++

 
0
  #9
Aug 12th, 2007
I need to send short strings, such as 'GH' and 'GM' to a CMUcam2 serially through hyperterminal also using dev c++. Would this code be able to do this? How do I use the code in hyperterminal? I'm more on the beginner side of programming.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3
Reputation: Oyseka is an unknown quantity at this point 
Solved Threads: 0
Oyseka Oyseka is offline Offline
Newbie Poster

Re: Serial port communication using C++

 
0
  #10
Nov 7th, 2007
Hi to all

I have started to use Visual Studio (C++) and having problems to use the "serialPort". Till now I have used Borland with custom Active-x (Binary/Byte operations were no problem). I have managed to 'Read' bytes, but the 'Write' is really throwing me. The Read can read a byte at-a-time, but the Write uses char arrays that I can't get my teeth around.

Any one seen some C++ code snipets that can write BINARY data to the serialPort object? I need to write either an array or byte by byte. The data in NOT ASCII....

Much appericiated

Cheers Guys
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC