c++ write to com1 eg
c++ write to com1 eg
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
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:
As far as reading the keypad...I haven't done that.
Hope it helps.
--Cuso.
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;
}
Hope it helps.

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

Johnny Patino
Software Developer
Matrix Orbital
Software Developer
Matrix Orbital
-
- LCD?
- Posts: 2
- Joined: Wed Feb 15, 2006 10:15 am
Funkymules code.
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'
Error C2146: syntax error : missing ';' before identifier 'WinMain'
-
- LCD?
- Posts: 2
- Joined: Wed Feb 15, 2006 10:15 am
Thanks Ray!
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
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?
'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?
-
- Matrix Orbital
- Posts: 745
- Joined: Thu Dec 13, 2001 4:00 pm
- Location: Earth.... I think..
- Contact:
replace
with
and set the character set in project options to anything but unicode and you should be fine.
Code: Select all
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
Code: Select all
int main(int argc, char **argv)