Serial Port Communication Code In VC++

A

Thread Starter

Anonymous

Can anyone help me with understanding the following peice of vc++ code?

The objective is to create a user interface for a hard ware unit.If by reading this code if anyone can tell me what exactly goes out of the serial port it will be great.

void CStereoDlg::OnDeltaposCoarsePan(NMHDR* pNMHDR, LRESULT* pResult)
{

CString Cli_Rsp,CliCmd;
float nPanSize=1000; // coarse pan = 10 degrees

NM_UPDOWN* pNMUpDown = (NM_UPDOWN*)pNMHDR;

if(!(((CRtsa2App*)AfxGetApp())->nPT_Stat[1][1]))
{
// Calculate angle
pNMUpDown->iPos=((CRtsa2App*)AfxGetApp())->nPT_Stat[1][6]/10;
nPanMoveAngle = pNMUpDown->iPos - pNMUpDown->iDelta*nPanSize;

// Set velocity scale
((CRtsa2App*)AfxGetApp())->nPT_Control->pan_vel_scale(1);

// Move pan axis
((CRtsa2App*)AfxGetApp())->nPT_Control->pan_abs(nPanMoveAngle,((CRtsa2App*)AfxGetApp())->nPT_Stat);
m_PanSpin.SetPos(nPanMoveAngle-pNMUpDown->iDelta);

}
UpdateData(FALSE);

*pResult = 0;
}


void pan_tilt::pan_abs(int angl,long int **status)
{
/* Write data out port */
sprintf(pt_buff,"%ipa%ld\n\r",1,-1*(long int)(angl*10));
pt_port.write_port(pt_buff);

/* Wait for move to start */
/* or timeout before */
/* returing control */
p_s=clock();
do
{
p_e=clock();
p_to=(p_e-p_s)/(float)CLOCKS_PER_SEC;
}while(!status[1][1]&&(p_to<PT_TO));

/* Return control */
return;
}

void serial_port::write_port(char *wp_buff)
{
DWORD written;

/* Write data out port */
if(!WriteFile(comm_h,wp_buff,strlen(wp_buff)*sizeof(char),&written,NULL))
printf("Error writing data to COMM%i port.\n",comm_port);
if(written!=(DWORD)(strlen(wp_buff)*sizeof(char)))
printf("Error writing data to COMM%i.\n",comm_port);

/* Return control */
return;
}
 
B

Branko Kovac

Hi,
My guess is that you are using some sort of dialog.

The string that goes out to the serial port is formed in pan_tilt::pan_abs member function. The string is something like "1pa-3210". The string is formed by sprintf function. If you want to observe the string, either use debugger or include printf statement between sprintf and write_port statement.

printf("%s",pt_buff);

Cheers
 
T

Timothy Anderson

Fire whoever wrote this code.

I'll put some comments BELOW the line of code

> void CStereoDlg::OnDeltaposCoarsePan(NMHDR* pNMHDR, LRESULT* pResult)

Whenever someone hits the uppy-downy button

> NM_UPDOWN* pNMUpDown = (NM_UPDOWN*)pNMHDR;

Take the information the message sends us (an NM_UPDOWN structure)

> if(!(((CRtsa2App*)AfxGetApp())->nPT_Stat[1][1]))

and if some magical application global variable is set

> // Calculate angle
> pNMUpDown->iPos=((CRtsa2App*)AfxGetApp())->nPT_Stat[1][6]/10;

Set the new position to some math done on another application global

> nPanMoveAngle = pNMUpDown->iPos - pNMUpDown->iDelta*nPanSize;

Woo Hoo! a local variable to store stuff in!

> // Set velocity scale
> ((CRtsa2App*)AfxGetApp())->nPT_Control->pan_vel_scale(1);

Call magical application global function pan_vel_scale to do something magical.

> // Move pan axis
> ((CRtsa2App*)AfxGetApp())->nPT_Control->pan_abs(nPanMoveAngle,((CRtsa2App*)AfxGetApp())->nPT_Stat);

Call the function that writes the stuff to the serial port using or local variable and the magical global array

> m_PanSpin.SetPos(nPanMoveAngle-pNMUpDown->iDelta);

Set the position of one of the dialog box elements

>
> }
> UpdateData(FALSE);

then tell the dialog to take all the information stored in the local variables and cram them to the screen.

> *pResult = 0;
> }

> void pan_tilt::pan_abs(int angl,long int **status)
> {
> /* Write data out port */
> sprintf(pt_buff,"%ipa%ld\n\r",1,-1*(long int)(angl*10));
> pt_port.write_port(pt_buff);

OK, here is the actual write. The biggest trick is to decipher the format string which is

%ipa%l\n\r

In long hand this is "write an integer, the string "pa" a long, then a "new line" then a carriage return.

Good luck!

Tim
 
D

David Wooden

1pa(-Angle * 10)(0x0a)(0x0d)

where (-Angle*10) is the negative content of the variable Angl multiplied by 10, 0x0a is the newline character and 0x0d is the carriage return
character.
 
A

Alex Pavloff

> /* Write data out port */
> sprintf(pt_buff,"%ipa%ld\n\r",1,-1*(long int)(angl*10));

If angl = 10, then the string will be "1pa-100\n\r", where \n and \r are LF and CR. This is what is sent to WriteFile. The "-100" portion of the string is (angl * -10).

Alex Pavloff - [email protected]
Eason Technology -- www.eason.com
 
C

Curt Wuollet

I can't help much, but now you know why I believe in and advocate Bonehead C (tm.)

Regards

cww
 
What goes out the serial port is all in this statement.

sprintf(pt_buff,"%ipa%ld\n\r",1,-1*(long int)(angl*10));

pt_buff = "1pa<angle>\n\r" where <angle> is -1 * angl * 10, \n is line feed, and \r is carriage return, both ASCII characters
 
Top