473,320 Members | 2,110 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,320 software developers and data experts.

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.HomePosition(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);
}
//***************************************Writing
Data**************************************//

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

bWriteStat = WriteFile( m_hIDComDev, (LPSTR) &ucByte, 1,
&dwBytesWritten, &m_OverlappedWrite );
if( !bWriteStat && ( GetLastError() == ERROR_IO_PENDING ) ){
if( WaitForSingleObject( m_OverlappedWrite.hEvent, 1000 ) )
dwBytesWritten = 0;
else{
GetOverlappedResult( m_hIDComDev, &m_OverlappedWrite,
&dwBytesWritten, FALSE );
m_OverlappedWrite.Offset += dwBytesWritten;
}
}

return( TRUE );

}

int CSerial::SendData( 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::ReadDataWaiting( void )
{

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

DWORD dwErrorFlags;
COMSTAT ComStat;

ClearCommError( m_hIDComDev, &dwErrorFlags, &ComStat );

return( (int) ComStat.cbInQue );

}

int CSerial::ReadData( void *buffer, int limit )
{

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

bool bReadStatus;
DWORD dwBytesRead, dwErrorFlags;
COMSTAT ComStat;

ClearCommError( m_hIDComDev, &dwErrorFlags, &ComStat );
//if( !ComStat.cbInQue ) return( 0 );

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

bReadStatus = ReadFile( m_hIDComDev, buffer, dwBytesRead,
&dwBytesRead, &m_OverlappedRead );
if( !bReadStatus ){
if( GetLastError() == ERROR_IO_PENDING ){
WaitForSingleObject( m_OverlappedRead.hEvent, 2000 );
return( (int) dwBytesRead );
}
return( 0 );
}
return( (int) dwBytesRead );

}

Oct 12 '05 #1
2 2969
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
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...
2
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
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...
2
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
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
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. ...
2
by: Reny J Joseph Thuthikattu | last post by:
Hi, Can any one guide me on how to program RS485 with .NET? Regards Reny
0
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...
4
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.