[CS411X] How do I drive a serial line-based controller? Part2
Dr. Douglas Lyon ( lyon@snet.net)
Fri, 18 Sep 1998 07:44:28 -0500
In my last e-mail on this
subject...I realized that:
String s = "M,0,0,0,0,0,0,7,0,0";
could be:
String s = "M,0,0,0,0,0,0,7,0,0\r\f";
byte b[] = s.getBytes();
Should be enough!
Check the value of the last two bytes by writing
them as int values to the console...is it 13 and 10?
- DL
To formulate a string+ control
characters to an embedded controller, you need to formulate
to different byte arrays and concatenate them.
byte [] st={'M',0,0,0,0,0,0,7,0,0,13,10}; // have
Should be:
String s = "M,0,0,0,0,0,0,7,0,0";
byte b[] = s.getBytes();
byte eb[] = {13,10};
byte turnBytes[] = new byte[b.length+eb.length];
int i=0;
for (; i < b.length; i++) {
turnBytes [i]=b[i];
}
turnBytes[i]=eb[0];
turnBytes[i+1]=eb[1];
now write out the turnBytes....
Got it?
- DL
Hi!
Here is my code.
If you can solve the problem, send me a email.
-Hak Kywn Roh
// Hak Kywn Roh (0367641)
// CPE388X
// Assignment #1
import java.io.*;
import java.util.*;
import javax.comm.*;
public class TurnTable
{
static Enumeration portList;
static CommPortIdentifier portId;
static SerialPort serialPort;
static OutputStream outputStream;
public static void main(String[] args)
{
TurnTable tt=new TurnTable();
System.out.println("getPortIdentifiers...");
portList=CommPortIdentifier.getPortIdentifiers();
System.out.println("getPortIdentifiers... done!");
while (portList.hasMoreElements())
{
portId=(CommPortIdentifier)portList.nextElement();
if
(portId.getPortType()==CommPortIdentifier.PORT_SERIAL)
{
if (portId.getName().equals("COM3"))
{
System.out.println("portId="+portId.getName());
try
{
serialPort=(SerialPort)portId.open("SimpleWriteApp", 2000);
System.out.println("Port
Open");
}
catch (PortInUseException e)
{
System.out.println(e.currentOwner+" owned this port");
System.out.println("Will
try to close");
serialPort.close();
}
try
{
outputStream=serialPort.getOutputStream();
}
catch (IOException e)
{
System.out.println("Could
not get output Stream:"+e);
}
try
{
serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,
SerialPort.STOPBITS_2, SerialPort.PARITY_NONE);
}
catch
(UnsupportedCommOperationException e)
{
System.out.println("Set
Serial Port Params :"+e);
}
try
{
byte [] mn={27, 13, 27, 13, 27, 13, 0};
outputStream.write(mn);
System.out.println("Send
magic number..");
}
catch (IOException e)
{
System.out.println(e);
}
sleepSec(10);
try
{
byte [] st={'M',0,0,0,0,0,0,7,0,0,13,10}; // have
to change this line...
outputStream.write(st);
System.out.println("Move
step.");
}
catch (IOException e)
{
System.out.println(e);
}
//tt.step(1);
//sleepSec(2);
//tt.step(2);
//sleepSec(2);
//tt.step(3);
//sleepSec(2);
serialPort.close();
System.out.println("port closed");
try {
System.in.read();
} catch (java.io.IOException e) {}
}
}
}
}
public void step(int n)
{
System.out.println("Move "+n+" steps");
byte [] st={'M',0,0,0,0,0,0,7,0,0,13,10};
for (int i=0;i<n;i++)
try
{
outputStream.write(st);
}
catch (IOException e)
{
System.out.println(e);
}
}
public static void sleepSec(int sec)
{
try
{
Thread.sleep(sec*1000);
}
catch (InterruptedException e) {}
}
}
Also try:
http://www.ferrettronics.com