Program doesn't respond if I send data to dial-up modem - c#

I have the problem that when I send data (AT-Commands) to my Dial-Up Modem my program doesnt respond any more and crashes. I use System.IO.Ports to make this work. This is my code:
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;
using System.Threading;
namespace Windows_XP_AOL
{
public partial class Form6 : Form
{
public Form6()
{
InitializeComponent();
}
private void Form6_Load(object sender, EventArgs e)
{
SerialPort serialPort = new SerialPort("COM1" , 9600);
serialPort.ReadTimeout = 10000;
serialPort.WriteTimeout = 10000;
serialPort.Open();
serialPort.DtrEnable = true;
serialPort.WriteLine("ATA");
serialPort.ReadLine();
serialPort.Close();
}
}
}
On the modem the Send Data Light lights up and the Read Data Light. But when I click on my button that sends the AT-Command (HAYES) (serialPort.WriteLine("ATA")) then my program just doesn't respond anymore and crashes. Because of my timeout function, it doesn't crash but my modem doesn't execute the command that I'm sending. Does someone know what I am making wrong? I can try to explain with pictures if someone doesn't understand it. Please I need this to work! Thanks for any help :)

Related

Is there a main method in C# analogous to C's int main()?

I'm new to C sharp writing a program to read data from a serial port. I'm used to writing in C and admittedly do not have much experience with object oriented languages. I want a piece of code to run to open a serial port without having to be called, ie. it will run as if it's in the int main() in C. I have the code below to try to figure out my problem.
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;
namespace Nav_Monitor
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void t_tick(object sender, EventArgs e)
{
textBox1.Text += Environment.NewLine;
textBox1.Text += DateTime.Now.ToString("hh:mm:ss tt");
}
SerialPort serialPort;
private void openSerialPort(object sender, EventArgs e)
{
serialPort = new SerialPort("COM1", 19200, Parity.None, 8, StopBits.One);
serialPort.Handshake = Handshake.None;
}
}
}
if I move the code outside of the method openSerialPort then I get an error 'the name "serialPort" does not exist in the current context'.
serialPort = new SerialPort("COM1", 19200, Parity.None, 8, StopBits.One);
serialPort.Handshake = Handshake.None;
private void openSerialPort(object sender, EventArgs e)
{
}
So do I need something analogous to main() in C to run code automatically? It doesn't seem like I should put code in my program.cs file which seems to be the analogous place. I'm lost!
The most analogous method is in fact inside Program.cs. Main() is the entry point for C# applications. However, you might consider kicking off any background work after the page has loaded or in the constructor if the work is non-blocking and can be run in the background.
I see you're working in Windows Forms, where the Form.Load event may be a good place to open your serial port.

Enable Microphone using NAudio c#

I have this code that uses NAudio to look for all microphones connected on laptop, andwhen I choose one it shows a meter of the sound in a progressbar.
when I gather all device, I collect them in a dropdwon list, my issue is that when I choose the one microphone from the list it will not be activated and display the sound on a meter unless I start WINDOWS SOUND RECORDER which seems to activate the mic.
how to enable or activate the microphone from the code without starting WINDOWS SOUND RECORDER?
here is the 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 NAudio;
using NAudio.CoreAudioApi;
using System.Threading;
using NAudio.Wave;
using NAudio.Mixer;
namespace NaudioVisualizer
{
public partial class Form1 : Form
{
public int a = 1;
public Form1()
{
InitializeComponent();
MMDeviceEnumerator de = new MMDeviceEnumerator();
var device = de.EnumerateAudioEndPoints(DataFlow.All, DeviceState.Active);
comboBox1.Items.AddRange(device.ToArray());
}
private void timer1_Tick(object sender, EventArgs e)
{
if (comboBox1.SelectedItem != null)
{
var device = (MMDevice)comboBox1.SelectedItem;
progressBar1.Value = (int)Math.Round(device.AudioMeterInformation.MasterPeakValue * 100);
}
}
}
}
I had the same problem try to use the following code :
var waveIn = new WaveIn();
waveIn.StartRecording();

Control a servo with arduino

