1: Serial Port Initialization
COM = new SerialPort ("com3", 9600, parity. Even, 7, stopbits. One );
2: Enable and disable the serial port
If (COM. isopen)
{Com. Close ();}
Com. open ();
If (COM. isopen)
{Com. Close ();}
3: C # convert ASCII characters to ASCII characters
Public static string CHR (INT asciicode)
{
If (asciicode> = 0 & asciicode <= 255)
{
System. Text. asciiencoding = new system. Text. asciiencoding ();
Byte [] bytearray = new byte [] {(byte) asciicode };
String strcharacter = asciiencoding. getstring (bytearray );
Return (strcharacter );
}
Else
{
Throw new exception ("ASCII code is not valid .");
}
}
Public static int ASC (string character)
{
If (character. Length = 1)
{
System. Text. asciiencoding = new system. Text. asciiencoding ();
Int intasciicode = (INT) asciiencoding. getbytes (character) [0];
Return (intasciicode );
}
Else
{
Throw new exception ("character is not valid .");
}
}
4: check and verify the command strings written to the serial port
/// <Summary>
/// And Verification
/// </Summary>
/// <Param name = "data"> </param>
/// <Returns> </returns>
Public String sumcheck (string data)
{
Int sum = 0;
For (INT I = 0; I <data. length; I ++)
{
Sum + = ASC (data. substring (I, 1 ));
}
String res = sum. tostring ("X ");
Res = res. substring (res. Length-2, 2 );
Return res;
}
5: Write to PLC
Private void btnwrite_click (Object sender, eventargs E)
{
String [] Write = new string [] {"2", "2"}; // write the value to be written to the PLC.
// Convert the value to be written into a hexadecimal number and fill in two bytes. Note that the high and low bytes must be exchanged.
String swritedata = "";
For (INT I = 0; I <write. length; I ++)
{
Int val = convert. toint32 (write [I]. length> 0? Write [I]: "0 ");
String S = Val. tostring ("X ");
While (S. Length <4)
{
S = "0" + S;
}
Swritedata + = S. substring () + S. substring );
}
MessageBox. Show (swritedata );
// Write command, 1 indicates writing, 1194 indicates the hexadecimal format of the address D202, 04 indicates D202, and d203 is 4 bytes, 1194 = (202*2) as for the hexadecimal number of + 4096, it indicates the starting position of D202, and Mitsubishi deliberately had to be so troublesome.
Swritedata = "1119404" + swritedata + CHR (3 );
// CHR (2) and CHR (3) are the flag characters that constitute the command. Then, the checksum is added, and the command organization is complete.
Swritedata = CHR (2) + swritedata + sumcheck (swritedata );
MessageBox. Show (swritedata );
// Write to the serial port
Com. Write (swritedata );
// Byte [] DATA = encoding. ASCII. getbytes (swritedata );
// Com. Write (data, 0, Data. Length );
}
6: Read PLC
Private void btnread_click (Object sender, eventargs E)
{
This.txt read0.clear ();
String sreaddata = "";
// Before reading the data in the PLC, you need to send a command to it to send the data to the serial port. In the following string, CHR (2), CHR (3) it is the format mark of the PLC command. In 0119404, 0 indicates reading, 1194 indicates the starting address of D202, and 04 indicates reading D202 and d203. There are 4 bytes in total, the checksum of 0119404 and CHR (3) is used to write the "read" command to the serial port. In fact, it is the same as writing data to the PLC address, but there is no data, and 0 indicates reading.
String sreadcmd = CHR (2) + "0119404" + CHR (3) + "66 ";
Com. Write (sreadcmd );
// Wait 1 second
System. Threading. thread. Sleep (1000 );
// Read data from the serial port
Byte [] DATA = new byte [1024];
Com. Read (data, 0, 1024 );
// If the first row is 2, the data is valid. There is a problem here. In the second row, the first row is '2' and the third row is 2. You need to test it again.
If (data [0] = 2)
{
String sreceivedata = system. Text. encoding. ASCII. getstring (data );
// MessageBox. Show (sreceivedata );
// Resolution command to parse the characters read into numbers. Pay attention to the high/low conversion.
For (INT I = 1; I <8; I + = 4)
{
String slow = sreceivedata. substring (I, 2 );
String shigh = sreceivedata. substring (I + 2, 2 );
// Int res = convert. toint32 (shweigh) + convert. toint32 (slow );
Int res = convert. toint32 (shigh, 16) + convert. toint32 (slow, 16 );
This.txt read0.text + = res. tostring () + ",";
}
}