//退出州 #define EXIT_SUCCESS 1 #define EXIT_UNSUCCESS 0 / ** *定义一些应该外包到配置文件的信息 * / // COM1的设备总和/> #define SUM_DEVICES_COM1 1 / ** *串口常量 * / //返回值/错误代码 #define SERIAL_OK 1 #define SERIAL_E_COMPORT_NOT_DEFINED -1 #define SERIAL_E_WRONG_COMPT -2 #define SERIAL_E_WRONG_COMORT -3 //寄存器3的串行模式 0-1:00 #define SERIAL_MODE_6BIT 0x01 //位 0-1:01 #define SERIAL_MODE_7BIT 0x02 //位 0-1:10 #define SERIAL_MODE_8BIT 0x03 //位 0-1:11 #define SERIAL_MODE_1STOPBIT 0x00 //位2:0 #define SERIAL_MODE_PARITY_NON 0x00 //位3-5:000 #define SERIAL_MODE_PARITY_EVEN 0x18 //位3-5:011 #define SERIAL_MODE_PARITY_MARK 0x28 //位3-5:101 #define SERIAL_MODE_PARITY_SPACE 0x38 //位3 -5:111 #define SERIAL_MODE_BREAK_CONTROL 0x40 //位6:0或 1中断控制; 向接收方发送中断条件 #define SERIAL_MODE_ENABLE_DLR 0x80 //位7:0或者 1 DLR访问 启用;如果设置,寄存器0和1成为一个大字寄存器( DLR) / ** *功能 - 原型 * / int serialInterfaceConnect(int nComPort,const int nBaud,const int nMode); void serialInterfaceSetBaudrate(int nComPort,const int nBaud); char serialInterfaceGetRegister(int nComPort,int nRegister); void serialInterfaceSendData(int nComPort,char * cString ,int nLen); int serialInterfaceConnect(int nComPort,const int nBaud,const int nMode); int serialInterfaceGetPort(int nComPort) ; void serialInterfaceSendTelegram(int nCom,char cCommand,int nSensor); char vaseGenerateChecksum(const char * cTelegram); / ** *函数/例程 * / int main(){ int i = 1; int c1 = 0,c2 = 0; int input; int ii = 0,rts_delay = 20000; / *连接到COM2 * / if(serialInterfaceConnect(serialInterfaceGetPort(2),1200, SERIAL_MODE_7BIT | SERIAL_MODE_2STOPBIT | SERIAL_MODE_PARITY_EVEN)){ printf(连接到COM2.\); saveToLog(DEBUG,连接到COM2。); / *连接到COM1 * / if(serialInterfaceConnect(serialInterfaceGetPort(1), 19200, SERIAL_MODE_7BIT | SERIAL_MODE_2STOPBIT | SERIAL_MODE_PARITY_EVEN)){ printf(连接到COM1.\); saveToLog(DEBUG,连接到COM1。 ); / ** *初始化所有设备 * / printf (***初始化设备*** \ n); for(i = 1; i< = SUM_DEVICES_COM1; i ++){ // for每个设备在com1 //初始化传感器 printf(激活传感器%d。\ n,i); saveToLog(INFO,激活传感器+ i); serialInterfaceSendTelegram(serialInterfaceGetPort(1),''T'',i ); //收到结果 c1 = 0; c1 = inp(serialInterfaceGetPort(1)+ 5); //如果是,那么获得Char 而(c1& 1){ c2 = inp(serialInterfaceGetPort(1)+ 0); printf(" IN%c \ n",c2); //打印字符到屏幕 c1 = inp(serialInterfaceGetPort(1)+ 5); } //结束时 //等待500毫秒 延迟(500); //获取版本 printf(从传感器获取版本%d。 \ n,i); saveToLog(INFO,从获取版本) b $ b sensor。"); saveToLog(INFO,i); serialInterfaceSendTelegram(serialInterfaceGetPort(1),''V '',i); //接收结果 c1 = 0; c1 = inp(serialInterfaceGetPort(1) + 5); //如果是这样,那么获得Char 而(c1& 1){ c2 = inp(serialInterfaceGetPort(1)+ 0); printf(" IN%c \ n", C2); //打印字符到屏幕 c1 = inp(serialInterfaceGetPort(1)+ 5); } //结束时 } //每个设备的结束初始化 } else { printf("错误:无法连接到COM1.\\\; saveToLog(错误,无法连接到 COM1。); 返回EXIT_UNSUCCESS ; } } else { printf("错误:无法连接到COM2.\\ \\ n"); saveToLog(错误,无法连接到COM2。); 返回EXIT_UNSUCCESS; } 返回EXIT_SUCCESS; } //结束函数main int serialInterfaceConnect(int nCom, const int nBaud,const int nMode) { / * *此函数连接到指定的串口 * / int nTmp = 0; outp(nCom + 1,0); //关闭中断 outp(nCom + 3,0x80); // SET DLAB ON serialInterfaceSetBaudrate(nCom,nBaud); //设置 波特率 nTmp = nMode& 0x03; if(nTmp == SERIAL_MODE_5BIT) saveToLog(DEBUG," SERIAL_MODE_5BIT"); if(nTmp == SERIAL_MODE_6BIT) saveToLog(DEBUG," SERIAL_MODE_6BIT"); if(nTmp == SERIAL_MODE_7BIT) saveToLog(DEBUG," SERIAL_MODE_7BIT") ; if(nTmp == SERIAL_MODE_8BIT) saveToLog(DEBUG," SERIAL_MODE_8BIT"); nTmp = nMode& ; 0x04; if(nTmp == SERIAL_MODE_1STOPBIT) saveToLog(DEBUG," SERIAL_MODE_1STOPBIT"); if(nTmp == SERIAL_MODE_2STOPBIT) saveToLog(DEBUG," SERIAL_MODE_2STOPBIT"); nTmp = nMode& 0x38; if(nTmp == SERIAL_MODE_PARITY_NON) saveToLog(DEBUG," SERIAL_MODE_PARITY_NON"); if(nTmp == SERIAL_MODE_PARITY_ODD) saveToLog(DEBUG," SERIAL_MODE_PARITY_ODD"); if(nTmp == SERIAL_MODE_PARITY_EVEN) saveToLog(DEBUG," SERIAL_MODE_PARITY_EVEN") ; if(nTmp == SERIAL_MODE_PARITY_MARK) saveToLog(DEBUG," SERIAL_MODE_PARITY_MARK"); if(nTmp == SERIAL_MODE_PARITY_SPACE) saveToLog(DEBUG," SERIAL_MODE_PARITY_SPACE"); nTmp = nMode& SERIAL_MODE_BREAK_CONTROL; if(nTmp == SERIAL_MODE_BREAK_CONTROL) saveToLog(DEBUG," SERIAL_MODE_BREAK_CONTROL"); nTmp = nMode& SERIAL_MODE_ENABLE_DLR; if(nTmp == SERIAL_MODE_ENABLE_DLR) saveToLog(DEBUG," SERIAL_MODE_ENABLE_DLR"); outp(nCom + 3,nMode); outp(nCom + 2,0xC7); // FIFO控制寄存器 saveToLog(DEBUG,关闭RTS。); outp(nCom + 4,0x0); //关闭DTR,RTS和OUT2 返回SERIAL_OK; } void serialInterfaceSetBaudrate(int nCom,const int nBaud){ int nDivisor = 115200 / nBaud; // 0x1c200 / nBaud // nDivisor默认值: // ------------------ // 0x03 = 38,400 BPS // 0x01 = 115,200 BPS // 0x02 = 57,600 BPS // 0x06 = 19,200 BPS // 0x0C = 9,600 BPS // 0x18 = 4,800 BPS // 0x30 = 2,400 BPS // 0x60 = 1,200 BPS outp(nCom + 0,nDivisor); //设置波特率 - 除数 Latch低字节 outp(nCom + 1,0 0x00); //设置波特率 - 除数 闩锁高字节 } void serialInterfaceSendData(int nCom,char * cString,int nLen){ int i = 0; for(; i< nLen; i ++){ outp(nCom + 0,cString [i]); } //结束 } int serialInterfaceGetPort(int nComPort){ / ** *这个函数得到了端口的地址 * / int nPort = 0; 开关(nComPort){ 案例1: nPort = 0x3F8; 休息; 案例2: nPort = 0x2F8; 休息; 案例3: nPort = 0x3E8; 休息; 案例4: nPort = 0x2E8; 休息; 默认: 返回SERIAL_E_COMPORT_NOT_DEFINED; } printf("返回for%d comPort%d。\ n",nComPort,nPort); 返回nPort; } 无效的服务ialInterfaceSendTelegram(int nCom,char cCommand,int nSensor) { / * *此函数通过寄存器4发送定义的电报 *到传感器 * / char cBuf [10]; char cChecksum; char cTelegram [5]; int nLen; int i = 0; int c1,c2; printf(发送电报为%d。\ n,nCom); //建立电报数据 cTelegram [0] =''b''; cTelegram [1] = cCommand; cTelegram [2] = nSensor + 48; // +''0'' cTelegram [3] =''\ r''; cTelegram [4] = 0x0; //生成校验和 cChecksum = baseGenerateChecksum((char *)& cTelegram [0]); //数据到发送 strcpy(cBuf,cTelegram); nLen = strlen(cTelegram); cBuf [4] = cChecksum; nLen ++; cBuf [5] = 0x0; nLen ++; / * *发送电报给COM * / printf(手动发送电报到OUT:%s \ n,cBuf); // printf(Telegram is%d characters long.\ n,nLen); //打开RTS outp(nCom + 4,0x0B); //打开RTS for(i = 0; i< nLen; i ++){ //发送数据 printf(" Data%c\ n。",cBuf [i]); outp(nCom + 0,cBuf [i]); } //关闭RTS outp(nCom + 4,0x0); //关闭RTS } //结束函数 char baseGenerateChecksum(const char * cTelegram){ int nChecksum = 0; unsigned int i = 0; for(; i< strlen(cTelegram); i ++){ nChecksum = nChecksum ^ cTelegram [i]; // Jedes Zeichen mit XOR $ b $bverknüpfen } // Letztes Zeichen muss ein \(ASCII 13)sein。 if(cTelegram [strlen(cTelegram) - 1]!=''\ r'') nChecksum = nChecksum ^ ''\ r''; nChecksum = nChecksum ^ 0xFF; // invertieren nChecksum = nChecksum& 0x7F的; // auf 7 Bit beschneiden return(char)nChecksum; Hello members, i have a (time-(?))-problem with the serial-communication with an ms-dos 6.2 and the rs232-interface with an rs485-adapter behind theinterface. I program in C. As compiler, i am using Open Watcom to build a DOS 16-bit executable. When i send the telegram to the device, the device answers very fast(<1 millisecond).After complete sending, i set the rts to 0x0 and wait for the answer,but i got no answer, or i am to slow.Somewhere is the error. Maybe someone has some ideas or can give me some tips for bettercoding? Thanks for your help. Here is my complete code: #include <stdio.h>//#include <stdlib.h>#include <string.h>#include <conio.h>#include <dos.h>#include <time.h> /** Define some program keys*/ // Exit-States#define EXIT_SUCCESS 1#define EXIT_UNSUCCESS 0 /*** Define some information which should be outsourced to a config file*/ // Sum of devices at COM1#define SUM_DEVICES_COM1 1 /*** Constants for serial port*/ // Return values / Error codes#define SERIAL_OK 1#define SERIAL_E_COMPORT_NOT_DEFINED -1#define SERIAL_E_WRONG_COMPT -2#define SERIAL_E_WRONG_COMORT -3 // Serial mode for register 3#define SERIAL_MODE_5BIT 0x00 // Bit0-1: 00#define SERIAL_MODE_6BIT 0x01 // Bit0-1: 01#define SERIAL_MODE_7BIT 0x02 // Bit0-1: 10#define SERIAL_MODE_8BIT 0x03 // Bit0-1: 11#define SERIAL_MODE_1STOPBIT 0x00 // Bit 2: 0#define SERIAL_MODE_2STOPBIT 0x04 // Bit 2: 1#define SERIAL_MODE_PARITY_NON 0x00 // Bit 3-5: 000#define SERIAL_MODE_PARITY_ODD 0x08 // Bit 3-5: 001#define SERIAL_MODE_PARITY_EVEN 0x18 // Bit 3-5: 011#define SERIAL_MODE_PARITY_MARK 0x28 // Bit 3-5: 101#define SERIAL_MODE_PARITY_SPACE 0x38 // Bit 3-5: 111#define SERIAL_MODE_BREAK_CONTROL 0x40 // Bit 6: 0 or1 Break control;sends the receiver a break condition#define SERIAL_MODE_ENABLE_DLR 0x80 // Bit 7: 0 or1 DLR accessenable; if set, registers 0 and 1 become one big word register (theDLR) /*** Function-Prototypes*/ int serialInterfaceConnect(int nComPort, const int nBaud, const intnMode);void serialInterfaceSetBaudrate(int nComPort, const int nBaud);char serialInterfaceGetRegister(int nComPort, int nRegister);void serialInterfaceSendData(int nComPort, char *cString, int nLen);int serialInterfaceConnect(int nComPort, const int nBaud, const intnMode);int serialInterfaceGetPort(int nComPort);void serialInterfaceSendTelegram(int nCom, char cCommand, intnSensor);char vaseGenerateChecksum(const char *cTelegram); /*** Functions / Routines*/int main(){ int i=1;int c1=0,c2=0;int input;int ii=0, rts_delay=20000; /* Connect to COM2 */if(serialInterfaceConnect(serialInterfaceGetPort(2 ), 1200,SERIAL_MODE_7BIT | SERIAL_MODE_2STOPBIT | SERIAL_MODE_PARITY_EVEN)){printf("Connected to COM2.\n");saveToLog(DEBUG, "Connect to COM2."); /* Connect to COM1 */if(serialInterfaceConnect(serialInterfaceGetPort(1 ),19200,SERIAL_MODE_7BIT | SERIAL_MODE_2STOPBIT | SERIAL_MODE_PARITY_EVEN)){printf("Connected to COM1.\n");saveToLog(DEBUG, "Connect to COM1."); /*** Initialise all devices*/printf("*** Initialise devices ***\n");for(i=1;i<=SUM_DEVICES_COM1; i++){// for every device at com1 // Initialise sensorprintf("Activate sensor %d.\n",i);saveToLog(INFO, "Activate sensor " +i); serialInterfaceSendTelegram(serialInterfaceGetPort (1), ''T'', i); // Receive resultc1 = 0;c1 = inp(serialInterfaceGetPort(1) +5);// If so, then get Charwhile(c1 & 1){c2 =inp(serialInterfaceGetPort(1) + 0);printf("IN %c\n",c2); // Print Char to Screenc1 =inp(serialInterfaceGetPort(1) + 5);}// end while // wait 500 millisecondsdelay(500); // Get Versionprintf("Get Version from sensor %d.\n", i);saveToLog(INFO, "Get Version fromsensor.");saveToLog(INFO, i); serialInterfaceSendTelegram(serialInterfaceGetPort (1), ''V'', i); // Receive resultc1 = 0;c1 = inp(serialInterfaceGetPort(1) +5);// If so, then get Charwhile(c1 & 1){c2 =inp(serialInterfaceGetPort(1) + 0);printf("IN %c\n",c2); // Print Char to Screenc1 =inp(serialInterfaceGetPort(1) + 5);}// end while}// end initialise for every device}else {printf("Error: Could not connect to COM1.\n");saveToLog(ERROR, "Could not connect toCOM1.");return EXIT_UNSUCCESS;}}else {printf("Error: Could not connect to COM2.\n");saveToLog(ERROR, "Could not connect to COM2.");return EXIT_UNSUCCESS;} return EXIT_SUCCESS; }// end function main int serialInterfaceConnect(int nCom, const int nBaud, const int nMode){/** This function connect to an specified serial-port*/int nTmp = 0; outp(nCom + 1, 0); // Turn off interruptsoutp(nCom + 3, 0x80); // SET DLAB ONserialInterfaceSetBaudrate(nCom, nBaud); // Setbaud rate nTmp = nMode & 0x03;if(nTmp == SERIAL_MODE_5BIT)saveToLog(DEBUG, " SERIAL_MODE_5BIT");if(nTmp == SERIAL_MODE_6BIT)saveToLog(DEBUG, " SERIAL_MODE_6BIT");if(nTmp == SERIAL_MODE_7BIT)saveToLog(DEBUG, " SERIAL_MODE_7BIT");if(nTmp == SERIAL_MODE_8BIT)saveToLog(DEBUG, " SERIAL_MODE_8BIT"); nTmp = nMode & 0x04;if(nTmp == SERIAL_MODE_1STOPBIT)saveToLog(DEBUG, " SERIAL_MODE_1STOPBIT");if(nTmp == SERIAL_MODE_2STOPBIT)saveToLog(DEBUG, " SERIAL_MODE_2STOPBIT"); nTmp = nMode & 0x38;if(nTmp == SERIAL_MODE_PARITY_NON)saveToLog(DEBUG, " SERIAL_MODE_PARITY_NON");if(nTmp == SERIAL_MODE_PARITY_ODD)saveToLog(DEBUG, " SERIAL_MODE_PARITY_ODD");if(nTmp == SERIAL_MODE_PARITY_EVEN)saveToLog(DEBUG, " SERIAL_MODE_PARITY_EVEN");if(nTmp == SERIAL_MODE_PARITY_MARK)saveToLog(DEBUG, " SERIAL_MODE_PARITY_MARK");if(nTmp == SERIAL_MODE_PARITY_SPACE)saveToLog(DEBUG, " SERIAL_MODE_PARITY_SPACE"); nTmp = nMode & SERIAL_MODE_BREAK_CONTROL;if(nTmp == SERIAL_MODE_BREAK_CONTROL)saveToLog(DEBUG, " SERIAL_MODE_BREAK_CONTROL");nTmp = nMode & SERIAL_MODE_ENABLE_DLR;if(nTmp == SERIAL_MODE_ENABLE_DLR)saveToLog(DEBUG, " SERIAL_MODE_ENABLE_DLR"); outp(nCom + 3, nMode);outp(nCom + 2, 0xC7); // FIFO Control RegistersaveToLog(DEBUG, "Turn off RTS.");outp(nCom + 4, 0x0); // Turn off DTR, RTS, and OUT2 return SERIAL_OK; } void serialInterfaceSetBaudrate(int nCom, const int nBaud){int nDivisor = 115200 / nBaud; // 0x1c200 / nBaud // nDivisor defaults:// ------------------// 0x03 = 38,400 BPS// 0x01 = 115,200 BPS// 0x02 = 57,600 BPS// 0x06 = 19,200 BPS// 0x0C = 9,600 BPS// 0x18 = 4,800 BPS// 0x30 = 2,400 BPS// 0x60 = 1,200 BPS outp(nCom + 0, nDivisor); // Set Baud rate - DivisorLatch Low Byteoutp(nCom + 1, 0x00); // Set Baud rate - DivisorLatch High Byte } void serialInterfaceSendData(int nCom, char *cString, int nLen){int i=0;for(; i<nLen; i++){outp(nCom + 0, cString[i]);}// end for } int serialInterfaceGetPort(int nComPort){/*** This functions get the adress for the port*/int nPort = 0; switch(nComPort){case 1:nPort = 0x3F8;break;case 2:nPort = 0x2F8;break;case 3:nPort = 0x3E8;break;case 4:nPort = 0x2E8;break;default:return SERIAL_E_COMPORT_NOT_DEFINED;} printf("Return for %d comPort %d.\n", nComPort, nPort); return nPort; } void serialInterfaceSendTelegram(int nCom, char cCommand, int nSensor){/** This function sends a defined telegram over register 4* to the sensor*/ char cBuf[10];char cChecksum;char cTelegram[5];int nLen;int i = 0; int c1, c2; printf("Send Telegram for %d.\n", nCom); // Build telegram datacTelegram[0] = ''b'';cTelegram[1] = cCommand;cTelegram[2] = nSensor + 48; // + ''0''cTelegram[3] = ''\r'';cTelegram[4] = 0x0; // Generate checksumcChecksum = baseGenerateChecksum((char*) &cTelegram[0]); // Data to sendstrcpy(cBuf, cTelegram);nLen = strlen(cTelegram); cBuf[4] = cChecksum;nLen++;cBuf[5] = 0x0;nLen++; /** Send telegram to COM*/printf("Send telegram manually to OUT: %s\n", cBuf);// printf("Telegram is %d characters long.\n",nLen); // Open RTSoutp(nCom + 4, 0x0B); // Turn on RTS for(i=0;i<nLen;i++){// Send dataprintf("Data %c\n.", cBuf[i]);outp(nCom + 0, cBuf[i]);} // Close RTSoutp(nCom + 4, 0x0); // Turn off RTS } // end function char baseGenerateChecksum(const char *cTelegram){int nChecksum = 0;unsigned int i=0; for(; i<strlen(cTelegram); i++) {nChecksum = nChecksum ^ cTelegram[i]; // Jedes Zeichenmit XORverknüpfen} // Letztes Zeichen muss ein \r (ASCII 13) sein.if(cTelegram[strlen(cTelegram) - 1] != ''\r'')nChecksum = nChecksum ^ ''\r''; nChecksum = nChecksum ^ 0xFF; // invertierennChecksum = nChecksum & 0x7F; // auf 7 Bit beschneidenreturn (char) nChecksum; 推荐答案 On 10 Mai,14 :28,Michael Post< michael.p ... @ purematic.dewrote: On 10 Mai, 14:28, Michael Post <[email protected]: 你好成员, Hello members, ...... 是outp和inp以最快的方式与界面沟通,或者 其他功能更快? 感谢您的帮助。 Michael ......is outp and inp the fastest way to communicate with the interface, orother functions are faster? Thanks for your help. Michael On 5 ??10è?, ????8ê±55·?, Michael Post< michael.p ... @ purematic.dewrote: On 5??10è?, ????8ê±55·?, Michael Post <[email protected]: On 10 Mai,14:28,Michael发布< michael.p ... @ purematic.dewrote:你好成员, ..... 是outp和inp与界面沟通的最快方式,或者 其他功能更快? 感谢您的帮助。 Michael On 10 Mai, 14:28, Michael Post <[email protected]:Hello members,.....is outp and inp the fastest way to communicate with the interface, orother functions are faster?Thanks for your help.Michael 我认为inp()和outp( )函数是用C语言访问 界面的最快方法。否则,您可以使用汇编语言访问接口的。抱歉,因为我忘记了关于UART接口的 详细信息,所以无法给你更多帮助。但我认为由C 编写的程序可以通过PC上的COMM端口以超过921600bps的速率发送数据。 所以,我想可能你应该检查是否接收器返回 接听信号。可能你需要示波器或逻辑分析仪来支持你调试这个程序。 I think the inp() and outp() function is the fastest way to access theinterface in C language. Else you can use assembly language to accessthe interface. Sorry for can''t give you more help because I forgot thedetail about the UART interface. But I think a program written by Ccan send data through COMM port in PC at a rate more than 921600bps.So, I think may be you should check whether the receiver return theanswer signal. May be you need a oscilloscope or a logic analyser tohelp you debug this program. 在文章< 11 * *********************@p77g2000hsh.googlegroups .com> ;, Michael Post< mi ******** **@purematic.dewrote: In article <11**********************@p77g2000hsh.googlegroups .com>,Michael Post <mi**********@purematic.dewrote: >我有一个(时间 - (?)) - 与ms的串行通信问题 - dos 6.2和带有rs485适配器的rs232接口位于接口后面。 >i have a (time-(?))-problem with the serial-communication with an ms-dos 6.2 and the rs232-interface with an rs485-adapter behind theinterface. 标准C语言只包含一个fopen()函数;如果fopen() 不足以打开设备正确的 通信参数,那么你需要调用系统 - 超出C范围的特定库。 用另一种方式表达:你所要求的不能用标准C中的,以及你需要在一个特定于新闻组的新闻组中询问你的操作系统。 - 有什么可以说的,请参阅,这是新的?它已经过时了,这已经是我们面前的了。 - 传道书 The standard C language only includes an fopen() function; if fopen()is not sufficient to open the device conditioned to the rightcommunication parameters, then you will need to call upon system-specific libraries that are beyond the scope of C. To phrase this another way: what you have asked for cannot be donein standard C, and you need to ask in a newsgroup specific toyour OS.--Is there any thing whereof it may be said, See, this is new? It hathbeen already of old time, which was before us. -- Ecclesiastes 这篇关于函数inp / outp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-30 05:35