c++ write to com1 eg

LK/ELK/VK/PK/OK/MX/GLK/EGLK/GVK/GLT Series

Moderators: Henry, Mods

Post Reply
mattyork
LCD?
Posts: 2
Joined: Tue Sep 30, 2003 8:13 pm
Location: vancouver

c++ write to com1 eg

Post 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

funkymule
LCD!
Posts: 13
Joined: Sun Sep 28, 2003 9:16 pm

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

Johnny1
Matrix Orbital
Matrix Orbital
Posts: 23
Joined: Tue Dec 10, 2002 1:23 pm
Contact:

Post 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! :hip:
Johnny Patino
Software Developer
Matrix Orbital

EtienneNavaar
LCD?
Posts: 2
Joined: Wed Feb 15, 2006 10:15 am

Funkymules code.

Post 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'

Ray
Matrix Orbital
Matrix Orbital
Posts: 745
Joined: Thu Dec 13, 2001 4:00 pm
Location: Earth.... I think..
Contact:

Post by Ray »

Add a include to Windows.h and you should be fine with that code.

EtienneNavaar
LCD?
Posts: 2
Joined: Wed Feb 15, 2006 10:15 am

Thanks Ray!

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

legit
LCD?
Posts: 4
Joined: Fri Aug 26, 2005 9:46 pm
Location: Denver, CO
Contact:

Another Compilation Error

Post 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?

Ray
Matrix Orbital
Matrix Orbital
Posts: 745
Joined: Thu Dec 13, 2001 4:00 pm
Location: Earth.... I think..
Contact:

Post by Ray »

replace

Code: Select all

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
with

Code: Select all

int main(int argc, char **argv) 
and set the character set in project options to anything but unicode and you should be fine.

Post Reply