read charcter from serial port

Discussion in 'Windows' started by answerme, Aug 22, 2008.

  1. answerme

    answerme New Member

    Joined:
    Dec 17, 2007
    Messages:
    114
    Likes Received:
    0
    Trophy Points:
    0
    Hi all
    I want to read & write character from the serial port. Does anyone knows how t o do it.I ma having XP with .net framework
     
  2. neo_vi

    neo_vi Member

    Joined:
    Feb 1, 2008
    Messages:
    720
    Likes Received:
    16
    Trophy Points:
    18
    Occupation:
    Software engineer
    Location:
    Earth
    Home Page:
    http://computertipaday.blogspot.com
  3. answerme

    answerme New Member

    Joined:
    Dec 17, 2007
    Messages:
    114
    Likes Received:
    0
    Trophy Points:
    0
    I need source code in C using .net framework
     
  4. neo_vi

    neo_vi Member

    Joined:
    Feb 1, 2008
    Messages:
    720
    Likes Received:
    16
    Trophy Points:
    18
    Occupation:
    Software engineer
    Location:
    Earth
    Home Page:
    http://computertipaday.blogspot.com
    try to google it. I'll also
     
  5. answerme

    answerme New Member

    Joined:
    Dec 17, 2007
    Messages:
    114
    Likes Received:
    0
    Trophy Points:
    0
    HAVE FUN

    Code:
    #include <windows.h>
    #include <stdio.h>
    #include <string.h>
    
    HANDLE hPort;
    
    BOOL WriteByte(BYTE bybyte)
    {
    	DWORD iBytesWritten=0;
    	DWORD iBytesToRead = 1;
    if(WriteFile(hPort,(LPCVOID)
    	&bybyte,iBytesToRead,&iBytesWritten,NULL)==0)
    	return FALSE;
    else
    	return TRUE;
    }
    
    BOOL WriteString(const void *instring, int length)
    {
    	int index;
    	BYTE *inbyte = (BYTE *) instring;
    	for(index = 0; index< length; ++index)
    	{
    	if (WriteByte(inbyte[index]) == FALSE)
    		return FALSE;
    	}
    	return TRUE;
    }
    
    BOOL ReadByte(BYTE resp)
    	{
    	BOOL bReturn = TRUE;
    	BYTE rx;
    	DWORD dwBytesTransferred=0;
    
    	if (ReadFile (hPort, &rx, 1, &dwBytesTransferred, 0)> 0)
    	{
    		if (dwBytesTransferred == 1)
    		{
    			resp=rx;
    			bReturn = TRUE;
    		}
    	else 
    		bReturn = FALSE;
    	}
    	else 
    		bReturn = FALSE;
    	return bReturn;
    }
    
    BOOL ReadString(void *outstring, int *length)
    {
    		BYTE data;
    		BYTE dataout[4096]={0};
    		int index = 0;
    	while(ReadByte(data)== TRUE)
    	{
    		dataout[index++] = data;
    	}
    	memcpy(outstring, dataout, index);
    	*length = index;
    	return TRUE;
    }
    
    void ClosePort()
    	{
    		CloseHandle(hPort);
    		return;
    	}
    
    HANDLE ConfigureSerialPort(LPCSTR lpszPortName)
    {
    	HANDLE hComm = NULL;
    	DWORD dwError;
    	DCB PortDCB;
    	COMMTIMEOUTS CommTimeouts;
    	// Open the serial port.
    	hComm = CreateFile (lpszPortName,GENERIC_READ | GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
    	PortDCB.DCBlength = sizeof (DCB); // Initialize the DCBlength member.
    	GetCommState (hComm, &PortDCB);	// Get the default port setting information.
    	
    // Change the DCB structure settings.
    PortDCB.BaudRate = 9600; // Current baud
    PortDCB.fBinary = TRUE; // Binary mode; no EOF check
    PortDCB.fParity = TRUE; // Enable parity checking
    PortDCB.fOutxCtsFlow = FALSE; // No CTS output flow control
    PortDCB.fOutxDsrFlow = FALSE; // No DSR output flow control
    PortDCB.fDtrControl = DTR_CONTROL_ENABLE;
    // DTR flow control type
    PortDCB.fDsrSensitivity = FALSE; // DSR sensitivity
    PortDCB.fTXContinueOnXoff = TRUE; // XOFF continues Tx
    PortDCB.fOutX = FALSE; // No XON/XOFF out flow control
    PortDCB.fInX = FALSE; // No XON/XOFF in flow control
    PortDCB.fErrorChar = FALSE; // Disable error replacement
    PortDCB.fNull = FALSE; // Disable null stripping
    PortDCB.fRtsControl = RTS_CONTROL_ENABLE;
    // RTS flow control
    PortDCB.fAbortOnError = FALSE; // Do not abort reads/writes on
    // error
    PortDCB.ByteSize = 8; // Number of bits/byte, 4-8
    PortDCB.Parity = NOPARITY; // 0-4=no,odd,even,mark,space
    PortDCB.StopBits = ONESTOPBIT; // 0,1,2 = 1, 1.5, 2
    
    // Configure the port according to the specifications of the DCB
    // structure.
    if (!SetCommState (hComm, &PortDCB))
    {
    printf("Could not configure serial port\n");
    return NULL;
    }
    // Retrieve the time-out parameters for all read and write operations
    // on the port.
    GetCommTimeouts (hComm, &CommTimeouts);
    // Change the COMMTIMEOUTS structure settings.
    CommTimeouts.ReadIntervalTimeout = MAXDWORD;
    CommTimeouts.ReadTotalTimeoutMultiplier = 0;
    CommTimeouts.ReadTotalTimeoutConstant = 0;
    CommTimeouts.WriteTotalTimeoutMultiplier = 0;
    CommTimeouts.WriteTotalTimeoutConstant = 0;
    if (!SetCommTimeouts (hComm, &CommTimeouts))
    {
    printf("Could not set timeouts\n");
    return NULL;
    }
    return hComm;
    }
    
    int main(void)
    {
    // Can also use COM2, COM3 or COM4 here
    hPort = ConfigureSerialPort("COM1");
    if(hPort == NULL)
    {
    printf("Com port configuration failed\n");
    return -1;
    }
    // Call your ReadString and WriteString functions here
    ClosePort();
    return 0;
    }
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice