// Reciver Control Program // Coded by UTAH // Create 2014/12/30 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Net; using System.Threading.Tasks; using System.Windows.Forms; namespace SUPRXCLV00 { public partial class Form1 : Form { // COMMON DATA AREAS char UDPopen_FLG = ' '; string remoteHost; // IP ADDRESS Int32 remotePort = 10040; // PROT-NO // RX use area Int64 BAND_Freq; Int64 MAIN_Freq; Int64 VFO1_Freq; Int64 VFO2_Freq; Int64 VFO3_Freq; // 453.5KHz Int64 VFO4_Freq; // 1732.5KHz Int64 STAT_width; // 100KHz Int64 Fstep; char FREQ_MODE; // L M H char STAT_whpos; // R L char STAT_filnw; // W N char STAT_2ndfl; // W M N char STAT_2ndfc; // filter cnt char STAT_vfo34; // VFO3:0 VFO4:1 char STAT_step; // step cnt char STAT_mode; // L;USB U;USB C;CW R;RTTY A:AM(BFO OFF) char STAT_mcnt; // char STAT_Fmod; // L;low M;modile H;high char STAT_AGC; // AGC A:ON/N:OFF // UDP init // UDP USE System.Net.Sockets.UdpClient UDP = new System.Net.Sockets.UdpClient(10040); // UDP CHECK PROC private delegate void Delegate_RcvData(string Rdata, Int32 Rleng); public Form1() { InitializeComponent(); } // UDP read PROC public void UDP_read_PROC() { string rcvMsg; // UDP read while (UDPopen_FLG == 'O') { try { System.Text.Encoding enc = System.Text.Encoding.UTF8; Console.WriteLine("UDP_read started"); System.Net.IPEndPoint remoteEP = null; byte[] rcvBytes = UDP.Receive(ref remoteEP); rcvMsg = enc.GetString(rcvBytes); //textBoxUDPrdata.Text = rcvMsg; Console.WriteLine("RCV Data:{0}", rcvMsg); Console.WriteLine("RCV adrs:{0}/Port-NO:{1}", remoteEP.Address, remoteEP.Port); Invoke(new Delegate_RcvData(UDPproc), new Object[] { rcvMsg, rcvMsg.Length }); } catch (Exception ex) { //MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); UDPopen_FLG = 'C'; // close } } Console.WriteLine("end of UDP_read"); } // UDP RECIVE DATA PROC private void UDPproc(string Rdata, Int32 Rleng) { Console.WriteLine("UDP Data:{0}", Rdata); textBoxUDPrdata.Text = Rdata; // char cmd0 = Convert.ToChar(Rdata.Substring(0, 1)); char cmd1 = Convert.ToChar(Rdata.Substring(1, 1)); // Command PROCESS if (cmd0 == 'I') // init Command ? { // *********************** C command ************************** if (cmd1 == 'C') // Connect info data ? { UDPset(Rdata); } } if (cmd0 == 'C') // Command ? { // *********************** D command ************************** if (cmd1 == 'D') // RX data ? { UDPset(Rdata); } } } // UDP DATA SET PROC private void UDPset(string Rdata) { BAND_Freq = Convert.ToInt64(Rdata.Substring(12, 10)); MAIN_Freq = Convert.ToInt64(Rdata.Substring(22, 10)); //textBoxFreq.Text = Convert.ToString(MAIN_Freq); textBoxFreq.Text = String.Format("{0:#,0}", MAIN_Freq); VFO1_Freq = Convert.ToInt64(Rdata.Substring(32, 10)); textBoxDDS1F.Text = String.Format("{0:#,0}", VFO1_Freq); VFO2_Freq = Convert.ToInt64(Rdata.Substring(42, 10)); textBoxDDS2F.Text = String.Format("{0:#,0}", VFO2_Freq); VFO3_Freq = Convert.ToInt64(Rdata.Substring(52, 10)); textBoxDDS3F.Text = String.Format("{0:#,0}", VFO3_Freq); VFO4_Freq = Convert.ToInt64(Rdata.Substring(62, 10)); textBoxDDS4F.Text = String.Format("{0:#,0}", VFO4_Freq); Fstep = Convert.ToInt64(Rdata.Substring(82, 10)); if (Fstep == 0) { Fstep = 1; } textBoxStepF.Text = String.Format("{0:#}", Fstep); STAT_Fmod = Convert.ToChar(Rdata.Substring(96, 1)); textBoxFmode.Text = Rdata.Substring(96, 1); } // UDP WRITE DATA PROC private void UDPsend(string Sdata) { if (UDPopen_FLG == 'O') // UDP opened ? { System.Text.Encoding enc = System.Text.Encoding.UTF8; byte[] sendBytes = enc.GetBytes(Sdata); UDP.Send(sendBytes, sendBytes.Length); } } // UDP connect PROC private void button_UDPConn_Click(object sender, EventArgs e) { if (UDPopen_FLG == ' ') // UDP open ? { // use UTF8 code System.Text.Encoding enc = System.Text.Encoding.UTF8; // remote IP & Port-no remoteHost = textBox_UDPIPadr.Text; UDP.Connect(remoteHost, remotePort); // Make task for UDP_read_PROC // Make object System.Threading.Thread UDP_task = new System.Threading.Thread( new System.Threading.ThreadStart(UDP_read_PROC)); // run UDP_task UDP_task.Start(); button_UDPConn.Text = "stop"; UDPopen_FLG = 'O'; // Open string INITDAT = "ICSUPRXCL1 "; // I+C+CLname(8) UDPsend(INITDAT); } else { // clode UDP string LOGOFFDAT = "IOSUPRXCL1 "; UDPsend(LOGOFFDAT); System.Threading.Thread.Sleep(2000); // wait 2Sec UDP.Close(); button_UDPConn.Text = "end "; UDPopen_FLG = 'C'; // Close Application.Exit(); } } private void buttonUDPsend_Click(object sender, EventArgs e) { //送信データを指定する UDPsend(textBoxUDPsdata.Text); } } // Form1 END } // name space end