| | |
Serial port communication using C++
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2006
Posts: 3
Reputation:
Solved Threads: 0
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++ )
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++ )
C++ Syntax (Toggle Plain Text)
Serial.h CODE 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 <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 CODE #include <iostream> #include <conio.h> #include <stdio.h> #include <time.h> #include <windows.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; }
Last edited by Salem; Sep 27th, 2006 at 1:40 pm. Reason: Added code tags - learn to use them yourself
Maybe looking here would help:
http://www.control.com/1026187785/index_html
http://www.lvr.com/serport.htm
http://www.beyondlogic.org/serial/serial1.htm
http://www.daniweb.com/techtalkforums/thread27589.html
Hope it helped , bye.
http://www.control.com/1026187785/index_html
http://www.lvr.com/serport.htm
http://www.beyondlogic.org/serial/serial1.htm
http://www.daniweb.com/techtalkforums/thread27589.html
Hope it helped , bye.
Last edited by ~s.o.s~; Sep 27th, 2006 at 1:01 pm.
I don't accept change; I don't deserve to live.
> 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
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
•
•
Join Date: Sep 2006
Posts: 3
Reputation:
Solved Threads: 0
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......
>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......
•
•
Join Date: Oct 2006
Posts: 1
Reputation:
Solved Threads: 0
•
•
•
•
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++ )
C++ Syntax (Toggle Plain Text)
Serial.h CODE 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 <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 CODE #include <iostream> #include <conio.h> #include <stdio.h> #include <time.h> #include <windows.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; }
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
•
•
Join Date: Nov 2007
Posts: 3
Reputation:
Solved Threads: 0
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
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
![]() |
Similar Threads
- serial port component for dephi 7 (Pascal and Delphi)
- Send data on a serial port (C++)
- Serial Port Communication (Assembly)
- Problem in developing an Serial Communication Interface (Visual Basic 4 / 5 / 6)
- Serial Port (C++)
- Serial Port (C++)
Other Threads in the C++ Forum
- Previous Thread: form control
- Next Thread: Loading image Borland c++ 5.5
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets







...