Page 1 of 1
c++ write to com1 eg
Posted: Tue Sep 30, 2003 8:19 pm
by mattyork
hi, i was looking for a simple example of how to write a byte to a LCD on COM1. Im using VC++ 6.0. An example of how to read keypad inputs would also be great. Thanks, matt
Posted: Wed Oct 01, 2003 1:57 pm
by funkymule
Using Windows apps, the easiest way to write to the LCD on COM1 (that I have done), is just open COM1 as a file, and write your bytes to the file. The only trick is that you have to open it with GENERIC_READ|GENERIC_WRITE, and OPEN_EXISTING flags.
Here's a "Hello World" that I did when I got my first display:
Code: Select all
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
DCB dcb;
HANDLE hCom;
hCom = CreateFile("COM1",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
GetCommState(hCom, &dcb);
dcb.BaudRate = CBR_19200; // set the baud rate
dcb.ByteSize = 8; // data size, xmit, and rcv
dcb.Parity = NOPARITY; // no parity bit
dcb.StopBits = ONESTOPBIT; // one stop bit
SetCommState(hCom, &dcb);
DWORD dw;
char buffer[256];
// set up screen options:
buffer[0] = 0xfe;
// auto line wrap off
buffer[1] = 68;
WriteFile(hCom,buffer,2,&dw,NULL);
// auto scroll off
buffer[1] = 82;
WriteFile(hCom,buffer,2,&dw,NULL);
// turn off underline cursor
buffer[1] = 75;
WriteFile(hCom,buffer,2,&dw,NULL);
// turn off blinking cursor
buffer[1] = 84;
WriteFile(hCom,buffer,2,&dw,NULL);
// clear the screen
buffer[1] = 88;
WriteFile(hCom,buffer,2,&dw,NULL);
lstrcpy((LPTSTR)buffer,"Hello, World!");
WriteFile(hCom,buffer,lstrlen(buffer),&dw,NULL);
CloseHandle(hCom);
return 0;
}
As far as reading the keypad...I haven't done that.
Hope it helps.
--Cuso.
Posted: Thu Oct 02, 2003 10:56 am
by Johnny1
Hi Matt,
Here's a link to an article from
The Code Project on how to get started with serial communications in Windows using VC++:
http://www.codeproject.com/system/serial_com.asp
This article shows how to send and receive data to serial ports. I have tried their sample code and it works with the following minor changes to the SerialCommDlg.cpp file:
1. Inside function * void CSerialCommDlg::OnOpen() * change line 181
From: m_theCommPort.Init ("COM1",
9600,0,1,8 );
To: m_theCommPort.Init ("COM1",
19200,0,1,8 ); // Basically just change baud rate and com port if necessary.
2. Inside function * void CSerialCommDlg::OnWrite() * change line 190
From: m_theCommPort.Write(
m_szSend, m_szSend.GetLength () );
To: m_theCommPort.Write(
m_szSend.GetBuffer(0), m_szSend.GetLength () ); // Need to convert String into character array
That's it! Compile and run. You should be able to send and receive data from the display.
A more indepth article on Serial Communications in Win32:
http://msdn.microsoft.com/library/defau ... serial.asp
I should point out that if you are new to programming ( specially to Serial Ports ) VC++ is not really the way to go. There are much easier languages to get started with and that will do the same work.
Good luck!

Funkymules code.
Posted: Tue Feb 28, 2006 6:11 pm
by EtienneNavaar
Can anyone help me compile funkymule's code from above. I get the following error when I try to compile it in VC++ 6.0.
Error C2146: syntax error : missing ';' before identifier 'WinMain'
Posted: Thu Mar 02, 2006 12:15 am
by Ray
Add a include to Windows.h and you should be fine with that code.
Thanks Ray!
Posted: Thu Mar 02, 2006 6:13 pm
by EtienneNavaar
You're the bomb Ray, it worked perfectly with the added 'include' you suggested. Thanks a lot for taking the time to post.
Another Compilation Error
Posted: Mon Oct 23, 2006 9:40 pm
by legit
Can someone help me compile this. I get the following errors:
'CreateFileW' : cannot convert parameter 1 from 'const char [5]' to 'LPCWSTR'
'lstrcpyW' : cannot convert parameter 2 from 'const char [14]' to 'LPCWSTR'
'lstrlenW' : cannot convert parameter 1 from 'char [256]' to 'LPCWSTR'
Help is greatly appreciated,
thanks,
- legit
Also how do i run it in Visual Studio when i finish building it?
Posted: Tue Oct 24, 2006 9:42 am
by Ray
replace
Code: Select all
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
with
and set the character set in project options to anything but unicode and you should be fine.