(Its like serial port monitoring)
As am working on Rs232 port ,which i have to do monitoring in text area ,
i have editbox (variable name is "rs_hex_data_send"), where i have to send the data in Hexa Decimal , after Read n write will have in hexadecimal but while printing in textraea(variable name is "m_textareacom1") its should be in ASCII value
problems 1: How do i take editbox( Variable name "rs_hex_data_send") data where i ahve write and Read
problems 2: How do i print the data in ASCII value i Text area( variable name "m_textareacom1")
Code which i wriiten is hardcode while declaring the hexdeciamla value , but i want from editbox to send hexa decimal data
What I have tried:
BOOL Serialmonitoring::OpenPort(CString portname)
{
portname = portname;
hComm = CreateFile(portname,
GENERIC_READ | GENERIC_WRITE,
0, \
0,
OPEN_EXISTING,
0,
0);
if (hComm == INVALID_HANDLE_VALUE) {
AfxMessageBox(L"Cannot open Communication Port.Please\nQuit the program and Re-start your PC.", MB_OK + MB_ICONERROR);
printf("\n Port %s Opened\n ", portname);
return false;
}
else
return true;
}
BOOL Serialmonitoring::ConfigurePort(DWORD BaudRate, BYTE ByteSize, DWORD fParity, BYTE Parity, BYTE StopBits)
{
if ((m_bPortReady = GetCommState(hComm, &m_dcb)) == 0) {
AfxMessageBox(L"GetCommState Error", MB_OK + MB_ICONERROR);
CloseHandle(hComm);
return false;
}
m_dcb.BaudRate = BaudRate;
m_dcb.ByteSize = ByteSize;
m_dcb.Parity = Parity;
m_dcb.StopBits = StopBits;
m_dcb.fBinary = TRUE;
m_dcb.fDsrSensitivity = false;
m_dcb.fParity = fParity;
m_dcb.fOutX = false;
m_dcb.fInX = false;
m_dcb.fNull = false;
m_dcb.fAbortOnError = TRUE;
m_dcb.fOutxCtsFlow = FALSE;
m_dcb.fOutxDsrFlow = false;
m_dcb.fDtrControl = DTR_CONTROL_DISABLE;
m_dcb.fDsrSensitivity = false;
m_dcb.fRtsControl = RTS_CONTROL_DISABLE;
m_dcb.fOutxCtsFlow = false;
m_dcb.fOutxCtsFlow = false;
m_bPortReady = SetCommState(hComm, &m_dcb);
if (m_bPortReady == 0) {
AfxMessageBox(L"SetCommState Error", MB_OK + MB_ICONERROR);
CloseHandle(hComm);
return false;
}
return true;
}
BOOL Serialmonitoring::SetCommunicationTimeouts(DWORD ReadIntervalTimeout, DWORD ReadTotalTimeoutMultiplier, DWORD ReadTotalTimeoutConstant, DWORD WriteTotalTimeoutMultiplier, DWORD WriteTotalTimeoutConstant)
{
if ((m_bPortReady = GetCommTimeouts(hComm, &m_CommTimeouts)) == 0)
return false;
m_CommTimeouts.ReadIntervalTimeout = ReadIntervalTimeout;
m_CommTimeouts.ReadTotalTimeoutConstant = ReadTotalTimeoutConstant;
m_CommTimeouts.ReadTotalTimeoutMultiplier = ReadTotalTimeoutMultiplier;
m_CommTimeouts.WriteTotalTimeoutConstant = WriteTotalTimeoutConstant;
m_CommTimeouts.WriteTotalTimeoutMultiplier = WriteTotalTimeoutMultiplier;
m_bPortReady = SetCommTimeouts(hComm, &m_CommTimeouts);
if (m_bPortReady == 0) {
AfxMessageBox(L"StCommTimeouts function failed", MB_OK + MB_ICONERROR);
CloseHandle(hComm);
return false;
}
return true;
}
BOOL Serialmonitoring::WriteData(char bybyte)
{Where
DWORD dNoOfBytesWritten;
DWORD dNoOfBytesWritten = strlen(str)+1;
if (WriteFile(hComm, &bybyte, 1, &dNoOfBytesWritten, NULL) == 0)
return false;
else return true;
}
BOOL Serialmonitoring::ReadData(BYTE& resp)
{
BYTE rx;
resp = 0;
DWORD dwBytesTransferred = 0;
if (ReadFile(hComm, &rx, 1, &dwBytesTransferred, 0)) {
if (dwBytesTransferred == 1) {
resp = rx;
return true;
}
}
return false;
}
void Serialmonitoring::ClosePort()
{
CloseHandle(hComm);
return;
}
// click button
void CCom1Dialog::OnBnClickedSend()
{
UpdateData(true);
CString rs;
m_editcomport1.GetWindowText(rs);
if (!(port.OpenPort(L"COM6"))) {
MessageBox(L"Cannot open Communication Port.Please\nquit the application & re-start your PC.", L"Error", MB_OK + MB_ICONERROR);
}
else {
if (!(port.ConfigurePort(1920, 8, 0, NOPARITY, ONESTOPBIT))) {
MessageBox(L"Cannot Configure Communication Port", L"Error", MB_OK + MB_ICONERROR);
port.ClosePort();
}
else {
if (!(port.SetCommunicationTimeouts(0, 100, 0, 0, 0))) {
MessageBox(L"Cannot Configure Communication Timeouts", L"Error", MB_OK + MB_ICONERROR);
port.ClosePort();
}
else {
if (!(port.WriteData(rs))) {
MessageBox(L"Cannot Write to Port", L"Error", MB_OK + MB_ICONERROR);
port.ClosePort();
}
else {
if (!(port.ReadData(data))) {
MessageBox(L"Timeout on getting Response.Please ensure that\nyou have connected the loop back plug to com1.", L"Timeout", MB_OK + MB_ICONEXCLAMATION);
port.ClosePort();
}
else {
m_ed2 = data;
UpdateData(false);
TRACE("Read port from data %d", m_ed2);
m_textareacom1.SetWindowTextW(rs);
port.ClosePort();
}
}
}
}
}
Declarations :
<pre>public:
void ClosePort();
BOOL ReadData(BYTE& resp);
BOOL WriteData(char bybyte);
BOOL OpenPort(CString portname);
BOOL SetCommunicationTimeouts(DWORD ReadIntervalTimeout, DWORD ReadTotalTimeoutMultiplier, DWORD ReadTotalTimeoutConstant, DWORD WriteTotalTimeoutMultiplier, DWORD WriteTotalTimeoutConstant);
BOOL ConfigurePort(DWORD BaudRate, BYTE ByteSize, DWORD fParity, BYTE Parity, BYTE StopBits);
HANDLE hComm;
DCB m_dcb;
COMMTIMEOUTS m_CommTimeouts;
BOOL m_bPortReady;
BOOL bWriteRC;
BOOL bReadRC;
DWORD iBytesWritten;
DWORD iBytesRead;
DWORD dwBytesRead;
char str[10];
Please go through the code n help me out
Problem 1. string value geting from editbox , Writedata() functions how to i make it as hexadecimal (char str[100))(should i convert string to hexadecimal or not)
problm2 2. ReadData() functions read in hexadecimal value , printing ass ASCII value
please help me with my code n give sonme idea