954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Serial port communication using C++

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++ )

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;

}
prashw
Newbie Poster
3 posts since Sep 2006
Reputation Points: 18
Solved Threads: 0
 

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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 
~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

> 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/cables/tech_rs232.htm

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

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......

prashw
Newbie Poster
3 posts since Sep 2006
Reputation Points: 18
Solved Threads: 0
 

Please, can you post you results and conclusion?
Did you manage to see voltage pattern for 'a'?

Micko
Junior Poster
148 posts since Aug 2005
Reputation Points: 55
Solved Threads: 6
 

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!!!!!

prashw
Newbie Poster
3 posts since Sep 2006
Reputation Points: 18
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++ )

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;
 
}



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

mikeranda11
Newbie Poster
1 post since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

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.

fhshockey05d
Newbie Poster
4 posts since Aug 2007
Reputation Points: 10
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

Oyseka
Newbie Poster
3 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

Use stty, you must be using wrong baud rate or some other configuration must be wrong.

ithelp
Nearly a Posting Maven
Banned
2,230 posts since May 2006
Reputation Points: 769
Solved Threads: 128
 

>>but the Write uses char arrays that I can't get my teeth around.

you can use win32 api functions to do that -- open the serial port with CreateFile(), then use WriteFile() to write the data and ReadFile() to read it just as you would any normal file on your computer. There are a whole bunch of other functions to configure the serial port and wait for incoming data. See links in MSDN under CreateFile for more detailed information about this.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 
Use stty, you must be using wrong baud rate or some other configuration must be wrong.


Thanks

Baud rate fine - as I can Rx data.

Tx Code looks like :
serialPort1->Write (bytes, (int)start, (int)len);

The problem is in the Tx - It changes non-ASCII (ie 0x80 to 0xFF Hex) bytes to either "?" or 2-byte (or 4-byte) chars. I tried this:

serialPort1->Encoding = Encoding::UTF8;

but it just replaces a non-ASCII with 2-bytes.

Also I have not managed to get it to accept a pointer for the array:

declare: char bytes[size of my buffer];
bytes = some data;
serialPort1->Write (bytes,0,len of data); (will not compile) This would seem the 'lowest' level of writing data to the port??

Maybe MICROSOFT did not consider engineering type applications on .Net's Serial Object??

If anyone can shed some light here, then I will post a code example for other 'dummies' like myself to use......

Cheers to all

Oyseka
Newbie Poster
3 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

howto convert thi program to Microsoft visual c++......
please help me.......
i don't know how to convert.............
please help me..........

Serial.h

// 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;
 
}
sardi
Newbie Poster
5 posts since Dec 2007
Reputation Points: 10
Solved Threads: 0
 

i just tried this code in Visual C++ 6.0, and it works perfectly, so you should be fine as well, if you're having problems that might be because ur not including all of ur ".h" files properly. In order to do that I would recommend to include all of ur .h files in "stdafx.h", then include stdafx.h in the file that holds ur "main()" function, it should work that way, that's what i did.

howto convert thi program to Microsoft visual c++...... please help me....... i don't know how to convert............. please help me..........

Serial.h CODE CODE// Flow control flags #define FC_DTRDSR 0x01 #define FC_RTSCTS 0x02 #define FC_XONXOFF 0x04 // ascii definitions #include #include //#define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers #include #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 #include #include #include #include #include #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; }

ArmanAlimian
Newbie Poster
1 post since Dec 2007
Reputation Points: 10
Solved Threads: 0
 

Hi all

Was anyone able to shed light on my earlier post of how to send 'binary' data to the comm port in Visual Studio (C++)?

Thanks

Oyseka
Newbie Poster
3 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

Hi there,
thanks alot for putting up the code, just what I wanted!
however I do have a small problem. what do I type to replace the words 'CODE' in serial.h and serial.cpp? if i just comment them out, the compiler gives error
.\serial_comm2.cpp(103) : error C2664: 'CreateFileW' : cannot convert parameter 1 from 'char *' to 'LPCWSTR', which i think is linked to the fact i commented them out.
really sorry I'm a newbie!

you can reply to my hotmail
Thanks!

Ivan85
Newbie Poster
4 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
 

Hi there, thanks alot for putting up the code, just what I wanted! however I do have a small problem. what do I type to replace the words 'CODE' in serial.h and serial.cpp? if i just comment them out, the compiler gives error .\serial_comm2.cpp(103) : error C2664: 'CreateFileW' : cannot convert parameter 1 from 'char *' to 'LPCWSTR', which i think is linked to the fact i commented them out. really sorry I'm a newbie!

you can reply to my hotmail Thanks!

You must be compiling for UNICODE. You can turn that off if you need to. If you don't want to turn UNICODE off then you will have to change the code he posted to use tchar_t (or TCHAR) iinstead of char.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Hi all

Was anyone able to shed light on my earlier post of how to send 'binary' data to the comm port in Visual Studio (C++)?

Thanks

Already told you in my original post to this thread. All data send through the com port is sent as binary data, even if it is just a text string. The communications functions don't care what you send -- the programs on the sender and receiver side have to know how to interpret the data, not the comm functions.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 
You must be compiling for UNICODE. You can turn that off if you need to. If you don't want to turn UNICODE off then you will have to change the code he posted to use tchar_t (or TCHAR) iinstead of char.

Hi thanks for you reply!
and Yes setting the project properties to not use the UNICODE option, so that worked. However, it then complains about the words 'CODE' being re-defined as 'int CODE', whereas before it was 'CODE CODE', was wondering if you can help me, this is the error msg:

1>c:\documents and settings\administrator\my documents\visual studio 2005\projects\serial_comm2\serial_comm2\serial_comm2.h(46) : error C2146: syntax error : missing ';' before identifier 'CODE'
1>c:\documents and settings\administrator\my documents\visual studio 2005\projects\serial_comm2\serial_comm2\serial_comm2.h(46) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\documents and settings\administrator\my documents\visual studio 2005\projects\serial_comm2\serial_comm2\serial_comm2.h(46) : error C2146: syntax error : missing ';' before identifier 'HANDLE'
1>c:\documents and settings\administrator\my documents\visual studio 2005\projects\serial_comm2\serial_comm2\serial_comm2.h(46) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\documents and settings\administrator\my documents\visual studio 2005\projects\serial_comm2\serial_comm2\serial_comm2.h(46) : error C2086: 'int CODE' : redefinition
1> c:\documents and settings\administrator\my documents\visual studio 2005\projects\serial_comm2\serial_comm2\serial_comm2.h(4) : see declaration of 'CODE'
1>AssemblyInfo.cpp

Thanks!
Ivan

Ivan85
Newbie Poster
4 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You