- Open a new Visual C# .NET console application.
- In Solution Explorer, right-click the References node and select Add Reference.
- On the COM tab, select Microsoft ADO Ext. 2.7 for DDL and Security, click Select to add it to the Selected Components, and then click OK.
- Delete all of the code from the code window for Class1.cs.
- Paste the following code into the code window:
using System;
using ADOX;
namespace ConsoleApplication1
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
ADOX.CatalogClass cat = new ADOX.CatalogClass();
cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=D:\\AccessDB\\NewMDB.mdb;" +
"Jet OLEDB:Engine Type=5");
Console.WriteLine("Database Created Successfully");
cat = null;
}
}
} Change the path to the new .mdb file as appropriate, and then press F5 to build and run the project.
The new .mdb file will be created in Access 2000 (Jet 4.0) format. For more information about different
Jet formats, see the "References" section of this article.
Saturday, September 19, 2009
Build an Access Database
Tuesday, September 15, 2009
How to disable "Internet explorer script error "

1. Open IE and go to menu "Tools.."
2. Select "Internet Options.."

3. Select tab "Advanced.."
4. See below Browsing..
5. Check in box "Disable Script Debugging (Internet Explorer)"
6. Check in box "Disable Script Debugging (Other)"
7. Remove check box "Display a notification about every script error"
8. Click "Apply" and then Click "OK"
9. Close and Open IE again
10. Now it work!!
Simple Modbus Protocol Class in C#
using System;
using System.Collections.Generic;
using System.Text;
using System.IO.Ports;
namespace Modbus_Poll_CS
{
class modbus
{
private SerialPort sp = new SerialPort();
public string modbusStatus;
#region Constructor / Deconstructor
public modbus()
{
}
~modbus()
{
}
#endregion
#region Open / Close Procedures
public bool Open(string portName, int baudRate, int databits, Parity parity, StopBits stopBits)
{
//Ensure port isn't already opened:
if (!sp.IsOpen)
{
//Assign desired settings to the serial port:
sp.PortName = portName;
sp.BaudRate = baudRate;
sp.DataBits = databits;
sp.Parity = parity;
sp.StopBits = stopBits;
//These timeouts are default and cannot be editted through the class at this point:
sp.ReadTimeout = 1000;
sp.WriteTimeout = 1000;
try
{
sp.Open();
}
catch (Exception err)
{
modbusStatus = "Error opening " + portName + ": " + err.Message;
return false;
}
modbusStatus = portName + " opened successfully";
return true;
}
else
{
modbusStatus = portName + " already opened";
return false;
}
}
public bool Close()
{
//Ensure port is opened before attempting to close:
if (sp.IsOpen)
{
try
{
sp.Close();
}
catch (Exception err)
{
modbusStatus = "Error closing " + sp.PortName + ": " + err.Message;
return false;
}
modbusStatus = sp.PortName + " closed successfully";
return true;
}
else
{
modbusStatus = sp.PortName + " is not open";
return false;
}
}
#endregion
#region CRC Computation
private void GetCRC(byte[] message, ref byte[] CRC)
{
//Function expects a modbus message of any length as well as a 2 byte CRC array in which to
//return the CRC values:
ushort CRCFull = 0xFFFF;
byte CRCHigh = 0xFF, CRCLow = 0xFF;
char CRCLSB;
for (int i = 0; i < (message.Length) - 2; i++)
{
CRCFull = (ushort)(CRCFull ^ message[i]);
for (int j = 0; j < 8; j++)
{
CRCLSB = (char)(CRCFull & 0x0001);
CRCFull = (ushort)((CRCFull >> 1) & 0x7FFF);
if (CRCLSB == 1)
CRCFull = (ushort)(CRCFull ^ 0xA001);
}
}
CRC[1] = CRCHigh = (byte)((CRCFull >> 8) & 0xFF);
CRC[0] = CRCLow = (byte)(CRCFull & 0xFF);
}
#endregion
#region Build Message
private void BuildMessage(byte address, byte type, ushort start, ushort registers, ref byte[] message)
{
//Array to receive CRC bytes:
byte[] CRC = new byte[2];
message[0] = address;
message[1] = type;
message[2] = (byte)(start >> 8);
message[3] = (byte)start;
message[4] = (byte)(registers >> 8);
message[5] = (byte)registers;
GetCRC(message, ref CRC);
message[message.Length - 2] = CRC[0];
message[message.Length - 1] = CRC[1];
}
#endregion
#region Check Response
private bool CheckResponse(byte[] response)
{
//Perform a basic CRC check:
byte[] CRC = new byte[2];
GetCRC(response, ref CRC);
if (CRC[0] == response[response.Length - 2] && CRC[1] == response[response.Length - 1])
return true;
else
return false;
}
#endregion
#region Get Response
private void GetResponse(ref byte[] response)
{
//There is a bug in .Net 2.0 DataReceived Event that prevents people from using this
//event as an interrupt to handle data (it doesn't fire all of the time). Therefore
//we have to use the ReadByte command for a fixed length as it's been shown to be reliable.
for (int i = 0; i < response.Length; i++)
{
response[i] = (byte)(sp.ReadByte());
}
}
#endregion
#region Function 16 - Write Multiple Registers
public bool SendFc16(byte address, ushort start, ushort registers, short[] values)
{
//Ensure port is open:
if (sp.IsOpen)
{
//Clear in/out buffers:
sp.DiscardOutBuffer();
sp.DiscardInBuffer();
//Message is 1 addr + 1 fcn + 2 start + 2 reg + 1 count + 2 * reg vals + 2 CRC
byte[] message = new byte[9 + 2 * registers];
//Function 16 response is fixed at 8 bytes
byte[] response = new byte[8];
//Add bytecount to message:
message[6] = (byte)(registers * 2);
//Put write values into message prior to sending:
for (int i = 0; i < registers; i++)
{
message[7 + 2 * i] = (byte)(values[i] >> 8);
message[8 + 2 * i] = (byte)(values[i]);
}
//Build outgoing message:
BuildMessage(address, (byte)16, start, registers, ref message);
//Send Modbus message to Serial Port:
try
{
sp.Write(message, 0, message.Length);
GetResponse(ref response);
}
catch (Exception err)
{
modbusStatus = "Error in write event: " + err.Message;
return false;
}
//Evaluate message:
if (CheckResponse(response))
{
modbusStatus = "Write successful";
return true;
}
else
{
modbusStatus = "CRC error";
return false;
}
}
else
{
modbusStatus = "Serial port not open";
return false;
}
}
#endregion
#region Function 3 - Read Registers
public bool SendFc3(byte address, ushort start, ushort registers, ref short[] values)
{
//Ensure port is open:
if (sp.IsOpen)
{
//Clear in/out buffers:
sp.DiscardOutBuffer();
sp.DiscardInBuffer();
//Function 3 request is always 8 bytes:
byte[] message = new byte[8];
//Function 3 response buffer:
byte[] response = new byte[5 + 2 * registers];
//Build outgoing modbus message:
BuildMessage(address, (byte)3, start, registers, ref message);
//Send modbus message to Serial Port:
try
{
sp.Write(message, 0, message.Length);
GetResponse(ref response);
}
catch (Exception err)
{
modbusStatus = "Error in read event: " + err.Message;
return false;
}
//Evaluate message:
if (CheckResponse(response))
{
//Return requested register values:
for (int i = 0; i < (response.Length - 5) / 2; i++)
{
values[i] = response[2 * i + 3];
values[i] <<= 8;
values[i] += response[2 * i + 4];
}
modbusStatus = "Read successful";
return true;
}
else
{
modbusStatus = "CRC error";
return false;
}
}
else
{
modbusStatus = "Serial port not open";
return false;
}
}
#endregion
}
}
using System.Collections.Generic;
using System.Text;
using System.IO.Ports;
namespace Modbus_Poll_CS
{
class modbus
{
private SerialPort sp = new SerialPort();
public string modbusStatus;
#region Constructor / Deconstructor
public modbus()
{
}
~modbus()
{
}
#endregion
#region Open / Close Procedures
public bool Open(string portName, int baudRate, int databits, Parity parity, StopBits stopBits)
{
//Ensure port isn't already opened:
if (!sp.IsOpen)
{
//Assign desired settings to the serial port:
sp.PortName = portName;
sp.BaudRate = baudRate;
sp.DataBits = databits;
sp.Parity = parity;
sp.StopBits = stopBits;
//These timeouts are default and cannot be editted through the class at this point:
sp.ReadTimeout = 1000;
sp.WriteTimeout = 1000;
try
{
sp.Open();
}
catch (Exception err)
{
modbusStatus = "Error opening " + portName + ": " + err.Message;
return false;
}
modbusStatus = portName + " opened successfully";
return true;
}
else
{
modbusStatus = portName + " already opened";
return false;
}
}
public bool Close()
{
//Ensure port is opened before attempting to close:
if (sp.IsOpen)
{
try
{
sp.Close();
}
catch (Exception err)
{
modbusStatus = "Error closing " + sp.PortName + ": " + err.Message;
return false;
}
modbusStatus = sp.PortName + " closed successfully";
return true;
}
else
{
modbusStatus = sp.PortName + " is not open";
return false;
}
}
#endregion
#region CRC Computation
private void GetCRC(byte[] message, ref byte[] CRC)
{
//Function expects a modbus message of any length as well as a 2 byte CRC array in which to
//return the CRC values:
ushort CRCFull = 0xFFFF;
byte CRCHigh = 0xFF, CRCLow = 0xFF;
char CRCLSB;
for (int i = 0; i < (message.Length) - 2; i++)
{
CRCFull = (ushort)(CRCFull ^ message[i]);
for (int j = 0; j < 8; j++)
{
CRCLSB = (char)(CRCFull & 0x0001);
CRCFull = (ushort)((CRCFull >> 1) & 0x7FFF);
if (CRCLSB == 1)
CRCFull = (ushort)(CRCFull ^ 0xA001);
}
}
CRC[1] = CRCHigh = (byte)((CRCFull >> 8) & 0xFF);
CRC[0] = CRCLow = (byte)(CRCFull & 0xFF);
}
#endregion
#region Build Message
private void BuildMessage(byte address, byte type, ushort start, ushort registers, ref byte[] message)
{
//Array to receive CRC bytes:
byte[] CRC = new byte[2];
message[0] = address;
message[1] = type;
message[2] = (byte)(start >> 8);
message[3] = (byte)start;
message[4] = (byte)(registers >> 8);
message[5] = (byte)registers;
GetCRC(message, ref CRC);
message[message.Length - 2] = CRC[0];
message[message.Length - 1] = CRC[1];
}
#endregion
#region Check Response
private bool CheckResponse(byte[] response)
{
//Perform a basic CRC check:
byte[] CRC = new byte[2];
GetCRC(response, ref CRC);
if (CRC[0] == response[response.Length - 2] && CRC[1] == response[response.Length - 1])
return true;
else
return false;
}
#endregion
#region Get Response
private void GetResponse(ref byte[] response)
{
//There is a bug in .Net 2.0 DataReceived Event that prevents people from using this
//event as an interrupt to handle data (it doesn't fire all of the time). Therefore
//we have to use the ReadByte command for a fixed length as it's been shown to be reliable.
for (int i = 0; i < response.Length; i++)
{
response[i] = (byte)(sp.ReadByte());
}
}
#endregion
#region Function 16 - Write Multiple Registers
public bool SendFc16(byte address, ushort start, ushort registers, short[] values)
{
//Ensure port is open:
if (sp.IsOpen)
{
//Clear in/out buffers:
sp.DiscardOutBuffer();
sp.DiscardInBuffer();
//Message is 1 addr + 1 fcn + 2 start + 2 reg + 1 count + 2 * reg vals + 2 CRC
byte[] message = new byte[9 + 2 * registers];
//Function 16 response is fixed at 8 bytes
byte[] response = new byte[8];
//Add bytecount to message:
message[6] = (byte)(registers * 2);
//Put write values into message prior to sending:
for (int i = 0; i < registers; i++)
{
message[7 + 2 * i] = (byte)(values[i] >> 8);
message[8 + 2 * i] = (byte)(values[i]);
}
//Build outgoing message:
BuildMessage(address, (byte)16, start, registers, ref message);
//Send Modbus message to Serial Port:
try
{
sp.Write(message, 0, message.Length);
GetResponse(ref response);
}
catch (Exception err)
{
modbusStatus = "Error in write event: " + err.Message;
return false;
}
//Evaluate message:
if (CheckResponse(response))
{
modbusStatus = "Write successful";
return true;
}
else
{
modbusStatus = "CRC error";
return false;
}
}
else
{
modbusStatus = "Serial port not open";
return false;
}
}
#endregion
#region Function 3 - Read Registers
public bool SendFc3(byte address, ushort start, ushort registers, ref short[] values)
{
//Ensure port is open:
if (sp.IsOpen)
{
//Clear in/out buffers:
sp.DiscardOutBuffer();
sp.DiscardInBuffer();
//Function 3 request is always 8 bytes:
byte[] message = new byte[8];
//Function 3 response buffer:
byte[] response = new byte[5 + 2 * registers];
//Build outgoing modbus message:
BuildMessage(address, (byte)3, start, registers, ref message);
//Send modbus message to Serial Port:
try
{
sp.Write(message, 0, message.Length);
GetResponse(ref response);
}
catch (Exception err)
{
modbusStatus = "Error in read event: " + err.Message;
return false;
}
//Evaluate message:
if (CheckResponse(response))
{
//Return requested register values:
for (int i = 0; i < (response.Length - 5) / 2; i++)
{
values[i] = response[2 * i + 3];
values[i] <<= 8;
values[i] += response[2 * i + 4];
}
modbusStatus = "Read successful";
return true;
}
else
{
modbusStatus = "CRC error";
return false;
}
}
else
{
modbusStatus = "Serial port not open";
return false;
}
}
#endregion
}
}
Monday, September 7, 2009
TCP Server Socket Programming in JAVA
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Lolek
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try {
ServerSocket socket = new ServerSocket(5000);
while (true) {
Socket accept = socket.accept();
new SocketThread(accept).start();
}
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
class SocketThread extends Thread
{
Socket socket;
public SocketThread(Socket socket)
{
this.socket=socket;
}
public void run()
{
BufferedReader read = null;
try {
read = new BufferedReader(new InputStreamReader(socket.getInputStream()));
BufferedWriter write = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
String line;
while (true) {
write.write(read.readLine());
write.newLine();
write.flush();
System.out.println(read.readLine());
}
} catch (IOException ex) {
Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
read.close();
} catch (IOException ex) {
Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
TCP Client Socket Programming in JAVA
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Lolek
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try {
String addressIP;
if (args.length == 0) {
addressIP = "127.0.0.1";
} else {
addressIP = args[0];
}
Socket socket = new Socket(addressIP, 5000);
BufferedReader read = new BufferedReader(new InputStreamReader(socket.getInputStream()));
BufferedReader readKeyboard = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter write = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
while (true) {
String line = readKeyboard.readLine();
write.write(line);
write.newLine();
write.flush();
System.out.println(read.readLine());
}
} catch (UnknownHostException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Subscribe to:
Posts (Atom)