Need help with matlab and serial communication

FireStormGS

[H]ard|Gawd
Joined
Jul 24, 2001
Messages
1,425
I'm working on a project where I need a computer running matlab to communicate with a PLC controller. Right now I have a usb -> serial converter and the end of the serial cable is cut open with all of the wires exposed. I need to be able to control the outputs on 3 of the serial cable wires from 0 to 5V. The problem is that normal serial communication only sends data on 1 pin. I need to know if it possible to override the normal serial communication commands and directly control the voltage on certain pins. If this is not possible I suppose I will have to switch to a parallel cable.
 
You could write your own driver to do it, but that could be interesting in it's own.
 
I was thinking it might come to getting specialized drivers and I was looking on google for direct serial pin control but couldnt find anything. I had to do something similar on another part of the project to get matlab to regonzied a cheapo webcam and get images from it.
 
do you want 0 or 5? or everything in between? If you want everything in between, you are shit out of luck for both cases.

You are surely out of luck with the serial port, you will have to use a parallel port or build some kind of multiplexer. You can't even directly control the state of pins on serial ports.

FireStormGS said:
I'm working on a project where I need a computer running matlab to communicate with a PLC controller. Right now I have a usb -> serial converter and the end of the serial cable is cut open with all of the wires exposed. I need to be able to control the outputs on 3 of the serial cable wires from 0 to 5V. The problem is that normal serial communication only sends data on 1 pin. I need to know if it possible to override the normal serial communication commands and directly control the voltage on certain pins. If this is not possible I suppose I will have to switch to a parallel cable.
 
Whatsisname said:
do you want 0 or 5? or everything in between? If you want everything in between, you are shit out of luck for both cases.

You are surely out of luck with the serial port, you will have to use a parallel port or build some kind of multiplexer. You can't even directly control the state of pins on serial ports.

You can control RTS and DTR with software. However, you cannot toggle TXD high or low, it is serial data only. Might be time to get a USB to parallel cable. Or a DIO-24 type PCI board for around $100.
 
What I need is 0 or 5V, on or off. I basically need to send a signal to the plc controller. And I need this for 3 different pins i.e. I need to be able to control 3 outputs. Ideally I would like to avoid having to use a microcontroller to interpret the signal and if this is the case I will just use a parallel cable instead. I'll have to check if the computer directly next to the plc controller has a parallel port, I think it does, but I'm trying to get this to work on my laptop right now.

If I can get this part figured out the next step is getting a signal from the plc. So I also need one input pin that the computer can monitor. I'm not sure of the voltage that the plc outputs though and is something I have to check into. Here's hoping its only 5V.
 
FireStormGS said:
If I can get this part figured out the next step is getting a signal from the plc. So I also need one input pin that the computer can monitor. I'm not sure of the voltage that the plc outputs though and is something I have to check into. Here's hoping its only 5V.

PLC outputs that I work with are typically 12/24VDC or 120/240VAC.


I'm curious, what PLC? Every PLC I know of has a standard PC interface, either RS232, RS422, RS385, or Ethernet. I've personally written a number of VB apps to log data from various PLCs, along with all the HMI packages you could think of.
 
Ok, I'm now using a basic stamp with a serial connection interface. I'm going to get it to talk with matlab and the stamp will control the hardware pins and whats needs to be high and low. I also found out that the plc has 30V outputs and 15V input lines so I'm going to use the basic stamp and a breadboard to control the voltage change as well.

I was using matlab and the stamp software for awhile today trying to get the two to communicate but I couldnt get it to work even when copying some examples of the net. Matlab would always say it timed out while looking for a connection.

Matlab Code
Code:
s = serial('COM3','BaudRate',2400,'DataBits',8,'Parity', 'none', 'FlowControl', 'none');
s.terminator = 'CR';
s.ReadAsyncMode = 'manual';
fopen(s)

s_scan = fscanf(s,'%d\n');
s_get = fgetl(s); 
s_read = fread(s);
fclose(s);

Basic Stamp Code
Code:
' Variables
test VAR Word

'**********************
' Main Program

test = 6

Main:
  SEROUT 16,2400,[DEC test, CR]

  PAUSE 250
  GOTO Main

END

this is all i get from matlab:

Warning: A timeout occurred before the read operation completed.
Warning: A timeout occurred before the read operation completed.
Warning: The specified amount of data was not returned within the Timeout period.

where as the debug window from the stamp software gives me this, so i know its atleast sending something. it comes it short burst which is I'm assuming the number 6 getting constantly transmitted. Any idea what I have to add to get matlab to pick this up?

ð>>>Àð>>>Àð>>>Àð>>>Àð>>>Àð>>>Àð>>>Àð>>>Àð>>>Àð>>>Àð>>>Àð>>>Àð>>>Àð>>>Àð>>>Àð>>>À
 
Back
Top