View Full Version : Help needed with RS232 program
mephistopheles
30-04-2003, 4:22 PM
I have a monitor with a DB9 external remote port, which accepts TTL high/low signals for switching between wired/wireless remote control; on/standby and x4 input source selection. I want to connect to this port from the HCPC serial port (eg via Max233CPP chip(s)) with a view to remote autoswitching from HCPC and need to get/write a program which will handle it.
I think all I really need to be able to do is initialise the serial port and send on/off RS232 signals to the Max233(s), which will simply convert them to high/low TTL equivalents and transmit them to the relevant DB9 pins on the monitor. I've got a vague idea of what I'm doing with the hardware but as I have only limited knowledge and experience of 8086 Assembler and some C from years ago, I'm a bit lost on the programming. Can anyone help me get started, please?
Gerbil
01-05-2003, 12:08 AM
I don't pretend to understand exactly what you're up to, but...
Girder (http://www.girder.nl) has a plugin for controlling the serial port.
Less fun than brushing up your programming skills, but it might be quicker.
mephistopheles
01-05-2003, 1:10 AM
Thanks, but, unfortunately, the Girder plugin requires the "device settings" to work and that's the bit I have a problem with.
The snag is that, whilst on the one hand what I need to do is very simple, ie issue just a series of ons/offs from the RS232 signal pins, this means that on the other it is a lot more complex than usual because I won't actually be sending any data, which is what the thing's designed for.
Having re-thought it through, it's probably going to be easier to incorporate a PIC rather than use multiple Max233s and stick with standard RS232 protocol rather than trying to hack the UART. I can send data signals to the PIC from the HCPC serial port via the Max233 using RS232 protocol, and the PIC will then select the high/low TTL output to the relevant pin(s) on the monitor's DB9 to switch it.
This bit of inspiration means I need even more help and of a slightly more technical kind!
amordue
01-05-2003, 8:39 AM
You can toggle dtr and rts (or should that be cts?) fairly easily, so as long as you only have 2 things to switch should be fine. What language / os are you using? I can post some code snippets up for C and Java if that would be helpful for you. If you need any help with pic stuff also feel free to ask away.
Have fun
Alex
mephistopheles
02-05-2003, 5:54 AM
I'm using XP Pro and am prepared to use any language (since I'll have to get the books out whatever I do), though I would prefer Assembler, really. And thanks, any/all code snippets would be handy, at least to get an idea of what's involved.
I need to be able to hit a total of 6 pins on the monitor one by one and, separately, a total of 4 simultaneously; x4 each low TTL,(each independently and only all simultaneously) for source selection ; x1 high/low TTL for wireless/external remote selection and x1 high/low TTL for on/standby. It would be along these lines:
Pin 1-4 TTL low - source select
Pin 5 TTL low - manual degauss - not needed
Pin 6 not used
Pin 7 TTL high/low - IR remote/external control method
Pin 8 TTL high/low - standby/on
Pin 9 GND
For example :
from state - standby + external remote + RGB2 source
command0 to pin 8 - low TTL - switch on;
command1 to pin 3 - low TTL - source select;
command2 to pin 1-4 - low TTL - select previous source;
command3 to pin 7 - high TTL - IR remote control;
command4 to pin 7 - low TTL - external control;
command5 to pin 8 - high TTL - shutdown to standby.
Ideally, I would like to be able to operate the program via IRman/Girder and/or X10 Mouse Remote/Max 10.
At an absolute minimum, I need an autorun program which just switches the monitor on from standby when the XP boot sequence is completed and then restores IR remote control over it pending XP shutdown, when that command set is reversed. The whole object, however, is to obviate the need for the monitor's own IR remote (except for manipulation of internal adjustment settings which can only be accessed from it), and to pave the way for something more complex involving macros for autoswitching external (non-HCPC) sources.
I think that's pretty much it. ANY help will be appreciated.
Ivan8192
02-05-2003, 10:03 AM
Assuming you already use your current parallel port, why not just add another parallel port to your PC? After all, your trying to connect to what is effectively a parallel port :)
Something like http://www.thepcstore.co.uk/Controllers/Unbranded/CT1001.php is less than £12. Save you messing around with the Maxim ICs.
Regards,
Ivan.
amordue
03-05-2003, 5:57 PM
Parallel port option is quite nice if you dont want to build any extra hardware, however since you are using XP you will need something like DirectIO or UserPort in order to access the parallel port and toggle the bits.
If you want to use serial, probably using something like the PIC16F84 + some cheap PIC C compiler like C2C (http://www.picant.com ) where there are functions already written for you to send and receive chars via serial.
Assuming you're using Visual C++ on the PC, somehting like this should work for setting up the serial port:-
#include "stdafx.h"
#include <windows.h>
#include <commctrl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define verbose 1
int initComms( void );
HANDLE comport1;
int main(int argc, char* argv[])
{
char received[8];
char transmit[8] = "To Send";
unsigned long cBytes;
// Set up serial port
initComms();
// Write 8 bytes from transmit array to the comport
WriteFile( comport1, &transmit[0], 8, &cBytes, 0 )
// Read 8 bytes from the comport into received array, this operation is blocking, wait until it has 8 chars
ReadFile( comport1, &received[0], 8, &cBytes, 0 );
return 0;
}
int initComms( void ) // Initialise Serial Comms
{
DCB dcb;
COMMTIMEOUTS commtimeouts;
HANDLE hLocal;
hLocal = comport1;
comport1 = INVALID_HANDLE_VALUE;
if ( hLocal != INVALID_HANDLE_VALUE )
CloseHandle ( hLocal );
hLocal = CreateFile( TEXT( "COM1:" ), GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL );
if ( hLocal == INVALID_HANDLE_VALUE )
{
error( "CreateFile COM1: failed\n" );
return 0;
}
dcb.DCBlength = sizeof( DCB );
if ( GetCommState( hLocal, &dcb ) == 0 )
{
error( "GetCommState Failed\n" );
CloseHandle( hLocal );
return 0;
}
// Serial port control values
dcb.BaudRate = 9600;
dcb.fBinary = TRUE;
dcb.fParity = FALSE;
dcb.fOutxCtsFlow = FALSE;
dcb.fOutxDsrFlow = FALSE;
dcb.fDtrControl = DTR_CONTROL_DISABLE;
dcb.fDsrSensitivity = FALSE;
dcb.fTXContinueOnXoff = FALSE;
dcb.fOutX = FALSE;
dcb.fInX = FALSE;
dcb.ErrorChar = FALSE;
dcb.fNull = FALSE;
dcb.fRtsControl = RTS_CONTROL_DISABLE;
dcb.fAbortOnError = FALSE;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
if ( SetCommState( hLocal, &dcb ) == 0 )
{
error( "SetCommState Failed\n" );
CloseHandle( hLocal );
return 0;
}
if ( GetCommTimeouts( hLocal, &commtimeouts ) == 0 )
{
error( "GetCommTimouts Failed\n" );
CloseHandle( hLocal );
return 0;
}
// Sets com1 to wait forever until character(s) arrives the return immediately on completion
commtimeouts.ReadIntervalTimeout = 0;
commtimeouts.ReadTotalTimeoutMultiplier = 0;
commtimeouts.ReadTotalTimeoutConstant = 0;
commtimeouts.WriteTotalTimeoutMultiplier = 10;
commtimeouts.WriteTotalTimeoutConstant = 1000;
if ( SetCommTimeouts( hLocal, &commtimeouts ) == 0 )
{
error( "SetCommTimouts Failed\n" );
CloseHandle( hLocal );
return 0;
}
if( verbose ) printf( "Comms Initialised Succesfully\n" );
comport1 = hLocal;
return 1; // Success
}
If you are using C2C for the PIC the main structure of the program can be generated for you, then you just add a few defines required for serial comms(specified in the docs) such as baud rate, and which pins you want to use for serial, then you just use putc and getc, to transmit and receive chars via serial. Connect PC to PIC via the max233, and connect the 8 inputs on the monitor to port B of the PIC.
Hope this helps
Alex