473,769 Members | 3,893 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Serial Comm Problem: Code Sample

I am not very good in C++ problem and this is my codes below. I do not know
whether I got handshaking involve in my code. Could you guys let me know?
HANDLE OpenComm(char *lpszPort, int nBaud, char *nParity, int nData, int
nStop)
{
HANDLE hCom;
LPDCB lpDcb;

char szCom[10];
memset(szCom, 0, sizeof(szCom));
strcpy(szCom, "\\.\\COM") ;
strcat(szCom, lpszPort);
strcat(szCom, ":");

lpDcb = new(DCB);

//create port handle
hCom =
CreateFile(szCo m,GENERIC_READ| GENERIC_WRITE,0 ,NULL,OPEN_EXIS TING,FILE_FLAG_ O
VERLAPPED,NULL) ;

//failed coz invalid handle provided
if (hCom == INVALID_HANDLE_ VALUE)
{
hCom = NULL;
}

//fail to get port state
if (!GetCommState( hCom,lpDcb))
{
hCom = NULL;
}

//set setting [COM1: baud=9600 parity=N data=8 stop=1]
char strTemp[50];
memset(strTemp, 0, sizeof(strTemp) );
sprintf(strTemp , "baud=%d parity=%s data=%d stop=%d", nBaud, nParity,
nData, nStop);

if (!BuildCommDCB( strTemp,lpDcb))
{
hCom = NULL;
}

if (!SetCommState( hCom,lpDcb))
{
hCom = NULL;
}

// set communication timeouts
// get default values
BOOL bPort;
COMMTIMEOUTS CommTimeouts;

bPort = GetCommTimeouts (hCom, &CommTimeout s);
// set new values
CommTimeouts.Re adIntervalTimeo ut = 15;
CommTimeouts.Re adTotalTimeoutC onstant = 250;
CommTimeouts.Re adTotalTimeoutM ultiplier = 1;
CommTimeouts.Wr iteTotalTimeout Constant = 250;
CommTimeouts.Wr iteTotalTimeout Multiplier = 1;
bPort = SetCommTimeouts (hCom, &CommTimeout s);

return hCom;
}
Nov 21 '05 #1
0 1064

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
9094
by: ^CeFoS^ | last post by:
Hello to everybody, I've done an application that draws in a frame the trajectory of a robot. The robot position is readed through the serial port, and several commands are wrote through the same port to change the direction of the robot. The trajectory frame is managed by an applet, and the project works good when the applet is called by a html document allocated in the same local machine under W98 where the classes and the serial port...
2
4564
by: Mike C. | last post by:
I am trying to get Java v1.4.2 to use my PCI add-on serial port. So far, everything I try gives me javax.comm.NoSuchPortException at javax.comm.CommPortIdentifier.getPortIdentifier(CommPortIdentifier.java:105) at org.sf.javaemu.Serial.SerialCommunications.openConnection(UnknownSource) at javaemu.main.init(Unknown Source)
2
2303
by: bray_stenovations | last post by:
Hello, I am experiencing some strange problems while using a usb to serial adapter vs. a real comm port. The problems started with getting a blue screen at random times. Changing usb to serial drivers seemed to fix that problem. Now I am experiencing a problem where sometimes I am getting a crash while using the usb adapter but everything works perfectly while using a natural comm port. Has anyone else experienced this problem. ...
6
6983
by: Peter Krikelis | last post by:
Hi All, I am having a problem setting up input mode for serial communications. (Sorry about the long code post). The following code is what I use to set up my comm port.
4
7470
by: M. Raab | last post by:
i have written an application that utilizes serail ports. In order not to have to reinvent code, I decided to use John Hind's sample code that he presented in MSDN magazine last year (Serial Comm: Use P/Invoke to Develop a ..NET Base Class Library for Serial Device Communications John Hind - MSDN Magazine - October 2002) it works fine except for one problem. i cannot address any port higher than COM9. I am using an equinox multi-serial...
7
2768
by: Michael Chong | last post by:
I wrote a program that communicate with SerialComm. In every 300 milliseconds, my program continuously send & receive data via the serial port once the program starts. My program is once in a while, the serial port seems corrupted because when my data is sent, it doesn't go through the serial port, so as same to receive process. In order to fix this, I have to close the port and reopen the port again. Why?!! Is there any way to fix...
6
9136
by: Ken Breit | last post by:
I am using the RS232.vb class to talk to the serial port. The problem I am having is when I try to read if anything is on the comm port. I call the read method, with the number of bytes I am expecting to read. As long as I know the length of the command that I will be getting on the input of the port it works great. The problem is when I don't know how long the data I am receiving is. If I request more bytes than I know I am getting...
0
7920
by: coolsilver.net | last post by:
I am not sure if I am in the right group. My problem is this: My VB.NET application prints to the Epson printer using Epson OPOS drivers. I have downloaded the Epson ADK for .NET from the EpsonExpert site (www.epsonexpert.com) I have also downloaded the Epson Advanced Printer Driver from the Epson site. I have installed Microsot POS for .NET on a windows 2000 machine. The installation completes but it throws an error "Error 5008....
3
3687
by: ... | last post by:
Hi I need to send a chr(255) to a serial port. When I send it, through comm.write (chr(255)) it sends a chr(63) ... in Hex, I write chr(&FF) and it actually sends chr(&3F) ... why does this happen, and how can I send it right ? I'm using vb.net 2005 express with framework 2.0 Thanks for an answear ...
8
12319
by: glaskan | last post by:
This code is meant to take an input from the serial port and then save the input from the serial port as the name and as the data to a text file but all i am getting is an empty text file or a text file with only "," in it. It would be much appreciated if anyone could help with this problem. Thanks Private Sub Form_Unload(Cancel As Integer) MSComm1.PortOpen = False ' Close the comm port End Sub Private Sub Timer1_Timer() If...
0
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10211
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10045
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9994
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7408
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6673
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5298
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3958
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.