基于labwindows的串口通信

更新时间:2023-10-23 00:13:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

#include #include #include #include #include #include #include \串口通信.h\

#define text_length 2048 static int config_handle; config_flag; portindex; port_open; RS232Error; comport; baudrate; parity; databits; stopbits; inputq; outputq; xmode; ctsmode; type; stringsize; bytes_sent; send_byte; send_mode; read_cnt; bytes_read;

char devicename[30]; char send_data[text_length]; char read_data[text_length];

char tbox_read_data[text_length]; double timeout;

static void *callbackdata;

static int panelHandle, graphpanel, config;

void ComCallback(int COMport, int eventMask, void *callbackdata); void SetConfigParms (); void GetConfigParms ();

void EnablePanelControls (int);

void SendAscii(void); void SendByte(void);

void DisplayRS232Error (void);

void SetConfigParms () { 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); }

void GetConfigParms () { 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); GetCtrlVal (config_handle, CONFIG_CTSMODE, &ctsmode); GetCtrlVal (config_handle, CONFIG_XMODE, &xmode); GetCtrlVal (config_handle, CONFIG_TIMEOUT, &timeout); GetCtrlIndex (config_handle, CONFIG_COMPORT, &portindex); #ifdef _NI_unix_

devicename[0]=0; #else

GetLabelFromIndex (config_handle, CONFIG_COMPORT, portindex, devicename); #endif }

void EnablePanelControls (int enable) {

SetCtrlAttribute (panelHandle, PANEL_SENDMODE, ATTR_DIMMED, enable); SetCtrlAttribute (panelHandle, PANEL_SENDDATA, ATTR_DIMMED, enable); SetCtrlAttribute (panelHandle, PANEL_SENDNUMBER, ATTR_DIMMED, enable); SetCtrlAttribute (panelHandle, PANEL_RECEIVENUMBER, ATTR_DIMMED, enable); SetCtrlAttribute (panelHandle, PANEL_RECEIVEDATA, ATTR_DIMMED, enable); SetCtrlAttribute (panelHandle, PANEL_STOPRECEIVE, ATTR_DIMMED, enable); SetCtrlAttribute (panelHandle, PANEL_CHECKDATA, ATTR_DIMMED, enable); SetCtrlAttribute (panelHandle, PANEL_CHECKGRAPH, ATTR_DIMMED, enable); SetCtrlAttribute (panelHandle, PANEL_FLUSHINPUT, ATTR_DIMMED, enable); SetCtrlAttribute (panelHandle, PANEL_FLUSHOUTPUT, ATTR_DIMMED, enable); SetCtrlAttribute (panelHandle, PANEL_DATA, ATTR_DIMMED, enable); }

