I have a program written in C++ which is based on configuring the serial port of the PC to be able to connect to another PC and between them do like a chat, or you both can write.
I read to the other PC, my problem is that do not parallel, meaning that when one writes the other only reads, therefore I think the best way would be to use threads to make do at the same time but the problem comes in that one does not use or how to do it.
I've never used threads in C++ before and would appreciate any help.
sorry for my English!
#include <iostream>
#include <string.h>
#include <windows.h>
BOOL SerialSendByte(HANDLE hPort, BYTE byte);
BOOL SerialReceiveByte(HANDLE hPort, BYTE *pbyte, BOOL *pTimeout);
BOOL CloseSerialPort(HANDLE hPort);
HANDLE OpenSerialPort( char *psPort, DWORD dwBaudRate, BYTE bByteSize, BYTE bParity, BYTE bStopBits, DWORD Timeout ) {
HANDLE hPort; DCB dcbPort; COMMTIMEOUTS commTimeouts; DWORD dwError;
hPort=CreateFile(
psPort, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
if ( hPort == INVALID_HANDLE_VALUE ) {
dwError = GetLastError (); return hPort;
}
FillMemory(&dcbPort, sizeof(dcbPort), 0);
dcbPort.DCBlength = sizeof(dcbPort);
GetCommState (hPort, &dcbPort);
dcbPort.BaudRate = dwBaudRate;
dcbPort.ByteSize = bByteSize;
dcbPort.Parity = bParity;
dcbPort.StopBits = bStopBits;
dcbPort.fBinary = TRUE; dcbPort.fParity = TRUE; dcbPort.fOutxCtsFlow = FALSE; dcbPort.fOutxDsrFlow = FALSE; dcbPort.fDtrControl = DTR_CONTROL_ENABLE;
dcbPort.fDsrSensitivity = FALSE; dcbPort.fTXContinueOnXoff = TRUE; dcbPort.fOutX = FALSE; dcbPort.fInX = FALSE; dcbPort.fErrorChar = FALSE; dcbPort.fNull = FALSE; dcbPort.fRtsControl = RTS_CONTROL_ENABLE;
dcbPort.fAbortOnError = FALSE; if (!SetCommState (hPort, &dcbPort)) {
dwError = GetLastError (); CloseSerialPort(hPort);
hPort = INVALID_HANDLE_VALUE;
return hPort;
}
commTimeouts.ReadIntervalTimeout = 0; commTimeouts.ReadTotalTimeoutMultiplier = 50; commTimeouts.ReadTotalTimeoutConstant = Timeout;commTimeouts.WriteTotalTimeoutMultiplier = 10; commTimeouts.WriteTotalTimeoutConstant = 1000; if (!SetCommTimeouts (hPort, &commTimeouts)) {
dwError = GetLastError (); CloseSerialPort(hPort);
hPort = INVALID_HANDLE_VALUE;
return hPort;
}
return hPort;
}
BOOL SerialSendByte(HANDLE hPort, BYTE byte){
BOOL bRes;
DWORD dwError, dwNumBytesWritten=0;
bRes=WriteFile(
hPort, &byte, 1, &dwNumBytesWritten, NULL );
if ((!bRes)||(dwNumBytesWritten!=1)){
dwError = GetLastError (); }
return bRes;
}
BOOL SerialReceiveByte(HANDLE hPort, BYTE *pbyte, BOOL *pTimeout){
BOOL bRes;
DWORD dwError, lpNumberOfBytesRead=0;
*pTimeout=FALSE;
bRes=ReadFile( hPort, pbyte, 1, &lpNumberOfBytesRead, NULL );
if (!bRes) {
dwError = GetLastError (); }
if ((bRes)&&(lpNumberOfBytesRead==0)){
*pTimeout = TRUE;
}
return bRes;
}
BOOL CloseSerialPort(HANDLE hPort){
BOOL bRes;
DWORD dwError;
bRes=CloseHandle(hPort);
if (!bRes) {
dwError = GetLastError (); }
return bRes;
}
int main(){
HANDLE hPort;
BOOL bRes;
char c;
BYTE byte;
BOOL timeout;
hPort=OpenSerialPort("COM1",CBR_9600,8,NOPARITY,TWOSTOPBITS,5000);
if (hPort==INVALID_HANDLE_VALUE) {
return 1;
}
while (1) {
c=getchar();
bRes=SerialSendByte(hPort,c);
if (!bRes) {
printf("Error escribiendo en puerto com1");
return 1;
}
if (c=='x') {
break;
}
}
CloseSerialPort(hPort);
while(1){
bRes=SerialReceiveByte(hPort,&byte,&timeout);
if (!bRes) {
break;
}
if (timeout){
printf("\n--->timeout\n");
} else {
putchar(byte);
}
}
if (!bRes) {
printf("Error leyendo de puerto com1");
return 1;
}
CloseSerialPort(hPort);
return 0;
}