Hi,I use serial line communication(RS232) to send/receive data using 2 PC. The port COM is opened succesfully but the problem is that read(reception) and write(sending) functions do not work(there is no error in debugging).I use PORT COM1 and COM4 -
int CCommunicationSérie::OpenCom(DWORD bauderate,CString portcom)
-
{
-
-
// DWORD m_EventMask;
-
BOOL m_bPortReady;
-
memset(&m_overlapped,0,sizeof(m_overlapped));
-
m_hCom = CreateFile((LPCSTR)portcom,
-
GENERIC_READ | GENERIC_WRITE,
-
0,
-
NULL,
-
OPEN_EXISTING,
-
FILE_FLAG_OVERLAPPED,
-
NULL);
-
if(m_hCom==INVALID_HANDLE_VALUE)
-
return 1;
-
if ((m_bPortReady=GetCommState(m_hCom,&m_dcb))==0)
-
{
-
CloseHandle(m_hCom);
-
return 2;
-
}
-
-
//ClearCommBreak(m_hCom);
-
//FillMemory( &m_dcb,0, sizeof(m_dcb));
-
//SetupComm( m_hCom, m_nInputBufferSize, m_nOutputBufferSize );
-
//::GetCommMask( m_hCom, &m_EventMask );
-
// ::SetCommMask( m_hCom, 0 );
-
m_dcb.fBinary=1;
-
m_dcb.fParity=0;
-
m_dcb.fNull = 0;
-
m_dcb.fDsrSensitivity=1;
-
m_dcb.fOutX=0;
-
m_dcb.fInX=0;
-
-
m_dcb.XonLim =500;
-
m_dcb.XoffLim =1;
-
m_dcb.EofChar = '$';
-
m_dcb.fAbortOnError=1;
-
m_dcb.fOutxDsrFlow=1;
-
m_dcb.fOutxCtsFlow=1;
-
m_dcb.fDtrControl=DTR_CONTROL_ENABLE;
-
m_dcb.fRtsControl=RTS_CONTROL_ENABLE;
-
m_dcb.DCBlength = sizeof(DCB);
-
-
m_dcb.BaudRate =bauderate;//9600
-
m_dcb.ByteSize = 8;
-
m_dcb.Parity =NOPARITY;
-
m_dcb.StopBits =ONESTOPBIT;
-
-
m_bPortReady=SetCommState(m_hCom, &m_dcb);
-
if(m_bPortReady==0) { CloseHandle(m_hCom);return 3;}
-
-
}
-
-
-
//vider les tampons
-
PurgeComm(m_hCom,PURGE_TXABORT|PURGE_RXABORT|PURGE_TXCLEAR|PURGE_RXCLEAR);
-
MessageBoxA("ouverture du port","",MB_OK);
-
return 0;
-
}
-
writing: -
bool CCommunicationSérie::WriteCom()
-
{
-
DWORD NbBytesWritten=0;
-
DWORD size=3;
-
-
-
//LPBYTE write_buffer=new BYTE[1];write_buffer[0]=0x01;
-
const char* write_buffer=new char[3];
-
write_buffer="1$";
-
if(WriteFile(m_hCom,(LPCVOID)write_buffer,size,&NbBytesWritten,&m_overlapped)==0)
-
{
-
delete[](write_buffer);
-
MessageBoxA("erreur","",MB_OK);
-
return true;//error
-
}
-
//delete[](write_buffer);
-
MessageBoxA("succés","",MB_OK);
-
return false;
-
-
}
-
reading -
bool CCommunicationSérie::ReadCom()
-
{
-
char* read_buffer=new char[5];
-
DWORD nNumberOfBytesToRead=3;
-
DWORD NumberOfBytesRead;
-
LPVOID ReadBuffer=NULL;
-
DWORD size=3;
-
-
DWORD dwErrorMask ;
-
COMSTAT cs; // structure
-
-
ClearCommError(m_hCom, &dwErrorMask , &cs); //initialise cs
-
if( cs.cbInQue>=size)
-
{
-
if (ReadFile (m_hCom,ReadBuffer,nNumberOfBytesToRead, &NumberOfBytesRead, &m_overlapped))
-
{
-
if (NumberOfBytesRead == size)
-
{
-
read_buffer=(char*)ReadBuffer;
-
MessageBoxA("sucées","",MB_OK);
-
return false;
-
}
-
}
-
MessageBoxA("erreur","",MB_OK);
-
return true;
-
}
-
MessageBoxA("erreur","",MB_OK);
-
return true;
-
}
-
thanks for help
3 2463 Banfa 9,065
Expert Mod 8TB
Are you sure the hardware is right? That is are you sure that you are using the correct cable, a NULL modem?
You can easily check by just using a terminal program (HyperTerminal, dare I say it_. on each PC which should be able to communicate if the cable is correct.
I'm using 2 USB to serial converters (one for each PC) and they are related with a plug (female female).
Banfa 9,065
Expert Mod 8TB
You mean they are connected with a plug ?
Have you tried the connection using Hyper-terminal (or some other terminal program)?
A standard female-female converter is not a NULL modem. I NULL modem crosses some of the connections rather than providing a straight through connection which is important because you need to connect tx-line of 1 PC to the rx-line of the other and vice versa.
Like I said do not try to guess if your hardware is correct, test it and confirm that it is correct using Hyper Terminal.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: T.S.Negi |
last post by:
Hi All,
I have to write a stored procedure which will send/Receive text file
from/to a server by using FTP.
Is anybody have done anything on it? or know about it.
If yes, I would like to know...
|
by: **** KiteOregon **** |
last post by:
I need to send & receive XML Document Objects via the Message Queue.
We have several applications that insert XML Document Object into the
message queue. This works with no problem. I am trying...
|
by: Kitchen Bin |
last post by:
Hi.
I am trying to use Sockets to do multiple Send and Receives via HTTP
(not simultaneously). A first pair of Send/Receives works fine and
sure enough I receive HTML back, but the next...
|
by: EppO |
last post by:
I wrote a small server/client app. I created a class for my customized
socket object using (synchronous) Sockets functions for both server
and client use.
When client connects to server, if It...
|
by: saurabhaggarwal |
last post by:
Hi
Could anyone please tell me how can we capture send/receive event in
MS Outlook.
Suppose I want to pop up a message box when user hits send/receive
button, how can we do that.
Actually...
|
by: kiran |
last post by:
Hi all,
I have a problem to communicate with serial port(COM3:). I am able to open
the handle but cannot send any data.
1. I connected my motoroala handset to PC through datacable.
2. I...
|
by: ChopDown |
last post by:
Hi, I want to send/receive a serializable object in socket:
==========sender==========
Dim MyMessage As New MessageClass.Message()
Dim client As New TcpClient...
|
by: SungHyun Nam |
last post by:
Hello,
If there are two ethernet device, how I can select a device to send/receive?
Actually I want to do loopback test between twe ethernet device.
Send a UDP packet through eth0 and receives...
|
by: Cerebro |
last post by:
Hi,
I'm really new to C++. I'd like to know how to send command to serial ports.
Im connecting to a uniwell POS machine and I dont know what commands I'll be using. They gave me this serial...
|
by: opkarmalkar |
last post by:
I want to interface 8051 mcu to pc using USB port.
I want to know that how can i send/receive a byte from pc to 8051 mcu via USB port using C language?
is there any C program availaible which can...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |