오픈소스가많다는데 소켓프로그래밍밖에안보여서요..ㅠㅠ
rs231을 통한 일대일 채팅프로그램을 구현해야되는데..제대로 안되네요
글을 쓰면 연속적으로 써지는게 아니라
워키토키 방식으로 한줄씩 돌아가면서 순차적으로 글을써야만해요ㅠㅠ..이거어떻게고치죠?
#include <iostream.h>
#include <windows.h>
#include <string.h>
// using namespace std; HANDLE hComm;
DCB m_dcb; int Open_Port(char* szPort)
{
//The CreateFile function opens a munications port
hComm = CreateFile( szPort, //pointer to name of the file
//If a port does not exist, an error will occur (ERROR_FILE_NOT_FOUND)
GENERIC_READ | GENERIC_WRITE, // access (read-write) mode.
0, // exclusive access. Communications ports cannot be shared in the same manner that files are shared
0, // default security attributes
OPEN_EXISTING, //Opens the file. The function fails if the file does not exist.
0,//FILE_FLAG_OVERLAPPED,
0);
//If the function succeeds, the return value is an open handle to the specified file.
//If the file does not exist before the call, GetLastError returns zero.
// check serial port
if (hComm == INVALID_HANDLE_VALUE)
{
// Handle the error
cout << "CreateFile failed with error" << endl <<endl;
return 1;
}
else
{
cout << "CreatFile is Success" << endl<<endl;
} return 0;
} int Init_serial()
{ //get serial communications device
//baud rate, stop bits, byte size, Parity check //There are three ways to initialize a DCB structure.
//The first method is to use the function GetCommState.
//This function returns the current DCB in use for the communications port.
if(GetCommState(hComm,&m_dcb)) // handle to communications device
// pointer to device-control block structure
{
cout << "Check Serial Satus" << endl;
cout << "baud rate : " << m_dcb.BaudRate << endl;
cout << "stop bit : " << m_dcb.StopBits << endl;
cout << "data size : " << m_dcb.ByteSize << endl;
cout << "Ready to Serial" << endl<< endl;
}
else
{
cout << "Fail to Set serial" <<endl<<endl;
}
/*
typedef struct _DCB { // dcb
DWORD BaudRate; // current baud rate
DWORD fParity: 1; // enable parity checking
BYTE ByteSize; // number of bits/byte, 4-8
BYTE Parity; // 0-4=no,odd,even,mark,space
BYTE StopBits; // 0,1,2 = 1, 1.5, 2
} DCB;
*/
// set serial device
m_dcb.BaudRate = 115200;
m_dcb.ByteSize = 8;
m_dcb.StopBits = ONESTOPBIT;
m_dcb.Parity = NOPARITY; //The SetCommState function configures a communications device according to the specifications
//in a device-control block (a DCB structure).
//The function reinitializes all hardware and control settings,
//but it does not empty output or input queues
if(SetCommState(hComm,&m_dcb)) // handle to communications device
// pointer to device-control block structure
{
cout << "Serial device is set" << endl;
cout << "baud rate : " << m_dcb.BaudRate << endl;
cout << "stop bit : " << (int)m_dcb.StopBits << endl;
cout << "Parity : " << (int)m_dcb.Parity << endl;
cout << "data size : " << (int)m_dcb.ByteSize << endl<<endl;
}
else
{
cout << "Fail to Serial set" << endl;
cout << "=============================" << endl;
return 1;
} return 0;
} int Comp_str(char *a)
{
int pt=0; if(strcmp(a,"/quit"))
pt= 1; // cout << "Result of Compare : " << pt << endl;
return pt;
} int main()
{
char *szPort;
const int buff_size=2048;
char buf[buff_size]="";
char sbuf[buff_size]="";
unsigned long byte_read=0;
unsigned long byte_written=0; int protocol =0;
int val;
szPort="COM2";
// Create File for Com Port
if(Open_Port(szPort))
{
cout << " Fail to Create File for ComPort" << endl;
return 1;
} // Initialize Serial Port
if(Init_serial())
{
cout << " Fail to Initialize Serial Port" << endl;
return 1;
} while(1)
{ cin.getline(buf, 20);
val=WriteFile(hComm, // handle to file to write to
buf, // pointer to data to write to file
strlen(buf), // number of bytes to write
&byte_written, // pointer to number of bytes written
NULL); // pointer to structure for overlapped I/O
if(val==0);
cout << "send data : " <<buf << endl;
val=ReadFile(hComm, // handle of file to read
sbuf, // pointer to buffer that receives data
sizeof(sbuf), // number of bytes to read
&byte_read, // pointer to number of bytes read
NULL); // pointer to structure for data if(val==0);
{
cout <<" recv_data : "<<sbuf << endl;
memset(sbuf, 0, buff_size);
}
}
cout <<"End of Chatting" << endl;
//The ReadFile function reads data from a file, starting at the position indicated by the file pointer. // device close
CloseHandle(hComm); // handle to object to close return 0;
}
#include <windows.h>
#include <string.h>
// using namespace std; HANDLE hComm;
DCB m_dcb; int Open_Port(char* szPort)
{
//The CreateFile function opens a munications port
hComm = CreateFile( szPort, //pointer to name of the file
//If a port does not exist, an error will occur (ERROR_FILE_NOT_FOUND)
GENERIC_READ | GENERIC_WRITE, // access (read-write) mode.
0, // exclusive access. Communications ports cannot be shared in the same manner that files are shared
0, // default security attributes
OPEN_EXISTING, //Opens the file. The function fails if the file does not exist.
0,//FILE_FLAG_OVERLAPPED,
0);
//If the function succeeds, the return value is an open handle to the specified file.
//If the file does not exist before the call, GetLastError returns zero.
// check serial port
if (hComm == INVALID_HANDLE_VALUE)
{
// Handle the error
cout << "CreateFile failed with error" << endl <<endl;
return 1;
}
else
{
cout << "CreatFile is Success" << endl<<endl;
} return 0;
} int Init_serial()
{ //get serial communications device
//baud rate, stop bits, byte size, Parity check //There are three ways to initialize a DCB structure.
//The first method is to use the function GetCommState.
//This function returns the current DCB in use for the communications port.
if(GetCommState(hComm,&m_dcb)) // handle to communications device
// pointer to device-control block structure
{
cout << "Check Serial Satus" << endl;
cout << "baud rate : " << m_dcb.BaudRate << endl;
cout << "stop bit : " << m_dcb.StopBits << endl;
cout << "data size : " << m_dcb.ByteSize << endl;
cout << "Ready to Serial" << endl<< endl;
}
else
{
cout << "Fail to Set serial" <<endl<<endl;
}
/*
typedef struct _DCB { // dcb
DWORD BaudRate; // current baud rate
DWORD fParity: 1; // enable parity checking
BYTE ByteSize; // number of bits/byte, 4-8
BYTE Parity; // 0-4=no,odd,even,mark,space
BYTE StopBits; // 0,1,2 = 1, 1.5, 2
} DCB;
*/
// set serial device
m_dcb.BaudRate = 115200;
m_dcb.ByteSize = 8;
m_dcb.StopBits = ONESTOPBIT;
m_dcb.Parity = NOPARITY; //The SetCommState function configures a communications device according to the specifications
//in a device-control block (a DCB structure).
//The function reinitializes all hardware and control settings,
//but it does not empty output or input queues
if(SetCommState(hComm,&m_dcb)) // handle to communications device
// pointer to device-control block structure
{
cout << "Serial device is set" << endl;
cout << "baud rate : " << m_dcb.BaudRate << endl;
cout << "stop bit : " << (int)m_dcb.StopBits << endl;
cout << "Parity : " << (int)m_dcb.Parity << endl;
cout << "data size : " << (int)m_dcb.ByteSize << endl<<endl;
}
else
{
cout << "Fail to Serial set" << endl;
cout << "=============================" << endl;
return 1;
} return 0;
} int Comp_str(char *a)
{
int pt=0; if(strcmp(a,"/quit"))
pt= 1; // cout << "Result of Compare : " << pt << endl;
return pt;
} int main()
{
char *szPort;
const int buff_size=2048;
char buf[buff_size]="";
char sbuf[buff_size]="";
unsigned long byte_read=0;
unsigned long byte_written=0; int protocol =0;
int val;
szPort="COM2";
// Create File for Com Port
if(Open_Port(szPort))
{
cout << " Fail to Create File for ComPort" << endl;
return 1;
} // Initialize Serial Port
if(Init_serial())
{
cout << " Fail to Initialize Serial Port" << endl;
return 1;
} while(1)
{ cin.getline(buf, 20);
val=WriteFile(hComm, // handle to file to write to
buf, // pointer to data to write to file
strlen(buf), // number of bytes to write
&byte_written, // pointer to number of bytes written
NULL); // pointer to structure for overlapped I/O
if(val==0);
cout << "send data : " <<buf << endl;
val=ReadFile(hComm, // handle of file to read
sbuf, // pointer to buffer that receives data
sizeof(sbuf), // number of bytes to read
&byte_read, // pointer to number of bytes read
NULL); // pointer to structure for data if(val==0);
{
cout <<" recv_data : "<<sbuf << endl;
memset(sbuf, 0, buff_size);
}
}
cout <<"End of Chatting" << endl;
//The ReadFile function reads data from a file, starting at the position indicated by the file pointer. // device close
CloseHandle(hComm); // handle to object to close return 0;
}