473,386 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

send /receive frame serial line

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
Expand|Select|Wrap|Line Numbers
  1. int CCommunicationSérie::OpenCom(DWORD bauderate,CString portcom)
  2. {
  3.  
  4. //    DWORD m_EventMask;
  5.     BOOL  m_bPortReady;    
  6.     memset(&m_overlapped,0,sizeof(m_overlapped));
  7.        m_hCom = CreateFile((LPCSTR)portcom,
  8.         GENERIC_READ | GENERIC_WRITE,
  9.         0,
  10.         NULL,
  11.         OPEN_EXISTING,
  12.         FILE_FLAG_OVERLAPPED,                      
  13.         NULL);               
  14.     if(m_hCom==INVALID_HANDLE_VALUE)
  15.         return 1;
  16.     if ((m_bPortReady=GetCommState(m_hCom,&m_dcb))==0)
  17.     {
  18.         CloseHandle(m_hCom);
  19.         return 2;
  20.     }
  21.  
  22.     //ClearCommBreak(m_hCom);
  23.     //FillMemory( &m_dcb,0, sizeof(m_dcb));
  24.     //SetupComm( m_hCom, m_nInputBufferSize, m_nOutputBufferSize );
  25.     //::GetCommMask( m_hCom, &m_EventMask );
  26.    // ::SetCommMask( m_hCom, 0 );    
  27.     m_dcb.fBinary=1;
  28.     m_dcb.fParity=0;
  29.     m_dcb.fNull = 0; 
  30.     m_dcb.fDsrSensitivity=1;          
  31.     m_dcb.fOutX=0;
  32.     m_dcb.fInX=0;
  33.  
  34.     m_dcb.XonLim =500;
  35.      m_dcb.XoffLim =1;
  36.     m_dcb.EofChar = '$';
  37.     m_dcb.fAbortOnError=1;
  38.      m_dcb.fOutxDsrFlow=1;
  39.     m_dcb.fOutxCtsFlow=1;
  40.     m_dcb.fDtrControl=DTR_CONTROL_ENABLE;
  41.     m_dcb.fRtsControl=RTS_CONTROL_ENABLE;      
  42.     m_dcb.DCBlength = sizeof(DCB);
  43.  
  44.     m_dcb.BaudRate =bauderate;//9600
  45.     m_dcb.ByteSize = 8;
  46.     m_dcb.Parity =NOPARITY;
  47.     m_dcb.StopBits =ONESTOPBIT;
  48.  
  49.       m_bPortReady=SetCommState(m_hCom, &m_dcb); 
  50.        if(m_bPortReady==0) { CloseHandle(m_hCom);return 3;}
  51.  
  52.       }
  53.  
  54.  
  55.     //vider les tampons
  56.    PurgeComm(m_hCom,PURGE_TXABORT|PURGE_RXABORT|PURGE_TXCLEAR|PURGE_RXCLEAR); 
  57.  MessageBoxA("ouverture du port","",MB_OK);    
  58.    return 0;
  59. }
  60.  
writing:
Expand|Select|Wrap|Line Numbers
  1.  bool CCommunicationSérie::WriteCom()
  2.  {
  3.    DWORD NbBytesWritten=0;
  4.   DWORD size=3;
  5.  
  6.  
  7.   //LPBYTE write_buffer=new BYTE[1];write_buffer[0]=0x01;
  8.   const char* write_buffer=new char[3];
  9.  write_buffer="1$";
  10.    if(WriteFile(m_hCom,(LPCVOID)write_buffer,size,&NbBytesWritten,&m_overlapped)==0)
  11.    {
  12.       delete[](write_buffer);
  13.         MessageBoxA("erreur","",MB_OK);            
  14.       return true;//error
  15.    }
  16.    //delete[](write_buffer); 
  17.    MessageBoxA("succés","",MB_OK);              
  18.    return false;
  19.  
  20.  }
  21.  
reading
Expand|Select|Wrap|Line Numbers
  1. bool CCommunicationSérie::ReadCom()
  2. {
  3.   char* read_buffer=new char[5];    
  4.   DWORD nNumberOfBytesToRead=3;
  5.   DWORD NumberOfBytesRead;
  6.   LPVOID ReadBuffer=NULL;
  7.   DWORD size=3;
  8.  
  9.    DWORD dwErrorMask ;
  10.    COMSTAT cs; // structure
  11.  
  12.    ClearCommError(m_hCom, &dwErrorMask , &cs); //initialise cs
  13.    if( cs.cbInQue>=size)
  14.    { 
  15.      if (ReadFile (m_hCom,ReadBuffer,nNumberOfBytesToRead, &NumberOfBytesRead, &m_overlapped))
  16.       {
  17.         if (NumberOfBytesRead == size)
  18.         {
  19.            read_buffer=(char*)ReadBuffer;
  20.                    MessageBoxA("sucées","",MB_OK);
  21.             return false;
  22.         }
  23.       }
  24.    MessageBoxA("erreur","",MB_OK);              
  25.    return true;
  26.    }
  27.    MessageBoxA("erreur","",MB_OK);            
  28.   return true; 
  29. }
  30.  
thanks for help
Mar 31 '11 #1
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.
Mar 31 '11 #2
I'm using 2 USB to serial converters (one for each PC) and they are related with a plug (female female).
Mar 31 '11 #3
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.
Mar 31 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
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...
1
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...
1
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...
1
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...
4
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...
1
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...
0
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...
5
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...
9
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...
4
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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$) { } ...
0
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...
0
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...
0
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
1
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...
0
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...
0
marktang
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,...
0
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.