Matlab, RS232, set the pin ! (need help)

Hi, With matlab, I would like to send data with RS232 link, but I have to set the value for matlab :
Pin Signal 2 transmitted data (out) 3 receiving data (in) 5 ground
How can I tell matlab to use tis pin configuration ? My code is for the moment : Is it possible to write "xmit=PIN_C2, rcv = PIN_C3" ???
%%Create the serial interface object,
%create the serial port
s = serial('COM1');
%set some properties
set(s, 'BaudRate', 4800);
set(s, 'FlowControl', 'none');
set(s, 'Terminator', 'LF');
set(s, 'StopBytes', 1);
set(s, 'Parity', 'none');%pair
%open the interface
fopen(s)
Best regards Stanislas

 Accepted Answer

Walter Roberson
Walter Roberson on 22 Jan 2013
Edited: Walter Roberson on 22 Jan 2013
It is not possible to control the serial pin configuration: those are wired into the serial port hardware.
If you are using a standard DB9 serial port on a PC, then the transmit is on line 3 and the receive is on line 2, whereas on standard DB25 serial ports, the transmit is on line 2 and the receive is on line 3.
If you need transmit on line 2 and receive on line 3 and you are using a DB9, then instead of changing the software, you should get a "null modem connector". There is a diagram of one over here. Stores that still carry serial cables at all usually have these in stock.
When you are working with RS232 cables, you really need to know the pin-outs carried through the cable. If you have a serial cable with DB9 on one end, it might already connect the pins the way you want.
Also, you need to remember that for RS232, the transmit of one side needs to feed into the receive of the other (and vice versus).

9 Comments

Okay thanks a lot. I have to explain all my question you have answered recently. I am a student, and I want to connect my computer to a treadmill motor. And set the speed with matlab. But the connection doesn't work. Here is the setting given by the manufacturer :
  • The communication protocol is the "trackmaster TM400" (no info on the web :()
  • the link between the computer and the motor is with RS232 SUB-D-9
  • the Pin are set in this way
2--> emission of data
3--> reception of data
5--> ground
  • Port Configuration is
o 4800 Baud
o Parity none
o 8 bits of data
o 1 bit of start
o 1 bit of stop
And I need through this connection to send order (they said it's ascii) like
  • 'AA' for stopping the motor
  • 'A1' for strating it
  • 'A30200', to run the motor at the speed of 20 km per hour
the fprint command you explained to me in another post, doesn't work, and it seems that my communication code is fine (fopen(s), set (s,. etc...). The only explanation I have, is that my cable is not a good one (but it's a RS232 that fit exactly in the com1 port of my computer, and of the motor)
I realy don't know what's going on. For instance, I have send "A30200" nothing, but when I send "A3+0200", the angle of the treadmill change (or the code to do so is A4+4 octets).
I am a student, completely lost, and you're the only one who help me (a lot). So from all my heart, thank you again.
Best regard
Stanislas
Are you using the TMX-425C or TMX-425CP ?
Stan sdsd
Stan sdsd on 23 Jan 2013
Edited: Walter Roberson on 23 Jan 2013
I am working on the one, the s3040 large treadmill. But If you have information on the protocole used with TMX-425, or CP, it may be usefull (they have RS232 communication, and I think, the protocole may be similar).
It sure is difficult to find information on the command set!
I have this only document, that I have uploaded. The command are in it, but the connection is : The motor can receive informations, but not anything like the order I give to it. For example : A30200 for the speed, and it's the angle of the trendmill that moove (but normally it's a A4 order). Have a look on the pdf for more information. I am completely blocked. http://pdfcast.org/pdf/qsdqsddd
try char(hex2dec('A3')) rather than 'A3'
If you were to send 'A3' then the 'A' would be 0x41 which corresponds to a response code and so would probably be ignored. Then the '3' would be 0x33 which is a request to change the maximum slope.
I cannot tell at the moment whether '0200' would be right or if char([0 2 0 0]) would be right for the "octets"
Tomorrow I will try to send fprintf(s,char(hex2dec('A2'))). A2 is the stop command. So I will manualy run the treadmill, to a speed of 5 km per hour, then do the connection, and send the new fprint command with your advice. I keep you posted. Best regards, and thank you very much for your help Stanislas PS : I will build my own null modem connector to be sure !
Serial cables that are male DB9 on both ends usually connect the lines through the way you would want.
You would not want fprintf(s,char(hex2dec('A2'))) as that will automatically send a line terminator afterwards. Instead use
fprintf(s, '%c', hex2dec('A2'))
Or at least I did not see anything in the pdf about line terminators; it appears to use a binary command sequence without line terminators.
Okay, I have make my serial cable, and with the official software given with the treadmill, I can command the motor the way I Want. You were right, DB9 in null modem was the answer at my first problem. But now, I want to send order to the motor with matlab. The connection is good. But, when I send 'A1' to stop the motor, nothing happend. If I send '0x410x32", It stop. But when I send a command for speed, like "A30200" for 20 km/h, so in hexadecimal, 0x410x330x300x320x300x30 it stop anyway. If I send an order of reset time "A8", as in hex "0x410x38", it stop the motor the same way. It's annoying, I have a protocole that said, (link on this topic), send A2 to stop the motor, but A2 as in fprintf(s,'A2'), don't work, neither do fprintf(s,char(hex2dec('A2'))), and with the hexa code it's a mistake for me, because all the command in hexa don't work. If you have any idea how to manage this problem... Thanks a lot for your previous advices, very helpful. Best regards Stanislas A

