473,804 Members | 3,469 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Please help with Serial Comms

I can't figure this out, Please Help!!!! I would appreciate any remarks
or opinions or help about the code below.

A you can see I am writing and reading from the serial port, that is
going OK MOST of the time. My big problem is when I want to break the
while loop. I am expecting a message in HEX to come in from the serial
port. for instance "0004 600A", which I get most of the time, sometimes
I just get random ASCII chars, anyway to the point

Question #1 is the way I am storing the read return(it seems to work
alright) the best way to do it, char in[13] ?

Question #2 I need to test the response in terms of binary?
0000 0000 0000 0100 0110 0000 0000 1010

I can manage to test the bit that I am interested in but how to get to
this point I am not to sure about.

Lastly, any remarks or bug fixes on the general structure of the my
code would be great

//*************** *************** *************** *************** *************** *************** *************** ***//

int _tmain(int argc, _TCHAR* argv[])
{
CSerial serial;
//CSyncSerialComm serial2("COM1") ;

char in[13];
char command[20];
DWORD bytesRead;
int value=1000;
int end;
int test;

serial.Open(1, 9600);
//serial.HomePosi tion(4);
while(1)
{
printf("\nEnter Command >>");
scanf("%d", &value);
sprintf(command , "4 lr %d\r4 m\r4 st\r", value);
serial.SendData (command, sizeof(command) );
end = serial.ReadData (&in,20);
in[end] = '\0';
printf("value = %s",in);

if((int)in[7] > 4)
break;
}
serial.Close();
getchar();

return(0);
}
//*************** *************** *********Writin g
Data*********** *************** ************//

bool CSerial::WriteC ommByte( unsigned char ucByte )
{
bool bWriteStat;
DWORD dwBytesWritten;

bWriteStat = WriteFile( m_hIDComDev, (LPSTR) &ucByte, 1,
&dwBytesWritten , &m_OverlappedWr ite );
if( !bWriteStat && ( GetLastError() == ERROR_IO_PENDIN G ) ){
if( WaitForSingleOb ject( m_OverlappedWri te.hEvent, 1000 ) )
dwBytesWritten = 0;
else{
GetOverlappedRe sult( m_hIDComDev, &m_OverlappedWr ite,
&dwBytesWritten , FALSE );
m_OverlappedWri te.Offset += dwBytesWritten;
}
}

return( TRUE );

}

int CSerial::SendDa ta( const char *buffer, int size )
{

if( !m_bOpened || m_hIDComDev == NULL ) return( 0 );

DWORD dwBytesWritten = 0;
int i;
for( i=0; i<size; i++ ){
WriteCommByte( buffer[i] );
dwBytesWritten+ +;
}

return( (int) dwBytesWritten );

}

//*************** *************** ******Reading
Data*********** *************** *************** ***********//
int CSerial::ReadDa taWaiting( void )
{

if( !m_bOpened || m_hIDComDev == NULL ) return( 0 );

DWORD dwErrorFlags;
COMSTAT ComStat;

ClearCommError( m_hIDComDev, &dwErrorFlag s, &ComStat );

return( (int) ComStat.cbInQue );

}

int CSerial::ReadDa ta( void *buffer, int limit )
{

if( !m_bOpened || m_hIDComDev == NULL ) return( 0 );

bool bReadStatus;
DWORD dwBytesRead, dwErrorFlags;
COMSTAT ComStat;

ClearCommError( m_hIDComDev, &dwErrorFlag s, &ComStat );
//if( !ComStat.cbInQu e ) return( 0 );

dwBytesRead = (DWORD) ComStat.cbInQue ;
if( limit < (int) dwBytesRead ) dwBytesRead = (DWORD) limit;

bReadStatus = ReadFile( m_hIDComDev, buffer, dwBytesRead,
&dwBytesRead , &m_OverlappedRe ad );
if( !bReadStatus ){
if( GetLastError() == ERROR_IO_PENDIN G ){
WaitForSingleOb ject( m_OverlappedRea d.hEvent, 2000 );
return( (int) dwBytesRead );
}
return( 0 );
}
return( (int) dwBytesRead );

}

Oct 12 '05 #1
2 2992
salsipius wrote:
I can't figure this out, Please Help!!!! I would appreciate any remarks
or opinions or help about the code below.

