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;

}

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.

> 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

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


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

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

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

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.

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

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

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 (<array>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;
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

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

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

}

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

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<EMAIL SNIPPED>
Thanks!

Edited 6 Years Ago by peter_budo: Keep It On The Site - Do not post asking for an answer to be sent to you via email or PM.

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

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.

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

What is the words CODE doing in that program? There is no such thing in C or C++. Are you confusing it with the CODE directive in assembly?

What is the words CODE doing in that program? There is no such thing in C or C++. Are you confusing it with the CODE directive in assembly?

If you look back at Prashw on Sep 27th, 2006, he has 'CODE' written in the code. I have a feeling that I have to write code in those spaces, but i have no idead what to write there, sorry for my inexperience! and my late reply :)

If you look back at Prashw on Sep 27th, 2006, he has 'CODE' written in the code. I have a feeling that I have to write code in those spaces, but i have no idead what to write there, sorry for my inexperience! and my late reply :)

You don't have to do anything there -- just delete the lines that contain CODE. He was attempting to use CODE tags, but didn't do it correctly. Also note that the code contains two files -- a *.h file and a *.cpp file. You can put all that into one *.cpp file if you wish -- just delete the file names and duplicate includes.\

Here, I fixed it up for you (except the formatting -- its terrible.)

#include <iostream>
#include <windows.h>
#include <string>
#include <conio.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
using namespace std;



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

#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);





// 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);

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

return 0;

}

Edited 6 Years Ago by Ancient Dragon: n/a

You don't have to do anything there -- just delete the lines that contain CODE. He was attempting to use CODE tags, but didn't do it correctly. Also note that the code contains two files -- a *.h file and a *.cpp file. You can put all that into one *.cpp file if you wish -- just delete the file names and duplicate includes.\

Here, I fixed it up for you (except the formatting -- its terrible.)

//rest of code
// up to this point

int main()
{
 HANDLE my=SerialInit("com1",1200);
 HANDLE *ptr;
 *ptr=my;
 SerialPutc(ptr,'a');
 getch();

 return 0;
}

Hi there!
thanks for clearing things up, and editing to code to show me (much appreciated!) although i still yet to find out what a control tag is.

after I deleted the words 'CODE' in both the header and cpp files, and it builds fine however when i run without debugging, the command prompt window shows an exception error:


Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
at main() in C:\...\serial.cpp: line 272
press any key to continue . . .

it basically referring to that line of code in main():

*ptr=my;

I am thinking of 2 solutions to this:
1) should the line be *ptr = new my; //something with 'new' operator
2) is it because I've set the project to be a Console Application project (with the black screen command prompt) where it should be a Windows Forms application? (I'm using Visual studios by the way)

Thanks!

This article has been dead for over six months. Start a new discussion instead.