int main (int argc, char *argv[]) { if (InitCVIRTE (0, argv, 0) == 0) return -1; /* out of memory */ if ((panelHandle = LoadPanel (0, \串口通信.uir\ return -1; DisplayPanel (panelHandle); RunUserInterface (); DiscardPanel (panelHandle); return 0; }

int CVICALLBACK configcallback (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { switch (event) { case EVENT_COMMIT: config_handle = LoadPanel (panelHandle, \串口通信.uir\ InstallPopup (config_handle);

if (config_flag) /* Configuration done at least once.*/ SetConfigParms (); else config_flag = 1; break; } return 0; }

int CVICALLBACK sendmodecallback (int panel, int control, int event,

void *callbackData, int eventData1, int eventData2) { switch (event) { case EVENT_COMMIT: GetCtrlVal (panelHandle, PANEL_SENDMODE, &send_mode); if(send_mode) { SetCtrlAttribute (panelHandle, PANEL_SENDDATAPLOT, ATTR_DIMMED, 0); SetCtrlAttribute (panelHandle, PANEL_DATA, ATTR_DIMMED,1); } else { SetCtrlAttribute (panelHandle, PANEL_SENDDATAPLOT, ATTR_DIMMED, 1); SetCtrlAttribute (panelHandle, PANEL_DATA, ATTR_DIMMED,0); } } return 0; }

int CVICALLBACK sendcallback (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { switch (event) { case EVENT_COMMIT: GetCtrlVal (panelHandle, PANEL_SENDMODE, &type); if(type) SendAscii(); else SendByte(); RS232Error = ReturnRS232Err (); if (RS232Error)

DisplayRS232Error(); SetCtrlVal (panelHandle, PANEL_SENDNUMBER, bytes_sent); break; } return 0; }

void SendAscii() {

GetCtrlVal (panelHandle, PANEL_SENDDATAPLOT, send_data); stringsize = StringLength (send_data);

bytes_sent = ComWrt (comport, send_data, stringsize); }

void SendByte() { GetCtrlVal (panelHandle, PANEL_DATA,&send_byte); bytes_sent = ComWrtByte (comport, send_byte); }

int CVICALLBACK receivecallback (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { switch (event) { case EVENT_COMMIT: InstallComCallback (comport, LWRS_RECEIVE, 2, 0, ComCallback, callbackdata);

case EVENT_RIGHT_CLICK : break; } return 0; }

void ComCallback(int COMport, int eventMask, void *callbackdata) {

int inputqueuelength; int bytesread; char buffer[512]; char *matchedchar = 0; double value; int i; inputqueuelength = GetInQLen (comport); bytesread = ComRd (comport, buffer, inputqueuelength); for (i = 0; i <= bytesread; i ++) { matchedchar = buffer + i ; CopyString (tbox_read_data, 0, matchedchar, 0, inputqueuelength); SetCtrlVal (panelHandle, PANEL_RECEIVEDATAPLOT, tbox_read_data);

break; } ArrayToFile (\串口数据.txt\tbox_read_data, VAL_CHAR, inputqueuelength, 1, VAL_DATA_MULTIPLEXED, VAL_GROUPS_AS_COLUMNS, VAL_CONST_WIDTH, 2, VAL_BINARY, VAL_APPEND); }

int CVICALLBACK clearcallback (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { switch (event) { case EVENT_COMMIT: ResetTextBox (panelHandle, PANEL_RECEIVEDATAPLOT, \ break; } return 0; }

int CVICALLBACK stopcallback (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { switch (event) { case EVENT_COMMIT: break; } return 0; }

int CVICALLBACK checkgraphcallback (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { switch (event) { case EVENT_COMMIT:

break; } return 0; }

int CVICALLBACK checkdatacallback (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { switch (event) { case EVENT_COMMIT: break; } return 0; }

int CVICALLBACK quitcallback (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { switch (event) { case EVENT_COMMIT: QuitUserInterface (0); break; } return 0; }

int CVICALLBACK flushoutputcallback (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { switch (event) { case EVENT_COMMIT: FlushOutQ (comport);

MessagePopup (\ break; } return 0; }

int CVICALLBACK flushinputcallback (int panel, int control, int event,

void *callbackData, int eventData1, int eventData2) { switch (event) { case EVENT_COMMIT: FlushInQ (comport);

MessagePopup (\ break; } return 0; }

int CVICALLBACK closecallback (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) { MessagePopup (\Message\\Reset the RS232 Configuer Content \ };

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); } else

EnablePanelControls (1); DiscardPanel (config_handle); break;

} return 0; }

int CVICALLBACK ledcallback (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) {

if (event == EVENT_TIMER_TICK) { } return 0; }

void DisplayRS232Error (void) {

char ErrorMessage[200]; switch (RS232Error) {

default :

if (RS232Error < 0) {

Fmt (ErrorMessage, \ MessagePopup (\ } break; case 0 :

MessagePopup (\ break; case -2 :

Fmt (ErrorMessage, \ \ MessagePopup (\ break; case -3 :

Fmt (ErrorMessage, \ \ MessagePopup (\ break; case -99 :

Fmt (ErrorMessage, \

\

\ check COM Port setting, or\\n\ \ check device.\

MessagePopup (\ break; } }

本文来源:https://www.bwwdw.com/article/lq3f.html

Top