A you can see I am writing and reading from the serial port, that is
going OK MOST of the time. My big problem is when I want to break the
while loop. I am expecting a message in HEX to come in from the serial
port. for instance "0004 600A", which I get most of the time, sometimes
I just get random ASCII chars, anyway to the point

Question #1 is the way I am storing the read return(it seems to work
alright) the best way to do it, char in[13] ?

Question #2 I need to test the response in terms of binary?
0000 0000 0000 0100 0110 0000 0000 1010

I can manage to test the bit that I am interested in but how to get to
this point I am not to sure about.

Lastly, any remarks or bug fixes on the general structure of the my
code would be great

[snip]

Parts of your post are on topic, parts are off topic.

First note that the hardware communication you are talking about is
clearly an extension to C++. The standard lang does not know from
serial ports. So, it's very hard to answer your question about
what is going wrong. But the "usual suspects" are things like:
- You may have missed the port having some extra behaviour or
signals indicating conditions. Maybe there is a system flag
or message. Maybe it does something on special conditions.
This is all off topic in this group.
- You may have to set some flag into the port to get it to behave
the way you want. Again, off topic here.
- You may have some signed/unsigned thing or some such type thing.
Maybe you've got more data in the port than fits in your buffer.
This is on-topic here, but it's hard to tell if this is the case,
since I don't have the rest of the class, nor the header files,
nor the docs for using your hardware nor compiler.

My suggestion is, find a news group where people talk about your
specific hardware, or your specific OS, or your specific compiler.
Socks

Oct 12 '05 #2
On Wed, 12 Oct 2005 09:34:40 -0700, salsipius wrote:

char in[13];
char command[20];

...
end = serial.ReadData (&in,20);


This looks really suspicious to me. Should the 20 be "sizeof(in) "?

- Jay
Oct 12 '05 #3

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

Similar topics

8
313
by: Elhanan maayan | last post by:
hi all.. (this message was also posted in vb.general, becouse i didn't know where exactly to fit it). i work in a company that deals with mainly with hardware devices via serial comm port. up untill most of the software was writeen in plain old dos c. that company is planning to move it's applications to windows. the applications themselves are rather small, theh usually just extract binary
2
2582
by: M | last post by:
does c# or the .NET framework provide any serial communications support? where can i find some info on how to handle serial ports in c#? thanks m
6
2877
by: Leandro Berti via DotNetMonster.com | last post by:
Hi All, I wrote a code to do serial communication with an equipament. When i use the code outside of threaded class it seens work properly, but when i put inside a class and execute a thread in the first seconds the communication is ok, later i receive read/write error. I?ve been in MSDN site and there i discover that the read/write error is a INVALID_HANDLE problem. But why??? I just create the serial communication file and use it....
2
4894
by: K.K. | last post by:
I am writing program that will recieve or sent data to selected port (users can select which port they want to recieve or sent) but I don't know how or whick object I can use.
13
1538
by: news.microsoft.com | last post by:
Hi, I don't know how to create and handle a serial communication with VB .Net. If you know somenthing, please tell me. Thank you very much, Silvia
2
4442
by: Carl Gilbert | last post by:
Hi I have a stepper motor and PCB from www.milinst.com and have connect the device to my PC through the serial. When using the downloadable application for the device, I receive a comms error. Does anyone know how to send data to the device to get the motor to turn? Regards, Carl Gilbert
2
2440
by: Reny J Joseph Thuthikattu | last post by:
Hi, Can any one guide me on how to program RS485 with .NET? Regards Reny
0
978
by: sgarcia22 | last post by:
Hello all, I workin on a windows application and it requires communicating using serial ports. The application only has one form called Form1. When I use the serial ports in the Form1.cs file they work just fine. I also have another in the project called COMMS.cs. When I try to use the serial port in the COMMS.cs file I get an error saying that Access to the port was denied. I know the port exists cause i have used it before in the same...
4
2146
by: adlloyd | last post by:
Hi all, I've got an application that's written in C++ making use of MFC (VS6). Its purpose is to process SMS messages received from a GSM modem connected via a serial port (USB connection). The serial communications are handled entirely in their own thread. For the most part this works perfectly.However, there appears to be a problem where multiple instances of the application are running in different Windows XP environments. It seems...
0
9708
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
10589
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
10340
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
10327
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,...
0
10085
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6857
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
5663
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3828
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2999
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.