Click here to Skip to main content
14,692,225 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
(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);
        // MessageBox(L"StCommTimeouts function failed", L"Com Port Error", MB_OK + MB_ICONERROR);
        CloseHandle(hComm);
        return false;
    }

    return true;
}

BOOL Serialmonitoring::WriteData(char bybyte)
{Where 
    // Herecwhere i have to write the data which is come from editbox (IN hexadecimal ) 
    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)
{
   //Here Read the data as hexa decimal but i didnt get 
    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);
	//BYTE data = 0;
	CString rs;
	m_editcomport1.GetWindowText(rs);// the string value which is taking from editbox
	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);
						//MessageBox(L"Read from Port completed successfully", L"Success", MB_OK + MB_ICONINFORMATION);
						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
Posted
Updated 12-Oct-20 0:17am
v3
Comments
Richard MacCutchan 12-Oct-20 5:52am
   
"should i convert string to hexadecimal or not"
How should we know? As I told you yesterday it depends on what you are trying to do and what device(s) you are communicating with.
Member 14837073 12-Oct-20 6:12am
   
Device which i am working on rs232 serial port

Smal project which given to me is , rs232 serial port monitoring

Data which i am sending through editbox , its should write in hexadecimal n read in a hexadecimal and print as ASCII value ...

byte which u mentioned, i tried write with 0 bytes n read with 0 bytes its working fine

but how do i write Arry bytes in hexadecimal in WriteData() function which i am geting from edibox value
Richard MacCutchan 12-Oct-20 6:54am
   
You cannot write to or read from a RS232 port unless it is connected to some device that will process the messages. And serial devices do not in general handle hexadecimal, they read and write in simple 8 bit bytes. So you need to understand what information the remote device needs to receive and what information it sends before you can do anything useful.

RS232 deals only with bytes, so any data you send must be sent as a stream of bytes, and any you receive will be the same. What you do in terms of presenting this to the user depends on what form the data is. For example a string of ASCII characters can be presented directly, as each character is a single byte. Binary data would need to be converted to whatever values it represents, which could be single byte values, 16 bit numbers etc. So the real issue is what data you are trying transfer across this port.
   
problem 1: you must get the text from the edit box with GetWindowText.

problem 2: each string has characters in ASCII format. So you need to access the strings characters like already answered here.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900