Annotation of hatari/src/rs232.c, revision 1.1.1.3

1.1       root        1: /*
                      2:   Hatari
                      3: 
                      4:   RS-232 Communications
                      5: 
                      6:   This is similar to the Printing functions, we open a direct file to the COM port and send bytes over it.
                      7:   Using such method mimicks the ST exactly, and even allows us to connect to an actual ST! To wait for
                      8:   incoming data, we create a thread which copies the bytes into an input buffer. This method fits in with
                      9:   the internet code which also reads data into a buffer.
                     10: */
                     11: 
1.1.1.3 ! root       12: /* FIXME: The functions here do not yet work with Hatari */
        !            13: 
        !            14: 
1.1       root       15: #include "main.h"
                     16: #include "debug.h"
                     17: #include "dialog.h"
                     18: #include "rs232.h"
1.1.1.2   root       19: 
1.1       root       20: 
                     21: BOOL bConnectedRS232=FALSE;          // Connection to RS232?
                     22: //HANDLE hCom=NULL;                  // Handle to file
                     23: //HANDLE RS232Thread=NULL;           // Thread handle for reading incoming data
                     24: long RS232ThreadID;
                     25: //DCB dcb;                           // Control block
                     26: unsigned char TempRS232InputBuffer[MAX_TEMP_RS232INPUT_BUFFER];
                     27: unsigned char InputBuffer_RS232[MAX_RS232INPUT_BUFFER];
                     28: int InputBuffer_Head=0,InputBuffer_Tail=0;
                     29: 
                     30: //-----------------------------------------------------------------------
                     31: /*
                     32:   Initialize RS-232, start thread to wait for incoming data (we will open a connection when first bytes are sent)
                     33: */
                     34: void RS232_Init(void)
                     35: {
                     36:   // Create thread to wait for incoming bytes over RS-232
                     37: //FIXME   RS232Thread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)RS232_ThreadFunc,0,NULL,&RS232ThreadID);
                     38: }
                     39: 
                     40: //-----------------------------------------------------------------------
                     41: /*
                     42:   Close RS-232 connection and stop checking for incoming data
                     43: */
                     44: void RS232_UnInit(void)
                     45: {
                     46:   // Close, kill thread and free resource
                     47: /* FIXME */
                     48: /*
                     49:   if (RS232Thread) {
                     50:     TerminateThread(RS232Thread,FALSE);
                     51:     CloseHandle(RS232Thread);
                     52:   }
                     53:   RS232_CloseCOMPort();
                     54: */
                     55: }
                     56: 
                     57: //-----------------------------------------------------------------------
                     58: /*
                     59:   Open file on COM port
                     60: */
                     61: BOOL RS232_OpenCOMPort(void)
                     62: {
                     63: /* FIXME */
                     64: /*
1.1.1.3 ! root       65:   char szString[32];
        !            66: 
1.1       root       67:   // Generate correct filename for COM port
                     68:   sprintf(szString,"COM%d",ConfigureParams.RS232.nCOMPort+1);  // 1,2...
                     69: 
                     70:   // Create our COM file for input/output
                     71:   bConnectedRS232 = FALSE;
                     72:   hCom = CreateFile(szString,GENERIC_READ | GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL ); 
                     73:   if (hCom!=INVALID_HANDLE_VALUE) {
                     74:     // Get any early notifications, for thread
                     75:     SetCommMask(hCom,EV_RXCHAR);
                     76:     // Create input/output buffers
                     77:     SetupComm(hCom,4096,4096);
                     78:     // Purge buffers
                     79:     PurgeComm(hCom,PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR);
                     80: 
                     81:     // Set defaults
                     82:     RS232_SetConfig(9600,0,UCR_1STOPBIT|UCR_PARITY|UCR_ODDPARITY);
                     83: 
                     84:     // Set all OK
                     85:     bConnectedRS232 = TRUE;
                     86:   }
                     87: */
                     88:   return(bConnectedRS232);
                     89: }
                     90: 
                     91: //-----------------------------------------------------------------------
                     92: /*
                     93:   Close file on COM port
                     94: */
                     95: void RS232_CloseCOMPort(void)
                     96: {
                     97: /* FIXME */
                     98: /*
                     99:   // Do have file open?
                    100:   if (bConnectedRS232) {
                    101:     // Close
                    102:     CloseHandle(hCom);
                    103:     hCom=NULL;
                    104: 
                    105:     bConnectedRS232 = FALSE;
                    106:   }
                    107: */
                    108: }
                    109: 
                    110: //-----------------------------------------------------------------------
                    111: /*
                    112:   Set hardware configuration of RS-232
                    113: 
                    114:   Ctrl:Communications parameters, (default 0)No handshake
                    115:     Bit 0: XOn/XOff
                    116:     Bit 1: RTS/CTS
                    117: 
                    118:   Ucr: USART Control Register
                    119:     Bit 1:0-Odd Parity, 1-Even Parity
                    120:     Bit 2:0-No Parity, 1-Parity
                    121:     Bits 3,4: Start/Stop bits
                    122:       0 0 0-Start, 0-Stop    Synchronous
                    123:       0 1 0-Start, 1-Stop    Asynchronous
                    124:       1 0 1-Start, 1.5-Stop  Asynchronous
                    125:       1 1 1-Start, 2-Stop    Asynchronous
                    126:     Bits 5,6: 'WordLength'
                    127:       0 0 ,8 Bits
                    128:       0 1 ,7 Bits
                    129:       1 0 ,6 Bits
                    130:       1 1 ,5 Bits
                    131:     Bit 7: Frequency from TC and RC
                    132: */
                    133: void RS232_SetConfig(int Baud,short int Ctrl,short int Ucr)
                    134: {
                    135: /* FIXME */
                    136: /*  
                    137:   // Get current config
                    138:   memset(&dcb,0x0,sizeof(DCB));
                    139:   GetCommState(hCom, &dcb);
                    140:   // Set defaults
                    141:   BuildCommDCB("baud=9600 parity=N data=8 stop=1",&dcb);
                    142:   
                    143:   // Need XOn/XOff?
                    144:   if (Ctrl&CTRL_XON_XOFF) {
                    145:     dcb.fOutX = TRUE;
                    146:     dcb.fInX = TRUE;
                    147:   }
                    148:   // And RTS/CTS?
                    149:   if (Ctrl&CTRL_RTS_CTS)
                    150:     dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
                    151: 
                    152:   // Type of parity(if enabled)
                    153:   if (Ucr&UCR_EVENPARITY)
                    154:     dcb.Parity = EVENPARITY;
                    155:   else
                    156:     dcb.Parity = ODDPARITY;
                    157:   // Need parity?
                    158:   if (Ucr&UCR_PARITY)
                    159:     dcb.fParity = TRUE;
                    160:   // Number of stop bits
                    161:   switch(Ucr&UCR_STARTSTOP) {
                    162:     case UCR_0STOPBIT:        // PC doesn't appear to have no stop bits? Eh?
                    163:     case UCR_1STOPBIT:
                    164:       dcb.StopBits = ONESTOPBIT;
                    165:       break;
                    166:     case UCR_15STOPBIT:
                    167:       dcb.StopBits = ONE5STOPBITS;
                    168:       break;
                    169:     case UCR_2STOPBIT:
                    170:       dcb.StopBits = TWOSTOPBITS;
                    171:       break;
                    172:   }
                    173: 
                    174:   // And set
                    175:   SetCommState(hCom, &dcb);
                    176: */
                    177: }
                    178: 
                    179: //-----------------------------------------------------------------------
                    180: /*
                    181:   Pass bytes from emulator to RS-232
                    182: */
                    183: BOOL RS232_TransferBytesTo(unsigned char *pBytes, int nBytes)
                    184: {
                    185: /* FIXME */
                    186: /*
                    187:   DWORD BytesWritten;
                    188: 
                    189:   // Do need to open a connection to RS232?
                    190:   if (!bConnectedRS232) {
                    191:     // Do have RS-232 enabled?
                    192:     if (ConfigureParams.RS232.bEnableRS232)
                    193:       bConnectedRS232 = RS232_OpenCOMPort();
                    194:   }
                    195: 
                    196:   // Have we connected to the RS232?
                    197:   if (bConnectedRS232) {
                    198:     // Send bytes directly to COM port
                    199:     if (WriteFile(hCom,pBytes,nBytes,&BytesWritten,NULL)) {
                    200:       FlushFileBuffers(hCom);
                    201:     }
                    202: 
                    203:     // Show icon on status bar
                    204:     StatusBar_SetIcon(STATUS_ICON_RS232,ICONSTATE_UPDATE);
                    205: 
                    206:     return(TRUE);  //OK
                    207:   }
                    208:   else
                    209: */    return(FALSE);  //Failed
                    210: 
                    211: }
                    212: 
                    213: //-----------------------------------------------------------------------
                    214: /*
                    215:   Read characters from our internal input buffer(bytes from other machine)
                    216: */
                    217: BOOL RS232_ReadBytes(unsigned char *pBytes, int nBytes)
                    218: {
                    219: /* FIXME */
                    220: /*
                    221:   int i;
                    222: 
                    223:   // Connected?
                    224:   if (bConnectedRS232) {
                    225:     // Read bytes out of input buffer
                    226:     for(i=0; i<nBytes; i++) {
                    227:       *pBytes++ = InputBuffer_RS232[InputBuffer_Head];
                    228:       InputBuffer_Head = (InputBuffer_Head+1)&MAX_RS232INPUT_BUFFER_MASK;
                    229:     }
                    230:     return(TRUE);
                    231:   }
                    232: */
                    233:   return(FALSE);
                    234: 
                    235: }
                    236: 
                    237: //-----------------------------------------------------------------------
                    238: /*
                    239:   Return TRUE if bytes waiting!
                    240: */
                    241: BOOL RS232_GetStatus(void)
                    242: {
                    243: /* FIXME */
                    244: /*
                    245:   // Connected?
                    246:   if (bConnectedRS232) {
                    247:     // Do we have bytes in the input buffer?
                    248:     if (InputBuffer_Head!=InputBuffer_Tail)
                    249:       return(TRUE);
                    250:   }
                    251: */
                    252:   // No, none
                    253:   return(FALSE);
                    254: }
                    255: 
                    256: //-----------------------------------------------------------------------
                    257: /*
                    258:   Add incoming bytes from other machine into our input buffer
                    259: */
                    260: void RS232_AddBytesToInputBuffer(unsigned char *pBytes, int nBytes)
                    261: {
                    262:   int i;
                    263: 
                    264:   // Copy bytes into input buffer
                    265:   for(i=0; i<nBytes; i++) {
                    266:     InputBuffer_RS232[InputBuffer_Tail] = *pBytes++;
                    267:     InputBuffer_Tail = (InputBuffer_Tail+1)&MAX_RS232INPUT_BUFFER_MASK;
                    268:   }
                    269: }
                    270: 
                    271: //-----------------------------------------------------------------------
                    272: /*
                    273:   Thread to read incoming RS-232 data, and pass to emulator input buffer
                    274: */
                    275: /* FIXME */
                    276: /*
                    277: DWORD FAR PASCAL RS232_ThreadFunc(LPSTR lpData)
                    278: {
                    279:   COMSTAT ComStat;
                    280:   DWORD dwErrorFlags;
                    281:   DWORD dwEvtMask;
                    282:   DWORD dwLength;
                    283: 
                    284:   SetCommMask(hCom,EV_RXCHAR);
                    285: 
                    286:   // Check for any RS-232 incoming data
                    287:   while(TRUE) {
                    288:     if (hCom) {
                    289:       // Halt here until we find some data coming through the RS-232
                    290:       dwEvtMask = 0;
                    291:       WaitCommEvent(hCom,&dwEvtMask,NULL);
                    292: 
                    293:       // Chars awaiting? Read them in
                    294:       if ((dwEvtMask&EV_RXCHAR)==EV_RXCHAR) {
                    295:         // Only try to read number of bytes in queue - don't read more than our buffer allows
                    296:         ClearCommError(hCom, &dwErrorFlags, &ComStat );
                    297:         dwLength = min(MAX_TEMP_RS232INPUT_BUFFER, ComStat.cbInQue);
                    298:         // Read the bytes in, if we have any
                    299:         if (dwLength!=0) {
                    300:           // Read into temporary buffer
                    301:           ReadFile(hCom,TempRS232InputBuffer,dwLength,&dwLength,NULL);
                    302:           // And copy into our internal queue
                    303:           RS232_AddBytesToInputBuffer(TempRS232InputBuffer,dwLength);
                    304:         }
                    305:       }
                    306: 
                    307:       // Sleep or a while
                    308:       Sleep(2);
                    309:     }
                    310:     else {
                    311:       // No RS-232 connection, sleep for 20ms
                    312:       Sleep(20);
                    313:     }
                    314:   }
                    315: 
                    316:   return(TRUE);
                    317: }
                    318: */

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.