Khung truyền thông:
STX + Command + Data + ETX
Với:
STX: 1 byte
Command : 1 byte (Write: 'W')
Data: 2 byte (byte thấp, byte cao)
ETX: 1 byte
Example.zip
STX + Command + Data + ETX
Với:
STX: 1 byte
Command : 1 byte (Write: 'W')
Data: 2 byte (byte thấp, byte cao)
ETX: 1 byte
Code:
#include <16F877A.h> // PIC16F877 header file
#use delay(clock=8000000) // for 8Mhz crystal
#fuses HS, NOWDT, NOPROTECT, NOLVP // for debug mode
#include "LCD_LIB.c"
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, stream=MYPC)
int8 temp;
int8 buffRev[5];// Bộ đệm nhận.
int8 idx = 0;
unsigned int32 value = 0; // KẾT QUẢ
void display();
void Initialize();
#INT_RDA
void serialPort_itr()
{
if(0 != kbhit())
{
temp = fgetc(MYPC);
if(temp==0x02 || temp==0x03) // Nếu gặp ký tự STX hoặc ETX thì
{
if(buffRev[0] == 'W') // nếu là lệnh ghi(W) thì
{
/ / byte cao = buffRev[1]*256, byte thấp = buffRev[2].
value = buffRev[1]*256 + buffRev[2];
LCD_clear();
}
idx = 0;
return;
}
buffRev[idx++]=temp; // gán dữ liệu vào bộ đệm nhận buffRev
}
}
void main(void)
{
Initialize();
do
{
display(); // Hiển thị K.Q
}while(TRUE);
}
// Hàm khởi tạo mọi thứ.
void Initialize()
{
enable_interrupts(INT_RDA); // cho phép ngắt truyền thông nối tiếp
enable_interrupts(GLOBAL); // cho phép ngắt toàn cục
lcd_init(); // Init LCD.
}
void display()
{
lcd_gotoxy(1,1);
printf(lcd_putc, "16 bit: %ld", value);
}
=================================================
VISUAL C# Source
=================================================
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
namespace HMI_PIC16F877A
{
public partial class FRMMAIN : Form
{
private const char STX = (char)2; // Start of text.
private const char ETX = (char)3; // End of text.
private string BufferRecv = "";
private byte[] BuffSend;
private SerialPort serialPort = null;
public FRMMAIN()
{
InitializeComponent();
}
private void FRMMAIN_Load(object sender, EventArgs e)
{
Initialize();
}
private void FRMMAIN_FormClosed(object sender, FormClosedEventArgs e)
{
try
{
if (serialPort.IsOpen) serialPort.Close();
}
catch (Exception ex)
{
MessageBox.Show(this, ex.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
private void btnGui_Click(object sender, EventArgs e)
{
try
{
char command = 'W'; // Lệnh ghi
UInt16 value = UInt16.Parse(txtGui.Text); // giá trị nguyên dương 16 bit.
BuffSend = new byte[5];
BuffSend[0] = (byte)STX;
BuffSend[1] = (byte)command;
BuffSend[2] = (byte)(value >> 8); // dịch sang phải 8 bit(byte cao).
BuffSend[3] = (byte)(value); //(byte thấp).
BuffSend[4] = (byte)ETX;
serialPort.Write(BuffSend, 0, BuffSend.Length); // gửi message này xuống PIC.
}
catch (Exception ex)
{
MessageBox.Show(this, ex.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
private void Initialize()
{
try
{
serialPort = new SerialPort();
serialPort.PortName = "COM3";
serialPort.BaudRate = 9600;
serialPort.DataBits = 8;
serialPort.Parity = Parity.None;
serialPort.StopBits = StopBits.One;
if (serialPort.IsOpen) serialPort.Close();
serialPort.Open();
}
catch (Exception ex)
{
MessageBox.Show(this, ex.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
}
Example.zip

Comment