From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,17e2607d925651 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2002-04-12 14:31:07 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!news-out.nuthinbutnews.com!feed-ev1!propagator-sterling!news-in.nuthinbutnews.com!newsfeed.nyc.globix.net!news.stealth.net!news.stealth.net!news-xfer.siscom.net!not-for-mail Newsgroups: comp.lang.ada Subject: [OT] Serial comm in Win using C [Re: Could someone teach me how to use windows API serial port calls.] From: Dan Andreatta References: Message-ID: User-Agent: Xnews/4.11.09 X-Original-NNTP-Posting-Host: 129.252.151.109 Date: 12 Apr 2002 15:31:44 -0700 X-Original-Trace: 12 Apr 2002 15:31:44 -0700, 129.252.151.109 X-COMPLAINTS: Report abuse to abuse@mhogaming.com Organization: Newshosting.com - Highest quality at a great price! www.newshosting.com NNTP-Posting-Host: 5967d465.news.newshosting.com X-Trace: DXC=^k1L3iF6BbIV4YY2 Dan Andreatta wrote in news:Xns91EEB1107559Eandreattamailchemsce@12.252.202.62: > kz105@student.cs.york.ac.uk (Jamie) wrote in > news:ba071575.0204121147.71bee262@posting.google.com: > >> Hi, there: >> >> I'm doing a project that needs an ada program under win2000 to control >> a machine over RS232 serial line. I wonder if there are someone would >> like teach me how to use windows API serial port calls to do it. since >> i'm a beginner of programing, i think I need some detailed examples if >> you could provide. >> >> Thanks in advance. >> > > http://msdn.microsoft.com/library/default.asp?url=/library/en- > us/dnwbgen/html/msdn_serial.asp OK, I feel generous. These are quick hacks in C I used to test the instrument. Compiled w/ MinGW GNU/C 2.8.1 (coming with GNAT 3.13p) // Writes to COM3 and waits an answer #include #include #include int main() { HANDLE hCom; DCB dcb; DWORD dwBytes,dwEvent; COMMTIMEOUTS old_ct,new_ct = {100,0,0,0}; COMMPROP cp; char wbuf[] = "DT0.9;DT;"; char rbuf[10] = "\0\0\0\0\0\0\0\0\0\0"; hCom = CreateFile( "COM3", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL ); GetCommState(hCom, &dcb); dcb.BaudRate = 9600; dcb.ByteSize = 8; dcb.Parity = NOPARITY; dcb.StopBits = ONESTOPBIT; dcb.fDtrControl = 1; dcb.fRtsControl = 1; SetCommState(hCom, &dcb); GetCommState(hCom, &dcb); //GetCommTimeouts(hCom,&old_ct); //SetCommTimeouts(hCom,&new_ct); //SetupComm(hCom,128,128); WriteFile( hCom, wbuf, 9, &dwBytes, NULL ); puts("Wrote DATA\n"); Sleep(500); ReadFile( hCom, rbuf, 10, &dwBytes, NULL ); printf("Read %u bytes : '%s'\n",dwBytes,rbuf); CloseHandle(hCom); return EXIT_SUCCESS; } // Dumps the status on a serial port #include #include #include int main() { HANDLE hCom; DCB dcb; COMMPROP commprop; COMMTIMEOUTS timeouts; FILE *f; f=fopen("COM.txt","w"); hCom = CreateFile( "COM1", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL ); GetCommState(hCom, &dcb); GetCommTimeouts(hCom, &timeouts); GetCommProperties(hCom, &commprop); fprintf(f,"DCB Structure:\n\ DWORD DCBlength = %u\n\ DWORD BaudRate = %u\n\ DWORD fBinary:1 = %u\n\ DWORD fParity:1 = %u\n\ DWORD fOutxCtsFlow:1 = %u\n\ DWORD fOutxDsrFlow:1 = %u\n\ DWORD fDtrControl:2 = %u\n\ DWORD fDsrSensitivity:1 = %u\n\ DWORD fTXContinueOnXoff:1 = %u\n\ DWORD fOutX:1 = %u\n\ DWORD fInX:1 = %u\n\ DWORD fErrorChar:1 = %u\n\ DWORD fNull:1 = %u\n\ DWORD fRtsControl:2 = %u\n\ DWORD fAbortOnError:1 = %u\n\ DWORD fDummy2:17 = %u\n\ WORD wReserved = %u\n\ WORD XonLim = %u\n\ WORD XoffLim = %u\n\ BYTE ByteSize = %u\n\ BYTE Parity = %u\n\ BYTE StopBits = %u\n\ char XonChar = %u\n\ char XoffChar = %u\n\ char ErrorChar = %u\n\ char EofChar = %u\n\ char EvtChar = %u\n\ WORD wReserved1; = %u\n", dcb.DCBlength, dcb.BaudRate, // current baud rate dcb.fBinary, // binary mode, no EOF check dcb.fParity, // enable parity checking dcb.fOutxCtsFlow, // CTS output flow control dcb.fOutxDsrFlow, // DSR output flow control dcb.fDtrControl, // DTR flow control type dcb.fDsrSensitivity, // DSR sensitivity dcb.fTXContinueOnXoff, // XOFF continues Tx dcb.fOutX, // XON/XOFF out flow control dcb.fInX, // XON/XOFF in flow control dcb.fErrorChar, // enable error replacement dcb.fNull, // enable null stripping dcb.fRtsControl, // RTS flow control dcb.fAbortOnError, // abort reads/writes on error dcb.fDummy2, // reserved dcb.wReserved, // not currently used dcb.XonLim, // transmit XON threshold dcb.XoffLim, // transmit XOFF threshold dcb.ByteSize, // number of bits/byte, 4-8 dcb.Parity, // 0-4=no,odd,even,mark,space dcb.StopBits, // 0,1,2 = 1, 1.5, 2 dcb.XonChar, // Tx and Rx XON character dcb.XoffChar, // Tx and Rx XOFF character dcb.ErrorChar, // error replacement character dcb.EofChar, // end of input character dcb.EvtChar, // received event character dcb.wReserved1); // reserved; do not use fprintf(f,"\nCOMMPROP Structure:\n\ WORD wPacketLength = %u WORD wPacketVersion = %u DWORD dwServiceMask = %u DWORD dwReserved1 = %u DWORD dwMaxTxQueue = %u DWORD dwMaxRxQueue = %u DWORD dwMaxBaud = %u DWORD dwProvSubType = %u DWORD dwProvCapabilities = %u DWORD dwSettableParams = %u DWORD dwSettableBaud = %u WORD wSettableData = %u WORD wSettableStopParity = %u DWORD dwCurrentTxQueue = %u DWORD dwCurrentRxQueue = %u DWORD dwProvSpec1 = %u DWORD dwProvSpec2 = %u WCHAR wcProvChar[1] = %u", commprop.wPacketLength, commprop.wPacketVersion, commprop.dwServiceMask, commprop.dwReserved1, commprop.dwMaxTxQueue, commprop.dwMaxRxQueue, commprop.dwMaxBaud, commprop.dwProvSubType, commprop.dwProvCapabilities, commprop.dwSettableParams, commprop.dwSettableBaud, commprop.wSettableData, commprop.wSettableStopParity, commprop.dwCurrentTxQueue, commprop.dwCurrentRxQueue, commprop.dwProvSpec1, commprop.dwProvSpec2, commprop.wcProvChar[1]); printf("\ DTR_CONTROL_DISABLE : %d\n\ DTR_CONTROL_ENABLE : %d\n\ DTR_CONTROL_HANDSHAKE : %d\n", DTR_CONTROL_DISABLE, DTR_CONTROL_ENABLE, DTR_CONTROL_HANDSHAKE); printf("\ RTS_CONTROL_DISABLE : %d RTS_CONTROL_ENABLE : %d RTS_CONTROL_HANDSHAKE : %d RTS_CONTROL_TOGGLE : %d\n", RTS_CONTROL_DISABLE, RTS_CONTROL_ENABLE, RTS_CONTROL_HANDSHAKE, RTS_CONTROL_TOGGLE); CloseHandle(hCom); fclose(f); return EXIT_SUCCESS; }