USB to RS232 converter
We recommend that you solder some headers or stiff wires onto a male DB9 connector so you can plug it right into you breadboard and you can use a standard DB9 male female cable. Although there are 9 connectors in a DB9, there are only three wires that matter, transmit, recieve and ground.
Be sure to connect the transmit of one side to the recieve of the other side. When one side is the microcontroller this is easy because you get to set in software which pins to use for transmit and recieve. When connecting between two hard wired RS232 ports, you may need to insert a Null Modem adapter which crosses the rx and tx wires.
‘''''''''''''BASIC STAMP TX is connected to pin 6. RX is connected to pin 7. inFromSerialVar var byte main: SEROUT 6, 16468, ["A"] SERIN 7, 16468, [inFromSerialVar] DEBUG ? inFromSerialVar GOTO main ‘''''''''''''BASIC ATOM TX is on pin 6; RX is on pin 7 inFromSerialVar var byte main: SEROUT 6, I9600, ["A"] SERIN 7, NI9600, [inFromSerialVar] DEBUG [inFromSerialVar] GOTO main ‘''''''''''''PIC BASIC PRO TX is connected to pin C6. RX is connected to pin C7. inFromSerialVar var byte main: SEROUT2 portc.6, 16468, ["A"] SERIN2 portc.7, 16468, [inFromSerialVar] SEROUT2 portc.6, 16468, DEBUG [“I got: “, inFromSerialVar, 10, 13] GOTO main ‘''''''''''''BX24 TX is on pin 11; RX is on pin 12 Dim InputVar(1 To 10) As Byte Dim OutputVar(1 To 40) As Byte dim outData As byte dim inData As byte Sub main() Call openQueue(InputVar, 10) Call openQueue(OutputVar, 40) Call defineCom3(12, 11, bx1000_1000) Call openCom(3, 9600, InputVar, OutputVar) outData = 65 do Call putQueue(OutputVar, outData, 1) Call getQueue(InputVar, inData, 1) ‘ check to see if there is a byte in the input buffer; ‘ if there is, get it with getQueue() If statusQueue(inputBuffer) = true then call= getQueue(inputBuffer, inData, 1) Call putQueue(outputBuffer, inData, 1) Debug.print cstr(inData) End if Loop End Sub
serout 6, NI9600, [DEC AVar,32,DEC BVar,32,DEC CVar, 44] 'DEC converts number to strings or on a BX OutputString =cstr(AVar)&chr(32)&cstr(BVar)&chr(32)&cstr(CVar)&chr(44) Call putQueueStr(outputVar, outputString)
main: SERIN serin 7, NI9600, 10, noData, [inFromSerialVar] SEROUT serout 6, NI9600, ["A"] DEBUG debug [inFromSerialVarinFromMacVar] goto main NoData: Serout 6, NI9600, [“.”] Goto main
Sometimes the blocking can work to your advantage because it synchronizes the two computers. For instance you could have your microcontroller send some sensor readings and then freeze while waiting for a reply, thus ensuring that it is only supplying as many readings as the other side can handle. These freezing up makes it easy to institute a something that might be termed "polling" or "handshaking" or "call and response" into your protocol.
global serialObject, serialObjectName, serialObjectFilename
global myVar1, myVar2, myVar3
on startMovie
clearglobals
-- variables for setting up the xtra.
-- Fill in your own values from the email
-- that you get from the licence registrar:
serialObjectFilename = "SerialXtra"
serialObjectName = "SerialXtra"
programmerName = "your.email@someisp.com"
serialnum = "xxx-xxxx-xxxx-xxxx "
licenseLength = 90
-- make a new instance of the xtra
openxlib the pathname & "serialXtra.osx"
serialObject = new (xtra serialObjectName, programmerName, serialnum, licenseLength )
-- check that it has been created correctly
if objectP( serialObject ) then
put serialObject
else
alert("Instance not valid")
end if
-- fill in the name of your serial port below:
serialObject.openPort("/dev/cu.USA19HS191P1.1")
-- set the data rate, start bits, etc:
serialObject.setProtocol(9600, "n", 8, 1)
end
on stopMovie
-- dispose of the xtra and close the xtra file
serialObject.closePort()
set serialObject to 0
--closeXlib
end
on exitFrame
doSerial
go the frame
end
on doSerial
-- see if the port’s been opened:
If serialObject.isPortOpen() then
If serialObject.charsavailable() = 0 then
--send a byte to ask for data
SerialObject.writeChar(“A”)
End if
If SerialObject.charsAvailable() >=3 then
--first byte sent by microcontroller:
MyVar = SerialObject.readNumber()
--next byte sent by microcontroller:
MyVar2 = SerialObject.readChar()
--third one sent by microcontroller:
MyVar3 = SerialObject.readChar()
End if
End if
end
import javax.comm.*;
//import gnu.io.*; //use this instead of javax.com if you are using RXTX
import java.io.*;
import java.awt.*;
import java.util.*;
public class SerialExample extends Frame implements SerialPortEventListener {
static SerialExample myFrame;
//we are making this into a frame so we can track the mouse
SerialPort mySerialPort =null;
InputStream in;
OutputStream out;
byte x = 50;
static public void main(String[] args) {
// Print out the list of serial ports,
//in case you don't know the name"
Enumeration portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
CommPortIdentifier portId = (CommPortIdentifier)portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL){
System.out.println(portId.getName() + " " +portId.getCurrentOwner());
}
}
// Change the port name below as needed on the Mac:
myFrame= new SerialExample("COM1",9600);
//the following is all just window management
myFrame.setVisible(true);
myFrame.setLocation(new java.awt.Point(0, 0));
myFrame.setSize(new java.awt.Dimension(255, 450));
myFrame.setLayout(null);
myFrame.setTitle("Serial");
//add a listener for closing the window, in that event call the windowClosing method
myFrame.addWindowListener(new java.awt.event.WindowAdapter() {public void windowClosing(java.awt.event.WindowEvent e) {myFrame.thisWindowClosing(e);}});
//add a listener for moving the mouse in the window,in that event call the mouseMoved method
myFrame.addMouseMotionListener(new java.awt.event.MouseMotionAdapter(){public void mouseMoved(java.awt.event.MouseEvent e) {myFrame.mouseMoved(e);}});
}
public SerialExample (String whichPort, int whichSpeed) {
//which port you want to use and the baud come in as parameters
try {
//find the port
CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier(whichPort);
//open the port
mySerialPort = (SerialPort)portId.open("SerialExample" + whichPort, 2000);
//configure the port
try {
mySerialPort.setSerialPortParams(whichSpeed,
mySerialPort.DATABITS_8,
mySerialPort.STOPBITS_1,
mySerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e){System.out.println("Probably an unsupported Speed");}
//establish streams for reading and writing to the port
try {
in = mySerialPort.getInputStream();
out = mySerialPort.getOutputStream();
} catch (IOException e) { System.out.println("couldn't get streams");}
try {
mySerialPort.addEventListener(this);
mySerialPort.notifyOnDataAvailable(true);
} catch (TooManyListenersException e) {System.out.println("couldn't add listener");}
try {
//send an initial character in case your microcontroller is waiting:
out.write("A".getBytes());
} catch (IOException e) {System.out.println("couldn't send byte");}
}
catch (Exception e) { System.out.println("Port in Use");}
}
public void serialEvent(SerialPortEvent event) {
if (event.getEventType()== SerialPortEvent.DATA_AVAILABLE) {
try {
if (in.available() >= 3) {
// we will wait for three sensor readings
int heat = in.read();
int light = in.read();
int pressure = in.read();
//do something with these numbers
System.out.println(heat + " " + light + " " + pressure);
out.write(x); //send the mouse position back
} //end if for data available
} catch (IOException e) {}
} //it’s a serial port event
}
void thisWindowClosing(java.awt.event.WindowEvent e) {
myFrame.setVisible(false); // Close the window when the close box is clicked
myFrame.dispose();
System.exit(0);
}
public boolean mouseMoved(java.awt.event.MouseEvent evt){
x = (byte) evt.getX();
// Put the mouse coord into a variable called x
return(true);
}
}
int bgcolor; // background color
int fgcolor; // fill color
String serialString = ""; // where we'll put what we receive serially
int serialCount = 0; // a count of how many bytes we've received
float xpos, ypos; // Starting position of the ball
void setup() {
beginSerial(); // Default start serial at 9600 baud
size(200, 200); // stage size
noStroke(); // no border on the next thing drawn
// Set the starting position of the ball (middle of the stage)
xpos = width/2;
ypos = height/2;
serialWrite(65); // send a capital A to start the MC sending
}
void loop() {
background(bgcolor);
fill(fgcolor);
// Draw the shape
ellipse(xpos, ypos, 20, 20);
}
void serialEvent() {
processString((char)serial);
}
void processString(char inByte) {
// add the latest byte from the serial port to the string to be parsed:
serialString += inByte;
// update the string length:
serialCount = serialString.length();
// if we have 4 bytes, parse the string:
if (serialCount > 2 ) {
fgcolor = (int)serialString.charAt(0);
xpos = (float)serialString.charAt(1);
ypos = (float)serialString.charAt(2);
// clear the string when we're done, and ask for more:
serialString = "";
// send a capital A to request new sensor readings:
serialWrite(65);
}
}
Often, there are connections for both sending and receiving, just as with asynchronous serial communication, for a total of four connections. One side of the communication is called the master because it supplies the timing pulses on the clock connection. The connections for synchronous serial are usually labeled MISO, for Master In, Slave Out (meaning that the data is going from the slave to the master) and MOSI, for Master Out, Slave In (going from the master to the slave). Sometimes you’ll see the terms SDI (serial data in) and SDO (serial data out). The clock will sometimes be labeled SCK, SCLK, or CLK.
In addition to the data lines, there’s usually a chip select connection. Several synchronous serial devices can be connected to one bus, meaning that you can run the data lines in parallel from one master controller to several slaves. In order to direct the flow of data in synchronous serial, each slave gets its own chip select connection to the master. This way, the master can set a given slave’s chip select pin to tell it to listen and to tell all the other chips to ignore the data. Generally, the chip select is taken low (given 0 volts) to select a given chip and taken high (given 5 volts) to tell the chip to ignore data. This is called an active low pin, and it’s indicated by a bar over the top of the pin in the schematic.
Checkout the the shiftin and shiftout commands for your microcontroller. You will have to supply which pins you are using as data, and clock, whether you are sending info feet first or head first (probably MSBFIRST) and finally a variable to be sent or to be filled with incoming data.
[Add to or Correct This Page]|[Yes, this is a Wiki]