Sign in to comment.

More Answers (1)

how can i set status bit 10, 11, 12 and 13 to high or low (TTL) using a DB9 to DB25 adapter in matlab?

8 Comments

Probably this should have gone into a new Question.
Are you asking about sending 13 bits per RS232 character? If so then you cannot do that: 12 bits is the maximum.
Or are you asking about pins 10 (reserved for dataset testing), 11 (unassigned), 12 (secondary carrier detect), and 13 (secondary clear to send)?
If you are asking about pins, then do the pins need to be set independently?
The two pins that can be changed independently of data transmission are DTR and RTS; they can be set as properties of the serial port. An example is http://www.mathworks.com/help/matlab/matlab_external/using-control-pins.html#f85343
The voltage levels of the DB9 port pins would not be TTL levels; they would be +3 to +15, and -3 to -15; see http://en.wikipedia.org/wiki/RS-232#Voltage_levels. TTL would be 0 to +5. However, converting to TTL levels is not difficult with a minor bit of hardware (e.g., a diode could have the effect of limiting the negative voltages to 0 instead, after which you could do something like use the +3 to +15 to control a transistor on a +5 power source.) You can buy RS232 to TTL level convertors fairly easily online.
If you need to control more than 2 of the pins independently, then you are going to run into difficulty, as TX is the only other output pin and it is pulsed with start bits enforced by the UART.
You should consider using multiple serial ports to get 4 controllable bits, or using a DAC to control outputs. Please see http://www.mathworks.co.uk/matlabcentral/answers/59311-hi-low-analog-output-to-usb
dear walter, i wanted to use a usb-to-serial cable (DB9), which has an adapter to DB25 to control an instrument. as you pointed out it is only possible to set the status bits of a DB25 (LPT) connector using a proper usb-to-lpt cable. i guess then matlab can be used easily using the commands such as digitalio, lpt read and lpt write. the binary vector command is then easily to be used for setting the status bits. am i correct? yes, i wanted these 4 status pins 10-13 to be controlled independently.
Walter Roberson
Walter Roberson on 29 Jan 2013
Edited: Walter Roberson on 29 Jan 2013
You should have mentioned parallel earlier :(
I will need to do more research on this.
Windows has been making it harder and harder to access parallel ports, and it is not recommended to design new projects using parallel. Parallel support will be removed in future MATLAB versions.
Even USB to parallel convertor hardware has difficulties. Some of the issues are outlined on this page describing the failures of most solutions; and he says quite frankly that his driver is not stable.
I suggest you examine alternative solutions such as are discussed by Image Analyst over here and also here
dear walter, if i want to use matlab and set the 10-13 status bits on the usb-to-lpt interface, do i use the lptwrite command?
There is no command named "lptwrite" in MATLAB.
I have simliar problem. lptwrite function seems not working well for the usb to lpt interface. I actually use usb to DB-15 interface. Perhaps it is serial port?
lptwrite is not a matlab built in function, it can be found in the link: https://github.com/Psychtoolbox-3/Psychtoolbox-3/wiki/FAQ%3A-TTL-Triggers-in-Windows
I am not familiar with the Psychtoolbox lptwrite function. I have occasionally looked at the workings of Psychtoolbox over the years, but I am not all that familiar with it.
Is it DB-15 you meant, or DB-25 ? If it is DB-15 then is that an SVGA interface? I have seldom encountered DB-15 interfaces other than SVGA. DB-25 interfaces can be serial or parallel.

Sign in to comment.

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!