I'm trying to control a servo with arduino uno and C# but the servo doesn't move
Here is the Arduino code:
#include <Servo.h>
Servo servoT1;
void setup()
{
Serial.begin(9600);
servoT1.attach(9);
}
int i=0;
void loop()
{
if (Serial.available() > 0)
{
i=Serial.read();
i=map(i, 0, 100, 0, 179);
servoT1.write(i);
}
}
Here is the C# 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;
namespace arduino_throttle
{
public partial class Form1 : Form
{
SerialPort uno1 = new SerialPort("COM3", 9600);
public Form1()
{
InitializeComponent();
}
private void hScrollBar1_Scroll(object sender, ScrollEventArgs e)
{
uno1.Open();
uno1.Write(HScroll.ToString());
uno1.Close();
}
}
}
I want to set the servo angle by scrolling the hScrollBar in the program.
the serial.read () method return the first byte of incoming serial data available
http://arduino.cc/en/Serial/Read
but you are sending string (array of chars)
try sending from the C# only one byte or char
to do this you need to check the max value is 256
and another thing
you need to send the value of the scrollbar
(you are sending the entire object in a string)
try this
uno1.Write(Convert.ToByte(HScroll.value));

Why is the program form not loading when accessing file?

With the below code, I've tested it out and the loading of the form works fine standalone, but when the program goes to check if a file exists, the form doesn't load properly and I'm at a loss as to what to do. Is there another method of checking to see if a file exists that I could use in this instance?
EDIT I've made a new 'startup' form to run the file exists check, but it still doesn't work. Again the form loads, but the contents of the form don't and the form itself freezes.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Timers;
using System.IO;
using System.Collections;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using System.Diagnostics;
using System.Windows.Input;
namespace program1
{
public partial class Startup : Form
{
public Startup()
{
InitializeComponent();
}
private void Startup_Load(object sender, EventArgs e)
{
notifyIcons norm = new notifyIcons();
Settings set = new Settings();
set.Show();
string curFile = Environment.CurrentDirectory + "\\age.txt";
if (File.Exists(curFile))
{
norm.Show();
this.Close();
}
else
{
set.Show();
for (;;)
{
if (File.Exists(curFile)) norm.Show(); this.Close();
Application.DoEvents();
}
}
}
}
}
I have no idea what this code is supposed to do.. but I can tell you why its not working.
while (ageFileExists)
That is never false. Therefore, your loop will continually loop... forever. You need to set it false somehow in the loop. I have no idea what sort of rules govern that though.
The reason the form doesn't load is because the loop never exits.. and so the message loop that makes the window do anything can never continue processing window messages.
If you can give more context around what you're trying to do I could help you with a proper solution. As it stands though, I can only see the problem.
while(ageFileExists)
{
if (File.Exists(curFile)) ageFileExists = true;
set.WindowState = FormWindowState.Normal;
}
If that file exists you have an infinite loop. You never set ageFileExists to false and that loop does nothing at all. And where does that label go? I seriously doubt you need to be using goto. Your code doesn't make much sense as it stands.
By using the following code in the main form, it seemed to work a treat! I think the critical part was Application.DoEvents();
Thanks for all of your assistance
InitializeComponent();
set.Show();
set.WindowState = FormWindowState.Minimized;
string curFile = Environment.CurrentDirectory + "\\age.txt";
if (File.Exists(curFile)) goto Labelx;
set.WindowState = FormWindowState.Normal;
for (; ; ) { Application.DoEvents(); if (File.Exists(curFile)) break; }
set.WindowState = FormWindowState.Minimized;
Labelx:TextReader reader = File.OpenText(Environment.CurrentDirectory + "\\age.txt");

Record input from NAudio WaveIn, and output to NAudio WaveOut

I want to be able to get input from a microphone device via NAudio.WaveIn, and then output that exact input to an output device via NAudio.WaveOut.
How would I do this?
Here is the code that worked for me:
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 NAudio.Wave;
using NAudio.CoreAudioApi;
namespace WindowsFormsApplication1
{
public partial class Form4 : Form
{
private BufferedWaveProvider bwp;
WaveIn wi;
WaveOut wo;
public Form4()
{
InitializeComponent();
wo = new WaveOut();
wi = new WaveIn();
wi.DataAvailable += new EventHandler<WaveInEventArgs>(wi_DataAvailable);
bwp = new BufferedWaveProvider(wi.WaveFormat);
bwp.DiscardOnBufferOverflow = true;
wo.Init(bwp);
wi.StartRecording();
wo.Play();
}
void wi_DataAvailable(object sender, WaveInEventArgs e)
{
bwp.AddSamples(e.Buffer, 0, e.BytesRecorded);
}
}
}
The best way would be to use a BufferedWaveProvider as the input to WaveOut. Then in the DataAvailable callback of WaveIn, supply the data recorded to the BufferedWaveProvider
void DataAvailable(object sender, WaveInEventArgs args)
{
bufferedWaveProvider.AddSamples(args.Buffer, 0, args.BytesRecorded);
}
You need to be aware that the default buffer sizes will result in a noticeable delay, so if you were hoping for low latency you might need to experiment a bit with buffer sizes to see how low you can get it.

Categories

Resources