📄 serial.c
字号:
void *callbackData, int eventData1,
int eventData2)
{
if (event == EVENT_COMMIT)
{
inqlen = GetInQLen (comport);
Fmt (msg, "%s<Input queue length = %i", inqlen);
MessagePopup ("RS232 Message", msg);
}
return 0;
}
/*---------------------------------------------------------------------------*/
/* Get the number of bytes in the output queue. */
/*---------------------------------------------------------------------------*/
int CVICALLBACK GetOutQCallBack (int panel, int control, int event,
void *callbackData, int eventData1,
int eventData2)
{
if (event == EVENT_COMMIT)
{
outqlen = GetOutQLen (comport);
Fmt (msg, "%s<Output queue length = %i", outqlen);
MessagePopup ("RS232 Message", msg);
}
return 0;
}
/*---------------------------------------------------------------------------*/
/* Get the status of the COM port and display it to the user. */
/*---------------------------------------------------------------------------*/
int CVICALLBACK ComStatusCallBack (int panel, int control, int event,
void *callbackData, int eventData1,
int eventData2)
{
if (event == EVENT_COMMIT)
{
com_status = GetComStat (comport);
DisplayComStatus ();
}
return 0;
}
/*---------------------------------------------------------------------------*/
/* Display status information to the user. */
/*---------------------------------------------------------------------------*/
void DisplayComStatus ()
{
com_msg[0] = '\0';
if (com_status & 0x0001)
strcat (com_msg, "Input lost: Input queue"
" filled and characters were lost.\n");
if (com_status & 0x0002)
strcat (com_msg, "Asynch error: Problem "
"determining number of characters in input queue.\n");
if (com_status & 0x0010)
strcat (com_msg, "Parity error.\n");
if (com_status & 0x0020)
strcat (com_msg, "Overrun error: Received"
" characters were lost.\n");
if (com_status & 0x0040)
strcat (com_msg, "Framing error: Stop bits were not received"
" as expected.\n");
if (com_status & 0x0080)
strcat (com_msg, "Break: A break signal was detected.\n");
if (com_status & 0x1000)
strcat (com_msg, "Remote XOFF: An XOFF character was received."
"\nIf XON/XOFF was enabled, no characters are removed"
" from the output queue and sent to another device "
"until that device sends an XON character.\n");
if (com_status & 0x2000)
strcat (com_msg, "Remote XON: An XON character was received."
"\nTransmisson can resume.\n");
if (com_status & 0x4000)
strcat (com_msg, "Local XOFF: An XOFF character was sent to\n"
" the other device. If XON/XOFF was enabled, XOFF is\n"
" transmitted when the input queue is 50%, 75%, and 90%\n"
" full.\n");
if (com_status & 0x8000)
strcat (com_msg, "Local XON: An XON character was sent to\n"
" the other device. If XON/XOFF was enabled, XON is\n"
" transmitted when the input queue empties after XOFF\n"
" was sent. XON tells the other device that it can \n"
" resume sending data.\n");
if (strlen (com_msg) == 0)
strcat (com_msg, "No status bits are set.");
MessagePopup ("RS232 Message", com_msg);
}
/*---------------------------------------------------------------------------*/
/* Send the appropriate data to the port. */
/*---------------------------------------------------------------------------*/
int CVICALLBACK SendCallBack (int panel, int control, int event,
void *callbackData, int eventData1,
int eventData2)
{
if (event == EVENT_COMMIT)
{
GetCtrlVal (panel_handle, SERIAL_SENDMODE, &send_mode);
if (send_mode)
SendAscii ();
else
SendByte ();
RS232Error = ReturnRS232Err ();
if (RS232Error)
DisplayRS232Error ();
SetCtrlAttribute (panel_handle, SERIAL_BYTES, ATTR_DIMMED, 0);
SetCtrlVal (panel_handle, SERIAL_BYTES, bytes_sent);
}
return 0;
}
/*---------------------------------------------------------------------------*/
/* Send one byte to the port. */
/*---------------------------------------------------------------------------*/
void SendByte (void)
{
GetCtrlVal (panel_handle, SERIAL_SENDBYTE, &send_byte);
bytes_sent = ComWrtByte (comport, send_byte);
}
/*---------------------------------------------------------------------------*/
/* Send ASCII characters to the port. */
/*---------------------------------------------------------------------------*/
void SendAscii (void)
{
GetCtrlVal (panel_handle, SERIAL_TBOX_SEND, send_data);
GetCtrlIndex (panel_handle, SERIAL_SENDTERM, &send_term_index);
switch (send_term_index)
{
case 1:
strcat(send_data, "\r");
break;
case 2:
strcat(send_data, "\n");
break;
}
stringsize = StringLength (send_data);
bytes_sent = ComWrt (comport, send_data, stringsize);
}
/*---------------------------------------------------------------------------*/
/* Display pertinent error information. */
/*---------------------------------------------------------------------------*/
int CVICALLBACK ErrorCallBack (int panel, int control, int event,
void *callbackData, int eventData1,
int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
RS232Error = ReturnRS232Err ();
DisplayRS232Error ();
break;
case EVENT_RIGHT_CLICK :
break;
}
return 0;
}
/*---------------------------------------------------------------------------*/
/* Read data from the COM port. */
/*---------------------------------------------------------------------------*/
int CVICALLBACK ReadCallBack (int panel, int control, int event,
void *callbackData, int eventData1,
int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
read_data[0] = '\0';
GetCtrlVal (panel_handle, SERIAL_READ_COUNT, &read_cnt);
GetCtrlIndex (panel_handle, SERIAL_READTERM, &read_term_index);
switch (read_term_index)
{
case 0:
read_term = 0;
break;
case 1:
read_term = 13;
break;
case 2:
read_term = 10;
break;
}
if (read_term)
bytes_read = ComRdTerm (comport, read_data, read_cnt,
read_term);
else
bytes_read = ComRd (comport, read_data, read_cnt);
/* Copy subset of read_data into tbox string for display.
ComRdTerm does not automatically put null byte after
number of bytes read into read_data string. */
CopyString (tbox_read_data, 0, read_data, 0, bytes_read);
SetCtrlVal (panel_handle, SERIAL_TBOX_READ, tbox_read_data);
RS232Error = ReturnRS232Err ();
if (RS232Error)
DisplayRS232Error ();
break;
case EVENT_RIGHT_CLICK :
break;
}
return 0;
}
/*---------------------------------------------------------------------------*/
/* Display Help on the Input Queue control. */
/*---------------------------------------------------------------------------*/
int CVICALLBACK InputQCallBack (int panel, int control, int event,
void *callbackData, int eventData1,
int eventData2)
{
if (event == EVENT_RIGHT_CLICK)
DisplayHelp (InputqHelp);
return 0;
}
/*---------------------------------------------------------------------------*/
/* Check the status of the queues and then terminate the RunUserInterface */
/* loop and end the application...or just provide help. */
/*---------------------------------------------------------------------------*/
int CVICALLBACK QuitCallBack (int panel, int control, int event,
void *callbackData, int eventData1,
int eventData2)
{
switch (event)
{
case EVENT_COMMIT :
if (port_open)
{
outqlen = GetOutQLen (comport);
if (outqlen > 0)
{
MessagePopup ("RS232 Message", "The output queue has\n"
"data in it. Wait for device to receive\n"
"the data or flush the queue.\n");
break;
}
RS232Error = CloseCom (comport);
if (RS232Error)
DisplayRS232Error ();
}
QuitUserInterface (0);
break;
case EVENT_RIGHT_CLICK :
DisplayHelp (QuitHelp);
break;
}
return 0;
}
/*---------------------------------------------------------------------------*/
/* Display error information to the user. */
/*---------------------------------------------------------------------------*/
void DisplayRS232Error (void)
{
char ErrorMessage[200];
switch (RS232Error)
{
default :
if (RS232Error < 0)
{
Fmt (ErrorMessage, "%s<RS232 error number %i", RS232Error);
MessagePopup ("RS232 Message", ErrorMessage);
}
break;
case 0 :
MessagePopup ("RS232 Message", "No errors.");
break;
case -2 :
Fmt (ErrorMessage, "%s", "Invalid port number (must be in the "
"range 1 to 8).");
MessagePopup ("RS232 Message", ErrorMessage);
break;
case -3 :
Fmt (ErrorMessage, "%s", "No port is open.\n"
"Check COM Port setting in Configure.");
MessagePopup ("RS232 Message", ErrorMessage);
break;
case -99 :
Fmt (ErrorMessage, "%s", "Timeout error.\n\n"
"Either increase timeout value,\n"
" check COM Port setting, or\n"
" check device.");
MessagePopup ("RS232 Message", ErrorMessage);
break;
}
}
/*---------------------------------------------------------------------------*/
/* Display help information to the user. */
/*---------------------------------------------------------------------------*/
void DisplayHelp (int HelpId)
{
switch (HelpId)
{
case 1 :
MessagePopup ("Quit Help",
"The Quit button closes the current COM port,\n"
"checks and displays any error messages, \n"
"and then exits this program.");
break;
case 2 :
MessagePopup ("Input Queue Help",
"Specifies the size of the input queue for the "
"selected port.\n"
"Default Value: 512\n"
"Valid Range: 28 to 65516");
break;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -