串口发送/接收来自pic和pc的 [英] serial port sending/receiving to/from pic and pc

查看:144
本文介绍了串口发送/接收来自pic和pc的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将pic16的PortB(引脚RB0到RB7)定义为连接到它的DIP开关的输入

PortD(引脚RD0到RD7)as输出每个连接到一个LED,PORT B的每个引脚对应一个来自PORT D



如何配置这个端口是第一个输入和输出地方,它应该在图片代码中配置?还是在c#应用程序中?我认为应该是在pic计划中。但我只是想知道



从微控制器读取时我想读取8输出LED的状态一次或我可以收集它们,这怎么可能?



我希望能够使用我的c#程序控制这些LED的状态,但我有一个关于如何接收状态并稍后设置的问题。 br $> b $ b

i尝试阅读,但它是在端口A而不是输出端口(端口d)

我正在使用proteus来刺激电路和comport虚拟仿真器和编写pic mikroc的代码(我是新来的,我从互联网上得到的大部分代码,我有点理解)

i只想知道如何阅读状态像D(101011)这样的东西,以及如果可能的话如何设置portD的每个引脚



我还是新手所有这些我都是只是尽我所能,

原谅我缺乏知识,毕竟我还在学习。

拜托,谢谢!:)





我的c#函数代码可以读写

i have
PortB(pins RB0 through RB7)of the pic16 defined as an input with a dip switch connected to it
PortD(pins RD0 through RD7)as output each connected to a LED,also each pin of PORT B corresponds to one from PORT D

how can i configure that this port is the input and output in the first place and it's supposed to be configured in the pic code? or in the c# application? i think it 's supposed to be in the pic program. but i'm just wondering

when reading from the microcontroller i want to read the status of the 8 output LEDS once or i can collect them, how is that possible?

and i want to be able to control the status of these leds using my c# program but i have an issue on how to receive their status and set it later .

i tried reading but it was on port A not on the output(port d)
i'm using proteus to stimulate the circuit and a comport virtual emulator and to write the code of the pic mikroc (i'm new to it most of the codes i got from the internet and i kinda understood)
i just want to know how to read the status of port D like (101011) pr something like that and how to set each pin of portD if that's possible

i'm still new to all of this i'm just trying my best ,
excuse my lack of knowledge ,afterall i'm still learning .
please, and Thanks !:)


my c# functions code to read and write

SerialPort port ;
void Pread()
{
    port = new SerialPort("COM2", 9600, Parity.None, 8, StopBits.One);
   // port.ReadTimeout = 500;
    port.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
    port.Open();


}

private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
    SerialPort port= (SerialPort)sender;
    string w = port.ReadByte().ToString();
    if (w != String.Empty)
    {
        Invoke(new Action(() => richTextBox1.AppendText(w))); //it will read and append the reading to a richtextbox
    }
}

void Pwrite(string num)
{
    port.WriteTimeout = 500;
    port.Open();
    port.WriteLine(num.ToString());
    port.Close();
}

private void button1_Click(object sender, EventArgs e)
{
    Pread();
}

推荐答案

这篇关于串口发送/接收来自pic和pc的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