RE: rs232?
Hi Sean and all others!
Thankyou for your posts, I have gone to the web site:
http://www.codeproject.com/system/serial.asp
suggested by Jochen, and I would like to adopt the CSerialWnd method, since
I feel comfortable with it. However, I don't understand a few things. like:
Do I declare it like this in WIN main of VC++?
#include <windows.h>
#include "Sysmets.h"
#define STRICT
#include <tchar.h>
#include "Serial.h"
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInnstance,
PSTR szCmdLine, int iCmdShow)
{
CSerial serial;
Setup(CSerial::EBaudrate(9600),
CSerial::EDataBits(8),
CSerial::EParity(NOPARITY),
CSerial::EStopBits(ONESTOPBIT));
// Attempt to open the serial port (COM1)
serial.Open(_T("COM1"));
// Setup the serial port (9600,N81) using hardware handshaking
serial.Setup
(CSerial::EBaud9600,CSerial::EData8,CSerial::EParNone,CSerial::EStop1);
serial.SetupHandshaking(CSerial::EHandshakeHardware);
// The serial port is now ready and we can send/receive data.
serial.Write("Hello world");
// Close the port again
serial.Close();
static TCHAR szAppName[] = TEXT ("SysMets3");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW |CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("xxx"),szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow (szAppName, TEXT ("Get System Metrics No3!"),
WS_OVERLAPPEDWINDOW |WS_VSCROLL | WS_HSCROLL,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL);
ShowWindow(hwnd, iCmdShow);
UpdateWindow (hwnd);
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
=================================================
But then further in the document it says to open the port with this:
=================================================
LONG Open (
LPCTSTR lpszDevice,
HWND hwndDest,
UINT nComMsg = WM_NULL,
LPARAM lParam = 0,
DWORD dwInQueue = 0,
DWORD dwOutQueue = 0
)
===================================================
and then it shows what looks like something I would be used to seeing:
===================================================
LRESULT CALLBACK MyWndProc (HWND hwnd, UINT nMsg, WPARAM wParam, LPARAM
lParam)
{
if (nMsg == CSerialWnd::mg_nDefaultComMsg)
{
// A serial message occurred
const CSerialWnd::EEvent eEvent = CSerialWnd::EEvent(LOWORD(wParam));
const CSerialWnd::EError eError = CSerialWnd::EError(HIWORD(wParam));
switch (eEvent)
{
case CSerialWnd::EEventRecv:
// TODO: Read data from the port
break;
...
}
// Return successful
return 0;
}
// Perform other window processing
...
}
==================================================
How do you put it all together! :(
I have read the whole document and it shows that you should not try to get
events for your serial port through the Windows message loop, it also shows
various ways of
getting events from the serial port such as:
"CSerial is the base serial class, which provides a wrapper around the Win32
API. It is a lot easier to use, because it combines all relevant calls in one
single class. It allows the programmer to mix overlapped and non-overlapped
calls, provides reasonable default settings, better readability, etc, etc.
CSerialEx adds an additional thread to the serial class, which can be used
to handle the serial events. This releases the main GUI thread from the
serial burden. The main disadvantage of this class is that it introduces
threading to your architecture, which might be hard for some people.
CSerialWnd fits in the Windows event driven model. Whenever a communication
event occurs a message is posted to the owner window, which can process the
event.
CSerialMFC is an MFC wrapper around CSerialWnd, which make the serial
classes fit better in MFC based programs. "
I would be interested in the CSerialWnd approach... But do I innitialize it
and set it up as shown above..... This document confused me more than
anything else.
Does anyone know of a good book which shows only the core code to simply
receive *one* byte and transmit *one* byte. It seems to me that you must
declare a serial object or in my case a CSerialWnd object and then set it up,
and then open the port and wait for some messages so you can trap them and
read or write to the port.
I have not yet gone to the Win32, Communications Resources section of the
SDK.
I wil do this as soon as I finish this post!
I have not used yet file I/Os, so I don't know if this will be easy to do
with
CreateFile, ReadFile and WriteFile, and CloseHandle to open, read/write and
close the port respectively.
I tried to enter words like SerialPort class in the help, but found nothing.
All samples, suggestions, help documents, or any pointers to this is (at
this point) so appreciated!
help!
--
Best regards
Robert
"Sean M. DonCarlos" wrote:
"Robby" wrote:
I find myself faced with the need to do some serial communications from VC++
to the rs232 port of my computer.
Can someone directme to the pertinant commands in C or in C++ in order to do
this.
In Win32, see the Communications Resources section of the SDK. Typically,
serial port I/O is handled in much the same way as file I/O: You use
CreateFile, ReadFile and WriteFile, and CloseHandle to open, read/write and
close the port respectively. There are some other APIs and structures that
handle configuration of the serial ports.
In C++/CLI, use the System::IO::Ports::SerialPort class.
Sean