📄 serial.c
字号:
/*---------------------------------------------------------------------------*/
/* */
/* FILE: serial.c */
/* */
/* PURPOSE: This example illustrates how to use the RS232 Library to send and*/
/* receive commands to and from a device via a serial port. First, */
/* you must configure the port, then send a command to the device, */
/* then receive a command from the device. */
/* */
/* It is important to select the correct terminator on sending and */
/* reading, which depends upon your particular RS232 device. This */
/* program gives the option of none, line feed or carriage return */
/* for terminators. It is also important to make sure you have the */
/* correct type of cable. See the LabWindows/CVI documenation for */
/* more details on cabling and handshaking. */
/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/
/* Include files */
/*---------------------------------------------------------------------------*/
#include <cvirte.h>
#include <userint.h>
#include <rs232.h>
#include <utility.h>
#include <formatio.h>
#include <string.h>
#include "serial.h"
/*---------------------------------------------------------------------------*/
/* Module-globals */
/*---------------------------------------------------------------------------*/
int panel_handle,
config_handle,
comport,
baudrate,
portindex,
parity,
databits,
stopbits,
inputq, /* Sets input queue length in OpenComConfig */
outputq, /* Sets output queue length in OpenComConfig */
xmode,
ctsmode,
stringsize,
bytes_sent,
bytes_read,
RS232Error,
config_flag,
breakstatus,
port_open,
com_status,
send_mode,
send_byte,
send_term_index,
read_term_index,
read_term,
inqlen, /* Stores result from GetInQLen */
outqlen; /* Stores result from GetOutQLen */
short read_cnt;
double timeout;
char devicename[30],
send_data[500],
read_data[2000],
tbox_read_data[2000],
com_msg[500],
msg[100];
#define QuitHelp 1
#define InputqHelp 2
/*---------------------------------------------------------------------------*/
/* Internal function prototypes */
/*---------------------------------------------------------------------------*/
void DisplayRS232Error (void);
void SetConfigParms (void);
void GetConfigParms (void);
void DisplayHelp (int);
void EnablePanelControls (int);
void DisplayComStatus (void);
void ActivateSendControls (int);
void SendAscii (void);
void SendByte (void);
/*---------------------------------------------------------------------------*/
/* This is the application's entry-point. */
/*---------------------------------------------------------------------------*/
int main (int argc, char *argv[])
{
if (InitCVIRTE (0, argv, 0) == 0)
return -1;
panel_handle = LoadPanel (0, "serial.uir", SERIAL);
DisplayPanel (panel_handle);
RunUserInterface ();
DiscardPanel (panel_handle);
CloseCVIRTE ();
return 0;
}
/*---------------------------------------------------------------------------*/
/* Set the port configuration parameters. */
/*---------------------------------------------------------------------------*/
void SetConfigParms (void)
{
SetCtrlVal (config_handle, CONFIG_COMPORT, comport);
SetCtrlVal (config_handle, CONFIG_BAUDRATE, baudrate);
SetCtrlVal (config_handle, CONFIG_PARITY, parity);
SetCtrlVal (config_handle, CONFIG_DATABITS, databits);
SetCtrlVal (config_handle, CONFIG_STOPBITS, stopbits);
SetCtrlVal (config_handle, CONFIG_INPUTQ, inputq);
SetCtrlVal (config_handle, CONFIG_OUTPUTQ, outputq);
SetCtrlVal (config_handle, CONFIG_CTSMODE, ctsmode);
SetCtrlVal (config_handle, CONFIG_XMODE, xmode);
SetCtrlIndex (config_handle, CONFIG_COMPORT, portindex);
}
/*---------------------------------------------------------------------------*/
/* Get the port configuration parameters. */
/*---------------------------------------------------------------------------*/
void GetConfigParms (void)
{
GetCtrlVal (config_handle, CONFIG_COMPORT, &comport);
GetCtrlVal (config_handle, CONFIG_BAUDRATE, &baudrate);
GetCtrlVal (config_handle, CONFIG_PARITY, &parity);
GetCtrlVal (config_handle, CONFIG_DATABITS, &databits);
GetCtrlVal (config_handle, CONFIG_STOPBITS, &stopbits);
GetCtrlVal (config_handle, CONFIG_INPUTQ, &inputq);
GetCtrlVal (config_handle, CONFIG_OUTPUTQ, &outputq);
GetCtrlIndex (config_handle, CONFIG_COMPORT, &portindex);
#ifdef _NI_unix_
devicename[0]=0;
#else
GetLabelFromIndex (config_handle, CONFIG_COMPORT, portindex,
devicename);
#endif
}
/*---------------------------------------------------------------------------*/
/* Let the user configure the port. */
/*---------------------------------------------------------------------------*/
int CVICALLBACK ConfigCallBack (int panel, int control, int event,
void *callbackData, int eventData1,
int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
config_handle = LoadPanel (panel_handle, "serial.uir", CONFIG);
InstallPopup (config_handle);
/* If user already has done configuration, then
display those new parameters. If entering
configuration for 1st time, set config_flag
and use default settings.
*/
if (config_flag) /* Configuration done at least once.*/
SetConfigParms ();
else /* 1st time.*/
config_flag = 1;
break;
case EVENT_RIGHT_CLICK :
break;
}
return(0);
}
/*---------------------------------------------------------------------------*/
/* close the configuration panel. */
/*---------------------------------------------------------------------------*/
int CVICALLBACK CloseConfigCallback (int panel, int control, int event,
void *callbackData, int eventData1,
int eventData2)
{
switch (event)
{
case EVENT_COMMIT :
port_open = 0; /* initialize flag to 0 - unopened */
GetConfigParms ();
DisableBreakOnLibraryErrors ();
RS232Error = OpenComConfig (comport, devicename, baudrate, parity,
databits, stopbits, inputq, outputq);
EnableBreakOnLibraryErrors ();
if (RS232Error) DisplayRS232Error ();
if (RS232Error == 0)
{
port_open = 1;
GetCtrlVal (config_handle, CONFIG_XMODE, &xmode);
SetXMode (comport, xmode);
GetCtrlVal (config_handle, CONFIG_CTSMODE, &ctsmode);
SetCTSMode (comport, ctsmode);
GetCtrlVal (config_handle, CONFIG_TIMEOUT, &timeout);
SetComTime (comport, timeout);
EnablePanelControls (0); /* Enable: no errors */
}
else
EnablePanelControls (1); /* Disable: errors found */
DiscardPanel (config_handle);
break;
}
return(0);
}
/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/
void EnablePanelControls (int enable)
{
SetCtrlAttribute (panel_handle, SERIAL_SEND, ATTR_DIMMED, enable);
SetCtrlAttribute (panel_handle, SERIAL_READ, ATTR_DIMMED, enable);
SetCtrlAttribute (panel_handle, SERIAL_READ_COUNT, ATTR_DIMMED, enable);
SetCtrlAttribute (panel_handle, SERIAL_TBOX_READ, ATTR_DIMMED, enable);
SetCtrlAttribute (panel_handle, SERIAL_BYTES, ATTR_DIMMED, enable);
SetCtrlAttribute (panel_handle, SERIAL_ERROR, ATTR_DIMMED, enable);
SetCtrlAttribute (panel_handle, SERIAL_FLUSHINQ, ATTR_DIMMED, enable);
SetCtrlAttribute (panel_handle, SERIAL_FLUSHOUTQ, ATTR_DIMMED, enable);
SetCtrlAttribute (panel_handle, SERIAL_GETINQ, ATTR_DIMMED, enable);
SetCtrlAttribute (panel_handle, SERIAL_GETOUTQ, ATTR_DIMMED, enable);
SetCtrlAttribute (panel_handle, SERIAL_COMSTATUS, ATTR_DIMMED, enable);
SetCtrlAttribute (panel_handle, SERIAL_READTERM, ATTR_DIMMED, enable);
SetCtrlAttribute (panel_handle, SERIAL_SENDMODE, ATTR_DIMMED, enable);
SetCtrlAttribute (panel_handle, SERIAL_RCV_HELP_MSG, ATTR_DIMMED, enable);
SetCtrlAttribute (panel_handle, SERIAL_TRANS_HELP_MSG, ATTR_DIMMED, enable);
SetCtrlAttribute (panel_handle, SERIAL_CLEARBOX, ATTR_DIMMED, enable);
ActivateSendControls (enable);
}
/*---------------------------------------------------------------------------*/
/* Activate or deactivate the Send controls. For activate, enable = 0, */
/* for deactivate, enable = 1, since 0 is not dimmed and 1 is dimmed. */
/*---------------------------------------------------------------------------*/
void ActivateSendControls (int enable)
{
GetCtrlVal (panel_handle, SERIAL_SENDMODE, &send_mode);
if (send_mode)
{ /* ascii mode */
SetCtrlAttribute (panel_handle, SERIAL_TBOX_SEND, ATTR_DIMMED,
enable);
SetCtrlAttribute (panel_handle, SERIAL_SENDTERM, ATTR_DIMMED, enable);
SetCtrlAttribute (panel_handle, SERIAL_TRANS_HELP_MSG, ATTR_DIMMED,
enable);
SetCtrlAttribute (panel_handle, SERIAL_SENDBYTE, ATTR_DIMMED,
!enable);
}
else
{ /* byte mode */
SetCtrlAttribute (panel_handle, SERIAL_SENDBYTE, ATTR_DIMMED, enable);
SetCtrlAttribute (panel_handle, SERIAL_TBOX_SEND, ATTR_DIMMED,
!enable);
SetCtrlAttribute (panel_handle, SERIAL_SENDTERM, ATTR_DIMMED,
!enable);
SetCtrlAttribute (panel_handle, SERIAL_TRANS_HELP_MSG, ATTR_DIMMED,
!enable);
}
}
/*---------------------------------------------------------------------------*/
/* Clear the character display. */
/*---------------------------------------------------------------------------*/
int CVICALLBACK ClearBoxCallBack (int panel, int control, int event,
void *callbackData, int eventData1,
int eventData2)
{
if (event == EVENT_COMMIT)
ResetTextBox (panel_handle, SERIAL_TBOX_READ, "\0");
return 0;
}
/*---------------------------------------------------------------------------*/
/* Active the send-mode controls. */
/*---------------------------------------------------------------------------*/
int CVICALLBACK SendModeCallBack (int panel, int control, int event,
void *callbackData, int eventData1,
int eventData2)
{
if (event == EVENT_COMMIT)
ActivateSendControls (0);
return 0;
}
/*---------------------------------------------------------------------------*/
/* Flush the input queue. */
/*---------------------------------------------------------------------------*/
int CVICALLBACK FlushInCallBack (int panel, int control, int event,
void *callbackData, int eventData1,
int eventData2)
{
if (event == EVENT_COMMIT)
{
FlushInQ (comport);
MessagePopup ("RS232 Message", "Input queue flushed.");
}
return 0;
}
/*---------------------------------------------------------------------------*/
/* Flush the output queue. */
/*---------------------------------------------------------------------------*/
int CVICALLBACK FlushOutQCallBack (int panel, int control, int event,
void *callbackData, int eventData1,
int eventData2)
{
if (event == EVENT_COMMIT)
{
FlushOutQ (comport);
MessagePopup ("RS232 Message", "Output queue flushed.");
}
return 0;
}
/*---------------------------------------------------------------------------*/
/* Get the number of bytes in the input queue. */
/*---------------------------------------------------------------------------*/
int CVICALLBACK GetInQCallBack (int panel, int control, int event,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -