|
|
View previous topic :: View next topic |
Author |
Message |
omer64
Joined: 28 Jan 2017 Posts: 1 Location: Uşak
|
C# serialport usage |
Posted: Thu Oct 26, 2017 3:37 pm |
|
|
Firstly, i'm sorry for my bad english!
When i use serialport1.writeline, pic16f877a is freezing.
But, when i use serialport1.write, sometimes pic16f877a freeze, sometimes pic receive data.
How can i solve this problem?
Have nice a day...
Code: |
#include <rxtx.h>
#define LCD_ENABLE_PIN PIN_B0
#define LCD_RS_PIN PIN_B1
#define LCD_RW_PIN PIN_B2
#define LCD_DATA4 PIN_B4
#define LCD_DATA5 PIN_B5
#define LCD_DATA6 PIN_B6
#define LCD_DATA7 PIN_B7
#include <lcd.c>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <input.c>
unsigned long int bilgi=0;
float volt1=0,volt2=0,volt3=0,volt4=0;
unsigned int zaman=0;
char gelen[50]="\0";
char veri1[20]="\0",veri2[20]="\0";
char bol[]="#";
#INT_RTCC
void RTCC_isr(void)
{
disable_interrupts(INT_RDA);
zaman++;
if(zaman>100)
{
zaman=0;
output_toggle(led2);
set_adc_channel(0);
delay_us(20);
bilgi=read_adc();
volt1=(0.0048828125*bilgi)*1000;
set_adc_channel(1);
delay_us(20);
bilgi=read_adc();
volt2=(0.0048828125*bilgi)*1000;
set_adc_channel(2);
delay_us(20);
bilgi=read_adc();
volt3=(0.0048828125*bilgi)*1000;
set_adc_channel(3);
delay_us(20);
bilgi=read_adc();
volt4=(0.0048828125*bilgi)*1000;
fprintf(mavidis,"%07.2f#%07.2f#%07.2f#%07.2f\n\r",volt1,volt2,volt3,volt4);
printf(lcd_putc,"\f%s",veri1);
printf(lcd_putc,"\n%s",veri2);
}
}
#INT_RDA
void RDA_isr(void)
{
disable_interrupts(INT_RDA);
disable_interrupts(INT_RTCC);
fgets(gelen,mavidis);
output_toggle(led1);
strcpy(veri1,strtok(gelen,bol));
strcpy(veri2,strtok(0,bol));
}
void main()
{
setup_adc_ports(ALL_ANALOG);
setup_adc(ADC_CLOCK_DIV_32);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_4|RTCC_8_BIT); //1,0 ms overflow
enable_interrupts(INT_RTCC);
enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);
output_low(led1);
output_low(led2);
lcd_init();
while(TRUE)
{
enable_interrupts(INT_RDA);
enable_interrupts(INT_RTCC);
}
}
|
Code: |
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;
namespace thread_data_received_deneme
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
CheckForIllegalCrossThreadCalls = false;
serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);
string[] portlar = SerialPort.GetPortNames();
foreach (string ports in portlar)
{
comboBox1.Items.Add(ports);
}
}
private void v_Click(object sender, EventArgs e)
{
if (serialPort1.IsOpen == false)
{
try
{
serialPort1.Open();
serialPort1.DiscardInBuffer();
serialPort1.DiscardOutBuffer();
label1.Text = "Bağlandı";
}
catch (Exception hata)
{
MessageBox.Show(hata.Message);
}
}
}
private void button1_Click(object sender, EventArgs e)
{
if (serialPort1.IsOpen == true)
{
try
{
serialPort1.DiscardInBuffer();
serialPort1.DiscardOutBuffer();
serialPort1.Close();
label1.Text = "Bağlantı Kesildi";
}
catch (Exception hata)
{
MessageBox.Show(hata.Message);
}
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{
serialPort1.Write(textBox1.Text.ToString()+ "\0"+(char)13);
serialPort1.DiscardInBuffer();
serialPort1.DiscardOutBuffer();
}
catch (Exception)
{
}
}
string gelen;
string[] veri = new string[5];
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
try
{
gelen = serialPort1.ReadLine();
veri = gelen.Split('#');
serialPort1.DiscardInBuffer();
serialPort1.DiscardOutBuffer();
textBox2.Text = veri[0].ToString();
textBox3.Text = veri[1].ToString();
textBox4.Text = veri[2].ToString();
textBox5.Text = veri[3].ToString();
}
catch (Exception)
{
}
}
private void button3_Click(object sender, EventArgs e)
{
try
{
serialPort1.Write("\0"+(char)13);
textBox1.Clear();
serialPort1.DiscardInBuffer();
serialPort1.DiscardOutBuffer();
}
catch (Exception)
{
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
serialPort1.PortName = comboBox1.Text.ToString();
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
serialPort1.BaudRate = Convert.ToInt32(comboBox2.Text);
}
}
}
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19496
|
|
Posted: Thu Oct 26, 2017 11:06 pm |
|
|
Start by reading the forum.
There are so many things fundamentally 'wrong' that it is difficult to know where to start.
Floating point maths in an interrupt.
Delays in an interrupt.
INT_RDA, means _one_ character is waiting. Not a string.
Disabling interrupts. Why?.
The whole approach is flawed. You have the code that should be in your main, all written inside interrupts.
Look at ex_sisr, for an example of 'how' to do a serial interrupt.
Then all your RTCC interrupt needs do is update a clock.
Repeat the mantra fifty times. All an interrupt handler should do is handle the event that triggered it, and exit ASAP.
Now you don't show your serial setup, but I'd guess it is missing 'ERRORS'. Because of the code faults, if the data arrives while the processor is inside the RTCC interrupt, the UART overflows, and is then hung. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|